3
class Post extends CActiveRecord
6
* The followings are the available columns in table 'tbl_post':
11
* @var integer $status
12
* @var integer $create_time
13
* @var integer $update_time
14
* @var integer $author_id
17
const STATUS_PUBLISHED=2;
18
const STATUS_ARCHIVED=3;
23
* Returns the static model of the specified AR class.
24
* @return CActiveRecord the static model class
26
public static function model($className=__CLASS__)
28
return parent::model($className);
32
* @return string the associated database table name
34
public function tableName()
40
* @return array validation rules for model attributes.
42
public function rules()
44
// NOTE: you should only define rules for those attributes that
45
// will receive user inputs.
47
array('title, content, status', 'required'),
48
array('status', 'in', 'range'=>array(1,2,3)),
49
array('title', 'length', 'max'=>128),
50
array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),
51
array('tags', 'normalizeTags'),
53
array('title, status', 'safe', 'on'=>'search'),
58
* @return array relational rules.
60
public function relations()
62
// NOTE: you may need to adjust the relation name and the related
63
// class name for the relations automatically generated below.
65
'author' => array(self::BELONGS_TO, 'User', 'author_id'),
66
'comments' => array(self::HAS_MANY, 'Comment', 'post_id', 'condition'=>'comments.status='.Comment::STATUS_APPROVED, 'order'=>'comments.create_time DESC'),
67
'commentCount' => array(self::STAT, 'Comment', 'post_id', 'condition'=>'status='.Comment::STATUS_APPROVED),
72
* @return array customized attribute labels (name=>label)
74
public function attributeLabels()
79
'content' => 'Content',
82
'create_time' => 'Create Time',
83
'update_time' => 'Update Time',
84
'author_id' => 'Author',
89
* @return string the URL that shows the detail of the post
91
public function getUrl()
93
return Yii::app()->createUrl('post/view', array(
95
'title'=>$this->title,
100
* @return array a list of links that point to the post list filtered by every tag of this post
102
public function getTagLinks()
105
foreach(Tag::string2array($this->tags) as $tag)
106
$links[]=CHtml::link(CHtml::encode($tag), array('post/index', 'tag'=>$tag));
111
* Normalizes the user-entered tags.
113
public function normalizeTags($attribute,$params)
115
$this->tags=Tag::array2string(array_unique(Tag::string2array($this->tags)));
119
* Adds a new comment to this post.
120
* This method will set status and post_id of the comment accordingly.
121
* @param Comment the comment to be added
122
* @return boolean whether the comment is saved successfully
124
public function addComment($comment)
126
if(Yii::app()->params['commentNeedApproval'])
127
$comment->status=Comment::STATUS_PENDING;
129
$comment->status=Comment::STATUS_APPROVED;
130
$comment->post_id=$this->id;
131
return $comment->save();
135
* This is invoked when a record is populated with data from a find() call.
137
protected function afterFind()
140
$this->_oldTags=$this->tags;
144
* This is invoked before the record is saved.
145
* @return boolean whether the record should be saved.
147
protected function beforeSave()
149
if(parent::beforeSave())
151
if($this->isNewRecord)
153
$this->create_time=$this->update_time=time();
154
$this->author_id=Yii::app()->user->id;
157
$this->update_time=time();
165
* This is invoked after the record is saved.
167
protected function afterSave()
170
Tag::model()->updateFrequency($this->_oldTags, $this->tags);
174
* This is invoked after the record is deleted.
176
protected function afterDelete()
178
parent::afterDelete();
179
Comment::model()->deleteAll('post_id='.$this->id);
180
Tag::model()->updateFrequency($this->tags, '');
184
* Retrieves the list of posts based on the current search/filter conditions.
185
* @return CActiveDataProvider the data provider that can return the needed posts.
187
public function search()
189
$criteria=new CDbCriteria;
191
$criteria->compare('title',$this->title,true);
193
$criteria->compare('status',$this->status);
195
return new CActiveDataProvider('Post', array(
196
'criteria'=>$criteria,
198
'defaultOrder'=>'status, update_time DESC',
b'\\ No newline at end of file'