guild/backend/modules/company/models/Company.php

65 lines
1.8 KiB
PHP
Raw Normal View History

2018-10-11 17:24:47 +03:00
<?php
namespace backend\modules\company\models;
use common\models\FieldsValue;
2019-06-26 17:22:10 +03:00
use common\models\FieldsValueNew;
2018-10-11 17:24:47 +03:00
class Company extends \common\models\Company
{
public $fields;
public function init()
{
parent::init();
2019-06-26 17:22:10 +03:00
$fieldValue = FieldsValueNew::find()
->where(
2018-10-11 17:24:47 +03:00
[
2019-06-26 17:22:10 +03:00
'item_id' => \Yii::$app->request->get('id'),
'item_type' => FieldsValueNew::TYPE_COMPANY,
2018-10-11 17:24:47 +03:00
])
->all();
$array = [];
if(!empty($fieldValue)){
foreach ($fieldValue as $item){
2019-06-26 17:22:10 +03:00
array_push($array,
['field_id' => $item->field_id,
'value' => $item->value,
'order' => $item->order,
'field_name' => $item->field->name]);
2018-10-11 17:24:47 +03:00
}
$this->fields = $array;
}
else{
$this->fields = [
[
'field_id' => null,
'value' => null,
'order' => null,
2019-06-26 17:22:10 +03:00
'field_name' => null,
2018-10-11 17:24:47 +03:00
],
];
}
}
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('Company');
FieldsValue::deleteAll(['company_id' => $this->id]);
foreach ( $post['fields'] as $item) {
2019-06-26 17:22:10 +03:00
$fildsValue = new FieldsValueNew();
2018-10-11 17:24:47 +03:00
$fildsValue->field_id = $item['field_id'];
$fildsValue->value = $item['value'];
$fildsValue->order = $item['order'];
2019-06-26 17:22:10 +03:00
$fildsValue->item_id = $this->id;
$fildsValue->item_type = FieldsValueNew::TYPE_COMPANY;
2018-10-11 17:24:47 +03:00
$fildsValue->save();
}
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
}