<?php namespace backend\modules\project\models; use common\classes\Debug; use common\models\FieldsValue; use common\models\FieldsValueNew; use common\models\ProjectUser; use yii\helpers\ArrayHelper; class Project extends \common\models\Project { public $fields; public $user; public function init() { parent::init(); $fieldValue = FieldsValueNew::find() ->where( [ 'item_id' => \Yii::$app->request->get('id'), 'item_type' => FieldsValueNew::TYPE_PROJECT, ]) ->all(); $array = []; if (!empty($fieldValue)) { foreach ($fieldValue as $item) { array_push($array, [ 'field_id' => $item->field_id, 'value' => $item->value, 'order' => $item->order, 'field_name' => $item->field->name]); } $this->fields = $array; } else { $this->fields = [ [ 'field_id' => null, 'value' => null, 'order' => null, 'field_name' => null, ], ]; } $user = ArrayHelper::getColumn(ProjectUser::find()->where(['project_id' => \Yii::$app->request->get('id')])->all(), 'card_id'); if (!empty($user)) { $this->user = $user; } } public function afterSave($insert, $changedAttributes) { $post = \Yii::$app->request->post('Project'); FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROJECT]); foreach ($post['fields'] as $item) { $fildsValue = new FieldsValueNew(); $fildsValue->field_id = $item['field_id']; $fildsValue->value = $item['value']; $fildsValue->order = $item['order']; $fildsValue->item_id = $this->id; $fildsValue->item_type = FieldsValueNew::TYPE_PROJECT; $fildsValue->save(); } ProjectUser::deleteAll(['project_id' => $this->id]); if($post['user']){ foreach ($post['user'] as $item) { $prUser = new ProjectUser(); $prUser->project_id = $this->id; $prUser->card_id = $item; $prUser->save(); } } parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub } }