English 中文(简体)
许多关系 Yii2
原标题:Many To Many relations Yii2

I need help with this code, I m new to Yii2. I m building a sample project to start, and I don t know why my code gets the right result but doesn t save the ids I need into the many-to-many table for the relationship. I started from this sample in the wiki: 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 = yiihelpersArrayHelper::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 ) ?>
最佳回答

之所以失败,是因为在多种模式中制定该守则将使模型变得ir脏。 更好的办法是使用能够管理你模式中关系的普遍组成部分。 例如,yii2-many-to-many-behavior

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签