B
B
Boris Yakushev2016-09-18 23:01:15
Yii
Boris Yakushev, 2016-09-18 23:01:15

Getting value from db yii2?

Greetings.
At the moment, I get the id value from the database like this:

$id = UserSubscription::find()->where(['user' => 16])->one();
$id = $id->id;

Is it possible to assign $id the value of a column in one line?
I tried like this, but it does not return a value, but an object.
$id = UserSubscription::find()->select(['id'])->where(['user' => 16])->one();

Thanks everyone for the advice.
ps Edited the question, because. due to the fact that I have a count column in the database, I misled everyone :)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
mitaichik, 2016-09-19
@mitaichik

There is a scalar method for this, that is, it will be like this:

UserSubscription::find()->select(['id'])->where(['user' => 16])->scalar()

D
Dmitry, 2016-09-18
@slo_nik

Goodnight.
Like so

$count = UserSubscription::find()
    ->where(['user' => 16])
    ->count();

P
Pavel Gogolinsky, 2016-09-18
@gogolinsky

UserSubscription::find()->where(['user' => 16])->count()

A
Andrew, 2016-09-19
@mhthnz

$data = UserSubscription::find()->select(['id'])->where(['user' => 16])->column();
$id = array_shift($data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question