public function afterSave($insert, $changedAttributes)
{
- if (isset($changedAttributes->address_region)) {
- CityStore::update(['region' => $changedAttributes->address_region], ['id' => $changedAttributes->store_id]);
- }
-
- if (isset($changedAttributes->address_city)) {
- CityStore::update(['city' => $changedAttributes->address_city], ['id' => $changedAttributes->store_id]);
- }
+ $fields = [
+ 'address_region' => 'region',
+ 'address_city' => 'city',
+ 'address_district' => 'district',
+ 'store_type' => 'type'
+ ];
- if (isset($changedAttributes->address_district)) {
- CityStore::update(['district' => $changedAttributes->address_district], ['id' => $changedAttributes->store_id]);
- }
+ foreach ($fields as $attribute => $field) {
+ $value = $insert ? $this->$attribute : ($changedAttributes[$attribute] ?? null);
- if (isset($changedAttributes->address_type)) {
- CityStore::update(['type' => $changedAttributes->address_type], ['id' => $changedAttributes->store_id]);
+ if (!empty($value)) {
+ CityStore::updateAll([$field => $value], ['id' => $this->store_id]);
+ }
}
}
- public function getStore() {
+ public function getStore()
+ {
return $this->hasOne(CityStore::class, ['id' => 'store_id']);
}
- public function getUpdatedBy() {
+ public function getUpdatedBy()
+ {
return $this->hasOne(Admin::class, ['id' => 'updated_by']);
}
- public function getCreatedBy() {
+ public function getCreatedBy()
+ {
return $this->hasOne(Admin::class, ['id' => 'created_by']);
}
- public function getTerritorialManager() {
+ public function getTerritorialManager()
+ {
return $this->hasOne(Admin::class, ['id' => 'territorial_manager']);
}
- public function getBushChefFlorist() {
+ public function getBushChefFlorist()
+ {
return $this->hasOne(Admin::class, ['id' => 'bush_chef_florist']);
}
- public function getMatrixType() {
+ public function getMatrixType()
+ {
return $this->hasOne(MatrixType::class, ['id' => 'matrix_type']);
}
- public function getStoreType() {
+ public function getStoreType()
+ {
return $this->hasOne(StoreType::class, ['id' => 'store_type']);
}
- public function getAddressRegion() {
+ public function getAddressRegion()
+ {
return $this->hasOne(StoreCityList::class, ['id' => 'address_region']);
}
- public function getAddressDistrict() {
+ public function getAddressDistrict()
+ {
return $this->hasOne(StoreCityList::class, ['id' => 'address_district']);
}
- public function getAddressCity() {
+ public function getAddressCity()
+ {
return $this->hasOne(StoreCityList::class, ['id' => 'address_city']);
}
}