thinkphp5.1 学习笔记 【多态关联】

  $result = Draft::update($input, ['id' => $input['id']], true);
                if (!empty(array_get($input, 'house_id'))) {
                    $houses = Houses::get(array_get($input, 'house_id'));
                    $result->objContent()->associate($houses);
                }
   public function objContent()
    {
        return $this->morphTo(['module', 'obj_id']);
    }
  /**
     * 添加关联数据
     * @access public
     * @param  Model     $model  关联模型对象
     * @param  string    $type   多态类型
     * @return Model
     */
    public function associate($model, $type = '')
    {
        $morphKey  = $this->morphKey;
        $morphType = $this->morphType;
        $pk        = $model->getPk();

        $this->parent->setAttr($morphKey, $model->$pk);
        $this->parent->setAttr($morphType, $type ?: get_class($model));
        $this->parent->save();

        return $this->parent->setRelation($this->relation, $model);
    }

猜你喜欢

转载自www.cnblogs.com/Json159/p/10292350.html
今日推荐