belongsTo(Community::class, 'community_id'); } public function designer() { return $this->belongsTo(Employee::class, 'designer_id', 'id'); } public function decostyle() { return $this->belongsTo(Decostyle::class, 'style_id'); } public function housetype() { return $this->morphTo('housetype', [ 'building_housetype' => 'app\model\BuildingHousetype', 'housetype' => 'app\model\Housetype' ]); } public function employee() { return $this->belongsTo(Employee::class, 'employee_id', 'id'); } public static function onBeforeUpdate($case){ $old = static::find($case->id); if($old->housetype_type == 'building_housetype' && ($case->housetype_type == 'housetype' || $old->housetype_id != $case->housetype_id)) { BuildingMaterialCase::where([ 'root_id'=>$old->root_id, 'housetype_id'=>$old->housetype_id, 'material_case_id'=>$old->id ])->delete(); } } public static function onAfterWrite($case) { if ($case->housetype_type == 'building_housetype') { $building_id = BuildingHousetype::where(['id' => $case->housetype_id])->value('building_id'); BuildingMaterialCase::create([ 'building_id' => $building_id, 'housetype_id' => $case->housetype_id, 'material_case_id' => $case->id, 'root_id' => $case->root_id ]); } } }