2017-04-24 17 views
6

Dựa trên https://github.com/wbraganca/yii2-dynamicform/wiki/Dynamic-Forms-With-Yii2-relation-trait-(VERY-EASY), tôi đang cố gắng triển khai các biểu mẫu động.Tạo hoạt động hoàn hảo, nhưng trong biểu mẫu Cập nhật, nếu tôi xóa bất kỳ phần tử biểu mẫu động nào, nó là không bị xóa, nhưng nếu tôi thêm trong hành động Cập nhật, nó sẽ được lưu lại. Đây là mã cập nhật của tôiYii2 wbraganca-dynamicform updation sử dụng yii2-quan hệ-đặc điểm không xóa các mục

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 
     ]); 
    } 
} 

Tại sao nó không xóa?

Đây là mô hình Alumni tôi

<?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']); 
    } 
} 

Đây là của tôi Mẫu Job

<?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']); 
    } 
} 
+0

Bạn cần phải làm điều này một mình. Nếu loadAll và saveAll được thực hiện, xóa các mô hình cần xóa. Xem https://wbraganca.com/yii2extensions/dynamicform-demo1/source-code thành actionUpdate() (tìm kiếm * deletedIDs *). Ở đó bạn thấy việc xóa mà bạn phải thực hiện cho giải pháp của riêng bạn. Xem xét sử dụng giao dịch. – robsch

+0

https://github.com/wbraganca/yii2-dynamicform/wiki/Dynamic-Forms-With-Yii2-relation-trait-(VERY-EASY) sử dụng cách tiếp cận khác. Nó sử dụng https://github.com/mootensai/yii2-relation-trait, mà họ nói là dễ dàng hơn – user7282

+0

Bạn nói đúng. Nhưng tôi đã không sử dụng những libs vì vậy tôi không thể giúp bạn. Bạn có thể gỡ lỗi RelationTrait.php. – robsch

Trả lời

1

Có dụ, nhìn vào actionUpdate

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

đầu tiên tìm Tất cả các model id lồng nhau không có trong yêu cầu POST đồng thời

 $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'))); 

sau đó trong giao dịch sau khi tiết kiệm mainModel xóa nó:

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

Đây là ví dụ, bạn phải thực hiện cho giải pháp của riêng bạn.

Tôi nghĩ rằng có lỗi trong mối quan hệ-triat: https://github.com/mootensai/yii2-relation-trait/issues/27

Vì vậy, trên là giải pháp tốt nhất

+0

Tôi đã thử đề xuất của bạn. nhưng yếu tố động không giữ lại dữ liệu về lỗi xác thực – user7282

Các vấn đề liên quan