php - 连接多个表 yii 2

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


  }?>

施海德

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




  }?>

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

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

我在关系方面做错了什么还是其他什么?非常感谢您的帮助。谢谢。

最佳答案

所有这些关系:

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

必须在ActiveCurriculum类中声明。咨询relations in Yii2 documentation .

关于php - 连接多个表 yii 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40121799/

相关文章:

PHPOffice/PHPWord - 如何设置横向纸张尺寸

php - WordPress 如何以两个为一组列出所有子页面

postgresql - 如何在 Yii2 中设置默认模式

database - yii2 动态改变数据库

yii2 - createAbsoluteUrl 在控制台应用程序中不起作用

mysql - Yii2 获取列的总和

php - PDF 文件是否可以定义 0 个页面,否则会导致页面大小为 0?

php - 如何使 CakePHP 重定向到不同的操作/ Controller ?

php - Symfony 2.8 app no style 加载资源失败

php - 如何自动重新打开与 MySQL 的持久连接