TimestampBehavior::class, 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'], ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'] ] ] ]; } /** * @inheritdoc */ public function rules() { return [ [['store_id', 'is_delete', 'id', 'parent_id', 'md_id', 'is_disabled', 'table_num', 'wx_status', 'ali_status'], 'integer'], [['url_path', 'qrcode_url', 'mini_path'], 'string'], [['created_at', 'updated_at'], 'safe'] ]; } public function beforeSave($insert) { if (parent::beforeSave($insert)) { // if ($this->parent_id) { // $parent_qrcode = MdFoodsQrcode::findOne(['id' => $this->parent_id, 'is_delete' => 0]); // if ($parent_qrcode && $parent_qrcode->parent_id) { // $parent_qrcode_ = MdFoodsQrcode::findOne(['id' => $parent_qrcode->parent_id, 'is_delete' => 0]); // if ($parent_qrcode_) { // $this->is_disabled = 1; // } // } // } return true; } return false; } public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); //$this->saveChild($this->id, $insert); } public function saveChild($id, $insert) { if ($insert) { if ($this->parent_id) { $parent = self::findOne(['id' => $this->parent_id, 'is_delete' => 0]); $self = self::findOne(['id' => $id]); if ($parent) { $self->store_id = $parent->store_id; $self->md_id = $parent->md_id; $self->is_delete = $parent->is_delete; $self->save(); } } } else { $parent = self::findOne(['id' => $id]); $qrcode_list = self::findAll(['parent_id' => $id, 'is_delete' => 0]); foreach ($qrcode_list as $qrcode) { $qrcode->store_id = $parent->store_id; $qrcode->md_id = $parent->md_id; $qrcode->is_delete = $parent->is_delete; $qrcode->save(); } } } public function getChildren() { return $this->hasMany(self::class, ['parent_id' => 'id'])->andWhere(['is_delete' => 0]); } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => '', 'store_id' => '店铺id', 'url_path' => '地址', 'qrcode_url' => '二维码保存地址', 'parent_id' => '父级id', 'table_num' => '桌号', 'mini_path' => '小程序路径', 'is_delete' => '', 'created_at' => '', 'updated_at' => '', ]; } // public function getShopPic() // { // return $this->hasMany(ShopPic::className(), ['shop_id'=>'id'])->where(['is_delete'=>0]); // } }