#php #yii2
#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 appmodels;
use Yii;
use yiibaseNotSupportedException;
use yiibehaviorsTimestampBehavior;
use yiidbActiveRecord;
use yiiwebIdentityInterface;
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 appmodels;
use Yii;
use yiibaseNotSupportedException;
use yiibehaviorsTimestampBehavior;
use yiidbActiveRecord;
use yiiwebIdentityInterface;
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 appmodels;
use Yii;
use yiibaseNotSupportedException;
use yiibehaviorsTimestampBehavior;
use yiidbActiveRecord;
use yiiwebIdentityInterface;
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 appmodels;
use Yii;
use yiibaseNotSupportedException;
use yiibehaviorsTimestampBehavior;
use yiidbActiveRecord;
use yiiwebIdentityInterface;
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 – yiibaseInvalidParamException
appmodelsActiveCurriculum has no relation named "glhead".
↵
Caused by: Unknown Method – yiibaseUnknownMethodException
Calling unknown method: appmodelsActiveCurriculum::getglhead()
in C:xampphtdocsenrollmentvendoryiisoftyii2baseComponent.php at line 285
Я делаю что-то не так с отношениями или это что-то еще? Помощь была бы очень благодарна. Спасибо.
Комментарии:
1.
ActiveCurriculum
класс?2. да, в файле php
Ответ №1:
Все эти отношения:
->joinWith('schead')
->joinWith('glhead')
->joinWith('glfees')
должно быть объявлено в ActiveCurriculum
классе. Обратитесь к связям в документации Yii2.