G'ofur N
G'ofur N

Reputation: 2651

Yii2 leftJoin() not working with find()

"photo_1" is the column of the "anmt_photo" and "title" is the column of the "anmt" table but when I use

<?= $anmt->title; ?>

it is working but

<?= $anmt->photo_1; ?>

not working

Here is my code :

$anmt_t = Anmt::find()
  ->select('*')
  ->leftJoin('anmt_photo', 'anmt_photo.photo_key = anmt.anmt_key')
  ->leftJoin('anmt_categ', 'anmt_categ.categ_key = anmt.anmt_key');

$count = $anmt_t->count();

// create a pagination object with the total count
$pagination = new Pagination(['defaultPageSize' => 20, 'totalCount' => $count]);


// limit the query using the pagination and retrieve the articles
$anmts = $anmt_t->offset($pagination->offset)
    ->limit($pagination->limit)
    ->all();

Upvotes: 1

Views: 802

Answers (2)

Pedro del Sol
Pedro del Sol

Reputation: 2841

$anmt_t = Anmt::find()
   ->select(['anmt_photo.photo_1', 'anmt.title']) // and other fields...
   ->leftJoin('anmt_photo', 'anmt_photo.photo_key = anmt.anmt_key')
   ->leftJoin('anmt_categ', 'anmt_categ.categ_key = anmt.anmt_key');

if you are loading many ActiveRecords, you may find it more efficient to declare relations and use joinWith() or with(), rather than leftJoin() to gather your data.

more reading in the docs

Upvotes: 1

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You need to add column names of other tables to:-

->select(['anmt.*','anmt_photo.photo_1'])

Upvotes: 0

Related Questions