public function beforeSave($insert) { if ($this->isNewRecord) { $this->ip = XUtils::getClientIP(); $this->create_time = time(); $this->user_agent = htmlspecialchars(Yii::$app->request->getUserAgent()); } else { $this->update_time = time(); $this->ext = serialize(['ip' => XUtils::getClientIP(), 'username' => Yii::$app->user->isGuest ? 'Guest' : Yii::$app->user->identity->username]); } return parent::beforeSave($insert); }
/** * 删除后将自分类提升至自己的父分类 */ public function afterDelete() { parent::afterDelete(); self::updateAll(['pid' => $this->pid], ['pid' => $this->id]); }
/** * 自动填写create_time、post_time、update_time * 新 Post 自动填写作者ID和作者的昵称 * 自动填写最终修改人(name和id) * @see CActiveRecord::beforeSave() * @param bool $insert * @return bool */ public function beforeSave($insert) { if (!parent::beforeSave($insert)) { return false; } $this->comment_count = intval($this->comment_count); //创建新Post if ($insert) { $this->create_time = $this->update_time = time(); $this->ext_info = null; } //编辑状态记录信息 if (in_array($this->scenario, [self::SCENARIO_EDIT, self::SCENARIO_MANAGE])) { $this->ext_info = is_array($this->ext_info) ? serialize($this->ext_info) : null; $this->update_time = time(); } //修改保存状态,处理发布时间 if ($this->status != self::STATUS_PUBLISHED && $this->status != self::STATUS_HIDDEN) { $this->post_time = null; } else { if (empty($this->post_time)) { $this->post_time = time(); } elseif (!is_numeric($this->post_time)) { $this->post_time = strtotime($this->post_time); } } if ($this->status != self::STATUS_HIDDEN) { $this->password = null; } //title安全修正,并生成文章URL别名 $this->title = htmlspecialchars($this->title); if (!$this->alias) { $this->alias = $this->title; } $this->alias = str_replace([' ', '%'], ['-'], trim($this->alias)); $this->alias = strip_tags($this->alias); $this->alias = htmlspecialchars($this->alias); //alias唯一性校验 if (self::find()->where(['alias' => $this->alias])->exists()) { $this->addError('alias', '访问别名不能重复!'); return false; } //生成并处理excerpt if (!$this->excerpt) { if ($this->status == self::STATUS_PUBLISHED) { $this->excerpt = strip_tags($this->excerpt, '<p><ul><li><strong>'); $this->excerpt = XUtils::strimwidthWithTag($this->content, 0, 350, '...'); } } $this->excerpt = str_replace(['<p></p>', '<p><br /></p>'], '', $this->excerpt); //处理封面图片 if ($this->cover) { //TODO 验证cover } else { $this->cover = $this->getCoverImage(); } //处理并生成tags $tags_array = array_unique(explode(',', str_replace([' ', ','], ',', $this->tags))); foreach ($tags_array as $key => $tag) { if (preg_match('/^[0-9a-zA-Z_\\x{4e00}-\\x{9fa5}]+$/u', $tag)) { $tags_array[$key] = $tag; } else { unset($tags_array[$key]); } } $this->tags = implode(',', $tags_array); return true; }
public function afterDelete() { parent::afterDelete(); if ($this->pid == 0) { self::deleteAll(['pid' => $this->id]); } }