joinWith多个表yii 2

时间:2016-10-19 03:51:50

标签: php yii2

您可以根据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']);
  }


  }?>

Schead

<?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']);
  }

  }?>

Glhead

<?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']);
  }

  }?>

Glfees

<?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']);
  }




  }?>

但是当查询执行时我收到此错误

enter image description here

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

我是否对这些关系做错了,还是其他什么?帮助会很大的帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

所有这些关系:

->joinWith('schead')
->joinWith('glhead')
->joinWith('glfees')

必须在ActiveCurriculum课程中声明。咨询relations in Yii2 documentation