Yii2 leftJoin()不使用find()

时间:2017-12-15 17:05:18

标签: php yii2

“photo_1”是“anmt_photo”的列,“title”是“anmt”表的列,但是当我使用

<?= $anmt->title; ?>

它正在运作但是

<?= $anmt->photo_1; ?>

不工作

这是我的代码:

$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();

2 个答案:

答案 0 :(得分:1)

$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');

如果您要加载许多ActiveRecords,您可能会发现声明关系并使用joinWith()with()而不是leftJoin()来收集数据更有效。

更多阅读docs

答案 1 :(得分:0)

您需要将其他表的列名添加到: -

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