嗨,yii 2 中可以根据列连接多个表吗?现在我正在使用 joinWith
看看我的代码:
ActiveCurriculum.php 这就是我加入表格的地方
public static function Addblock($group, $clientid){
$subjects = ActiveCurriculum::find()
->select(['scstock.*', 'schead.*', 'glhead.*', 'glfees.*'])
->joinWith('schead')
->joinWith('glhead')
->joinWith('glfees')
->where([
'scstock.sectiongroup' => $group
])
->asArray()
->all();
}
这些是我设置的关系:
Scstock
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class Scstock extends ActiveRecord{
public static function tableName()
{
return '{{%scstock}}';
}
public function getSchead(){
return $this->hasOne(Schead::className(), ['TrNo' => 'TrNo']);
}
public function getGlhead(){
return $this->hasOne(Glhead::className(), ['TrNo' => 'TrNo']);
}
public function getGlfees(){
return $this->hasOne(Glfees::className(), ['TrNo' => 'TrNo']);
}
}?>
施海德
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class Schead extends ActiveRecord{
public static function tableName()
{
return '{{%schead}}';
}
public function getScstock(){
return $this->hasOne(ActiveCurriculum::className(), ['TrNo' => 'TrNo']);
}
public function getGlhead(){
return $this->hasOne(Glhead::className(), ['TrNo' => 'TrNo']);
}
public function getGlfees(){
return $this->hasOne(Glfees::className(), ['TrNo' => 'TrNo']);
}
}?>
GL头
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class Glhead extends ActiveRecord{
public static function tableName()
{
return '{{%glhead}}';
}
public function getScstock(){
return $this->hasOne(ActiveCurriculum::className(), ['TrNo' => 'TrNo']);
}
public function getSchead(){
return $this->hasOne(Schead::className(), ['TrNo' => 'TrNo']);
}
public function getGlfees(){
return $this->hasOne(Glfees::className(), ['TrNo' => 'TrNo']);
}
}?>
Glfes
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class Glfees extends ActiveRecord{
public static function tableName()
{
return '{{%glfees}}';
}
public function getGlhead(){
return $this->hasOne(Glhead::className(), ['TrNo' => 'TrNo']);
}
public function getSchead(){
return $this->hasOne(Schead::className(), ['TrNo' => 'TrNo']);
}
public function getScstock(){
return $this->hasOne(ActiveCurriculum::className(), ['TrNo' => 'TrNo']);
}
}?>
但是当查询执行时我收到此错误
Invalid Parameter – yii\base\InvalidParamException
app\models\ActiveCurriculum has no relation named "glhead".
↵
Caused by: Unknown Method – yii\base\UnknownMethodException
Calling unknown method: app\models\ActiveCurriculum::getglhead()
in C:\xampp\htdocs\enrollment\vendor\yiisoft\yii2\base\Component.php at line 285
我在关系方面做错了什么还是其他什么?非常感谢您的帮助。谢谢。
最佳答案
所有这些关系:
->joinWith('schead')
->joinWith('glhead')
->joinWith('glfees')
必须在ActiveCurriculum
类中声明。咨询relations in Yii2 documentation .
关于php - 连接多个表 yii 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40121799/