多对多关系Yii2

loading...


0

我需要有关此代码的帮助,我需要构建一个示例项目,并且我不需要将我需要的ID保存到关系的多对多表中 . 我从维基中的这个示例开始:https://www.yiiframework.com/wiki/708/book-has-author-many-to-many-relations-using-kartikselect2

我的模特

public function rules()
{
    return [
        [['anno', 'durata', 'flagdelete', 'categoriaid'], 'integer'],
        [['titolo', 'riassunto', 'regista', 'descrizione'], 'string', 'max' => 50],
        [['attoriIds'], 'safe'],
    ];
}

public function attributeLabels()
{
    return [
        'titolo' => 'Titolo',
        'anno' => 'Anno',
        'durata' => 'Durata',
        'riassunto' => 'Riassunto',
        'regista' => 'Regista',
        'descrizione' => 'Descrizione',
        'categoriaid' => 'Categoria',
        'nome' => 'Attori',
    ];
}

/**
 * @var array
 */
public $attoriIds = [];

public function getdropAttori()
{
    $data = Attori::find()->asArray()->all();
    return ArrayHelper::map($data, 'id', 'nome');
}

/**
 * @return mixed
 */
public function getAttoriIds()
{
    return $this->attoriIds;
}

/**
 * @param $attoriIds
 */
public function setAttoriIds($attoriIds)
{
    $this->attoriIds = \yii\helpers\ArrayHelper::getColumn(
        $this->getAttdvd()->asArray()->all(),
        'attori_id'
    );
}

/**
 * @param $insert
 * @param $changedAttributes
 */
public function afterSave($insert, $changedAttributes)
{
    $actualAttoris = [];
    $attoriExists = 0;

    if (($actualAttoris = Attdvd::find()->andWhere(
        "dvd_id = $this->id"
    )->asArray()->all()) !== null
    ) {
        $actualAttoris = ArrayHelper::getColumn($actualAttoris, 'attori_id');
        $attoriExists = 1;
    }

    if (!empty($this->despIds)) {
        foreach ($this->despIds as $id) {
            $actualAttoris = array_diff($actualAttoris, [$id]);
            $r = new Attdvd();
            $r->dvd_id = $this->id;
            $r->attori_id = $id;
            $r->save();
        }
    }

    if ($attoriExists == 1) {
        foreach ($actualAttoris as $remove) {
            $r = Attdvd::findOne(
                ['attori_id' => $remove, 'dvd_id' => $this->id]
            );
            $r->delete();
        }
    }
    parent::afterSave($insert, $changedAttributes);
}

/**
 * @return mixed
 */
public function getAttdvd()
{
    return $this->hasMany(Attori::className(), ['id' => 'attori_id'])->viaTable(
        'attdvd', ['dvd_id' => 'id']
    );
}

我的控制器

public function actionCreate()
{
    $model = new Dvd();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    }

    return $this->render('create', [
        'model' => $model,
    ]);

}

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    //i think the problem is the line below
    $model->attoriIds = $model->AttoriIds;

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    }

    return $this->render('update', [
        'model' => $model,
    ]);
}

我的表格(工作正常):

<?= $form->field($model, 'AttoriIds')->widget(Select2::classname(), ['data'=>$model->dropAttori, 'options' => ['multiple' => true, 'placeholder' => 'Seleziona attori']])->label('Attori') ?>
1回答

  • 1

    这是不好的方式,因为在多个模型中制作该代码会使模型更脏 . 更好的方法是使用可以管理模型中关系的通用组件 . 例如yii2-many-to-many-behavior

评论

loading...

暂时没有评论!