Yii2 wbraganca-dynamicform更新使用yii2-relation-trait不删除项目

时间:2017-04-24 10:49:53

标签: yii2 yii2-model

基于https://github.com/wbraganca/yii2-dynamicform/wiki/Dynamic-Forms-With-Yii2-relation-trait-(VERY-EASY),我正在尝试实现动态表单.Create工作正常,但在Update表单中,如果我删除任何动态表单元素,它不会被删除,但是如果我添加了更新动作,它正在保存。 这是我的更新代码

public function actionUpdate($id)
{
    $modelAlumni = $this->findModel($id);
    $modelsJob = $modelAlumni->jobs;


    if ($modelAlumni->loadAll(Yii::$app->request->post()) && $modelAlumni->saveAll()) {
        return $this->redirect(['view', 'id' => $modelAlumni->id]);
    } else {
        return $this->render('update', [
            'modelAlumni' => $modelAlumni,
            'modelsJob' => (empty($modelsJob)) ? [new Job] : $modelsJob
        ]);
   }
}

为什么不删除?

这是我的校友模特

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "alumni".
 *
 * @property integer $id
 * @property string $name
 * @property integer $gender
 * @property integer $contact_number
 * @property string $year_graduated
 * @property string $qualification
 * @property integer $department_id
 * @property integer $specialization
 * @property string $email
 *
 * @property Job[] $jobs
 */
class Alumni extends \yii\db\ActiveRecord
{
     use \mootensai\relation\RelationTrait;
     public $organization;
     public $designation;
     public $location;
     public $current_status;
     public $joining_date;


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'alumni';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'gender',   'contact_number', 'year_graduated', 'qualification', 'department_id', 'specialization', 'email'], 'required'],
            [['name', 'organization', 'designation', 'location'], 'string'],
            [['gender',   'contact_number', 'department_id', 'specialization'], 'integer'],
            [['year_graduated'], 'safe'],
            [['qualification', 'email'], 'string', 'max' => 500],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'name' => Yii::t('app', 'Name'),
            'gender' => Yii::t('app', 'Gender'),

            'contact_number' => Yii::t('app', 'Contact Number'),
            'year_graduated' => Yii::t('app', 'Year Graduated'),
            'qualification' => Yii::t('app', 'Qualification'),
            'department_id' => Yii::t('app', 'Department ID'),
            'specialization' => Yii::t('app', 'Specialization'),
            'email' => Yii::t('app', 'Email'),

        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getJobs()
    {
        return $this->hasMany(Job::className(), ['alumni_id' => 'id']);
    }
}

这是我的工作模式

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "job".
 *
 * @property integer $job_id
 * @property string $organization
 * @property string $current_status
 * @property string $designation
 * @property string $joining_date
 * @property string $location
 * @property integer $alumni_id
 *
 * @property Alumni $alumni
 */
class Job extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'job';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['organization', 'current_status', 'designation', 'joining_date', 'location', 'alumni_id'], 'required'],
            [['organization'], 'string'],
            [['joining_date'], 'safe'],
            [['alumni_id'], 'integer'],
            [['current_status', 'designation'], 'string', 'max' => 300],
            [['location'], 'string', 'max' => 255],
            [['alumni_id'], 'exist', 'skipOnError' => true, 'targetClass' => Alumni::className(), 'targetAttribute' => ['alumni_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'job_id' => Yii::t('app', 'Job ID'),
            'organization' => Yii::t('app', 'Organization'),
            'current_status' => Yii::t('app', 'Current Status'),
            'designation' => Yii::t('app', 'Designation'),
            'joining_date' => Yii::t('app', 'Joining Date'),
            'location' => Yii::t('app', 'Location'),
            'alumni_id' => Yii::t('app', 'Alumni ID'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAlumni()
    {
        return $this->hasOne(Alumni::className(), ['id' => 'alumni_id']);
    }
}

1 个答案:

答案 0 :(得分:1)

有例子,请看actionUpdate

https://github.com/wbraganca/yii2-dynamicform

首先找到不在当前POST请求中的所有嵌套模型ID

        $oldIDs = ArrayHelper::map($modelsAddress, 'id', 'id');
        $modelsAddress = Model::createMultiple(Address::classname(), $modelsAddress);
        Model::loadMultiple($modelsAddress, Yii::$app->request->post());
        $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsAddress, 'id', 'id')));

然后在保存mainModel之后的事务中删除它:

if (! empty($deletedIDs)) 
{
   Address::deleteAll(['id' => $deletedIDs]);
}

这是一个例子,您必须为自己的解决方案实施。

我认为关系三元组中存在错误: https://github.com/mootensai/yii2-relation-trait/issues/27

所以上面是最好的解决方案