gpt4 book ai didi

php - 获得连接表 Yii 2 的值(value)

转载 作者:行者123 更新时间:2023-11-30 22:13:22 31 4
gpt4 key购买 nike

你好,我正在尝试从连接表 schad.section 中访问一个值以存储在 subjectcontainer.section 中,主要是我使用的是 scstock 数据,但 section 部分位于 schad.section 中,所以我所做的是加入schead 和 schstock 在一起,这样我就可以访问部分列。这是我所做的。

     $subject = ActiveCurriculum::find()
->select('scstock.*')
->leftJoin('schead', 'schead.TrNo = scstock.TrNo')
->where([ 'schead.TrNo' => $TrNo])
->one();

$activesubject = new ActiveSubject();
$activesubject->clientid = $clientid;
$activesubject->TrNo = $subject->TrNo;
$activesubject->subjectcode = $subject->subjectcode;
$activesubject->schedday = $subject->schedday;
$activesubject->schedtime = $subject->schedtime;
$activesubject->section = $subject->section;
$activesubject->room = $subject->room;
$activesubject->units = $subject->units;
$activesubject->save();
//reduces the slot of ccsubject by 1
$subject->slots = $subject->slots - 1;
//never forget the saving part
$subject->save();

首先 $subject 将访问 sctock 表以通过 TrNo 加入 will schead。然后 $activesubject 将访问 subjectcontainer 表以存储值。现在我的问题是我收到此错误。

enter image description here

有人可以帮我解决这个问题吗?

最佳答案

确保在模型中定义与 Schead 对象的关系。这种关系的例子:

/**
* @property $schead Schead
*/
class YourModel extends \yii\db\ActiveRecord
{
/**
* @return Schead
*/
public function getSchead()
{
return $this->hasOne(Schead::className(), ['field1' => 'field2']);
}
}

此外,$subject->schead.section 是访问相关模型属性的错误方式。请改用 $subject->schead->section。如果有 schad 是可选的,不要忘记先检查相关对象是否存在,例如:

$subject->schead ? $subject->schead->section : null

还要检查代码是否有拼写错误(可能是 sched/schead?)。您可以在 official docs 中阅读有关处理关系的更多信息.

关于php - 获得连接表 Yii 2 的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284280/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com