Based on https://github.com/wbraganca/yii2-dynamicform/wiki/Dynamic-Forms-With-Yii2-relation-trait-(VERY-EASY) , I am trying to implement dynamic forms. The creation works fine, but in the "Update" form, if I delete any element of the dynamic form, it is not deleted, but if I add the "Update" action, it will be saved. This is my update code.
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
]);
}
}
Why doesn't he delete?
This is my graduate model.
<?php
namespace app\models;
use Yii;
class Alumni extends \yii\db\ActiveRecord
{
use \mootensai\relation\RelationTrait;
public $organization;
public $designation;
public $location;
public $current_status;
public $joining_date;
public static function tableName()
{
return 'alumni';
}
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],
];
}
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'),
];
}
public function getJobs()
{
return $this->hasMany(Job::className(), ['alumni_id' => 'id']);
}
}
Here is my work model
<?php
namespace app\models;
use Yii;
class Job extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'job';
}
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']],
];
}
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'),
];
}
public function getAlumni()
{
return $this->hasOne(Alumni::className(), ['id' => 'alumni_id']);
}
}