demo
参考资料:
http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html
控制器:
PHP Code复制内容到剪贴板
- public function beforeAction($action)
- {
- if (in_array($action->id, ["material", "participant", "survey", "survey-detail", "schedule", "schedule-detail", "forum", "sign-manage", "weather", "life"])) {
- $planId = \Yii::$app->request->get("planId");
- if (!$planId) {
- return $this->render('/cnooc/nopic');
- }
- $data = ['plan_id' => $planId, 'unique_code' => \Yii::$app->user->getId()];
- $role = $this->getParticipantRole($data);
- /* 检查是否为参与者 */
- if (!$role) {
- return $this->render('/cnooc/nopic', [
- "role" => $role
- ]);
- }
- $this->role = $role;
- $this->appInfo = [
- 'corp_id' => \Yii::$app->config->get("CORP_ID"),
- 'secret' => \Yii::$app->config->get("SECRET_KEY"),
- 'agent_id' => \Yii::$app->config->get("WEIXIN_TRAINING")['agentId'],
- ];
- }
- return parent::beforeAction($action);
- }
模型:
PHP Code复制内容到剪贴板
- public function beforeSave($insert)
- {
- if (parent::beforeSave($insert)) {
- $this->template = serialize($this->template);
- if ($insert == true) {
- $this->used = BooleanEnum::FLASE;
- return true;
- }
- return true;
- } else {
- return false;
- }
- }
PHP Code复制内容到剪贴板
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- if ($insert) {
- $article = $this->article;
- Yii::$app->notify->category('reward')
- ->from($this->user_id)
- ->to($article->user_id)
- ->extra(['article_title' => Html::a($article->title, ['/article/view', 'id' => $article->id]), 'money' => $this->money, 'comment' => $this->comment])
- ->send();
- }
- }
PHP Code复制内容到剪贴板
- public function beforeDelete()
- {
- if (!parent::beforeDelete()) {
- return false;
- }
- // ...custom code here...
- return true;
- }
PHP Code复制内容到剪贴板
- public function afterDelete() {
- Yii::$app->db->createCommand("DELETE FROM {{%banner_image}} WHERE banner_id=".$this->cacheId)->execute();
- parent::afterDelete();
- }
PHP Code复制内容到剪贴板
- public function afterFind()
- {
- parent::afterFind();
- $this->template = unserialize($this->template);
- }