B
B
bizzonaru2016-08-07 07:31:16
Yii
bizzonaru, 2016-08-07 07:31:16

Tell me how in YII (ActiveRecord) to specify which column to return from the table?

Now it turns out, I just do a loop through the loop to select a value.

/**
     * @return \yii\db\ActiveQuery
     */
    public function getAuthors() 
    {
        $data = $this->hasMany(Authors2products::className(), ['products_id' => 'id'])->asArray()->all();
        if(count($data)) {
            $result = [];
            foreach($data as $k => $v) {
                $result[] = $v['authors_id'];
            }
            return $result;
        }
        return [];
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Gogolinsky, 2016-08-07
@gogolinsky

$data = $this->hasMany(Authors2products::className(), ['products_id' => 'id'])->select('authors_id')->column()

A
Andrew, 2016-08-07
@mhthnz

...
return \yii\helpers\ArrayHelper::getColumn($data, 'authors_id');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question