【发布时间】:2015-10-28 09:20:33
【问题描述】:
在控制器中我有以下代码:
public function actionView($id)
{
$query = new Query;
$query->select('*')
->from('table_1 t1')
->innerJoin('table_2 t2', 't2.t1_id = t1.id')
->innerJoin('table_3 t3', 't2.t3_id = t3.id')
->innerJoin('table_4 t4', 't3.t4_id = t4.id')
->andWhere('t1.id = ' . $id);
$rows = $query->all();
return $this->render('view', [
'model' => $this->findModel($id),
'rows' => $rows,
]);
}
查看数据库架构:https://github.com/AntoninSlejska/yii-test/blob/master/example/sql/example-schema.png
视图view.php中显示的是table_2-4中的数据,与table_1相关:
foreach($rows as $row) {
echo $row['t2_field_1'];
echo $row['t2_field_2'];
...
}
见:Yii2 innerJoin() 和:http://www.yiiframework.com/doc-2.0/yii-db-query.html
它有效,但我不确定它是否是最正确的 Yii2 方式。
我尝试在模型 TableOne 中定义关系:
public function getTableTwoRecords()
{
return $this->hasMany(TableTwo::className(), ['t1_id' => 'id']);
}
public function getTableThreeRecords()
{
return $this->hasMany(TableThree::className(), ['id' => 't3_id'])
->via('tableTwoRecords');
}
public function getTableFourRecords()
{
return $this->hasMany(TableFour::className(), ['id' => 't4_id'])
->via('tableThreeRecords');
}
然后在控制器TableOneController中加入记录:
$records = TableOne::find()
->innerJoinWith(['tableTwoRecords'])
->innerJoinWith(['tableThreeRecords'])
->innerJoinWith(['tableFourRecords'])
->all();
但它不起作用。如果我只加入前三个表,那么它可以工作。如果我添加第四个表,则会收到以下错误消息:“获取未知属性:frontend\models\TableOne::t3_id”
如果我以这种方式更改函数 getTableFourRecords():
public function getTableFourRecords()
{
return $this->hasOne(TableThree::className(), ['t4_id' => 'id']);
}
然后我收到此错误消息:“SQLSTATE[42S22]: Column not found: 1054 Unknown column 'table_4.t4_id' in 'on clause'
正在执行的 SQL 是: SELECT table_1.* FROM table_1 INNER JOIN table_2 ON table_1.id = table_2.t1_id INNER JOIN table_3 ON t3_id7@. = table_3.id INNER JOIN table_4 ON table_1.id = table_4.t4_id"
【问题讨论】:
-
我想你这里有一个类型:
['id', 't3_id'])它应该是['id' => 't3_id'] -
@TouqeerShafi 谢谢。看起来我一直有错字。我必须是盲人。我现在再次尝试一切。
-
@TouqeerShafi 我需要一些时间来测试它,因为真正的数据库方案看起来完全不同......它现在对前三个表工作正常。我不知道,如何将第四个表正确添加到模型中。我编辑了这个问题。您可以看到我现在收到的错误。
标签: yii2 inner-join