您好,我正在尝试从已连接的表schead.section中访问值以存储在subjectcontainer.section中,主要是我使用了scstock数据,但该部分位于schead.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加入schead。然后$activesubject将访问subjectcontainer表以存储值。现在我的问题是出现此错误。

php - 获取联接表Yii 2的值(value)-LMLPHP

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

最佳答案

确保在模型中定义与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。如果具有schead是可选的,请不要忘记首先检查相关对象的存在,例如:

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


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

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

10-14 00:19