/protected/models/Content.php

https://bitbucket.org/coolhihi/ccms · PHP · 208 lines · 122 code · 14 blank · 72 comment · 3 complexity · 2469ee061d43d7d30483a1ea94026463 MD5 · raw file

  1. <?php
  2. /**
  3. * This is the model class for table "{{content}}".
  4. *
  5. * The followings are the available columns in table '{{content}}':
  6. * @property integer $id
  7. * @property integer $category_id
  8. * @property integer $weight
  9. * @property string $title
  10. * @property string $author
  11. * @property string $origin
  12. * @property string $attachment
  13. * @property string $image
  14. * @property string $flags
  15. * @property integer $allowcomment
  16. * @property string $summary
  17. * @property string $content
  18. * @property string $tags
  19. * @property integer $clicktimes
  20. * @property integer $create_time
  21. * @property integer $lastedit_time
  22. * @property integer $enable
  23. * @property integer $manager_id
  24. *
  25. * The followings are the available model relations:
  26. * @property Comment[] $comments
  27. * @property Category $category
  28. * @property Manager $manager
  29. */
  30. class Content extends CBaseModel
  31. {
  32. public $flagsArr=array();
  33. public $image_s;
  34. /**
  35. * Returns the static model of the specified AR class.
  36. * @return Content the static model class
  37. */
  38. public static function model($className=__CLASS__)
  39. {
  40. return parent::model($className);
  41. }
  42. /**
  43. * @return string the associated database table name
  44. */
  45. public function tableName()
  46. {
  47. return '{{content}}';
  48. }
  49. /**
  50. * @return array validation rules for model attributes.
  51. */
  52. public function rules()
  53. {
  54. // NOTE: you should only define rules for those attributes that
  55. // will receive user inputs.
  56. return array(
  57. array('category_id, weight, title, allowcomment, enable', 'required'),
  58. array('category_id, weight, allowcomment, clicktimes, create_time, manager_id, lastedit_time, enable', 'numerical', 'integerOnly'=>true),
  59. array('title, author, origin, attachment, image, flags, tags', 'length', 'max'=>200),
  60. array('category_id', 'numerical', 'min'=>1, 'tooSmall'=>'请选择栏目.'),
  61. array('content, summary, image_s, flagsArr', 'safe'),
  62. // The following rule is used by search().
  63. // Please remove those attributes that should not be searched.
  64. array('id, category_id, title, author, origin, flags, tags, manager_id, enable', 'safe', 'on'=>'search'),
  65. );
  66. }
  67. /**
  68. * @return array relational rules.
  69. */
  70. public function relations()
  71. {
  72. // NOTE: you may need to adjust the relation name and the related
  73. // class name for the relations automatically generated below.
  74. return array(
  75. 'comments' => array(self::HAS_MANY, 'Comment', 'content_id', 'order'=>'comments.id desc'),
  76. 'commentsCount' => array(self::STAT, 'Comment', 'content_id'),
  77. 'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
  78. 'manager' => array(self::BELONGS_TO, 'Manager', 'manager_id'),
  79. );
  80. }
  81. /**
  82. * @return array customized attribute labels (name=>label)
  83. */
  84. public function attributeLabels()
  85. {
  86. return array(
  87. 'id' => 'ID',
  88. 'category_id' => '所属栏目',
  89. 'weight' => '排序权重',
  90. 'title' => '标题',
  91. 'author' => '作者',
  92. 'origin' => '来源',
  93. 'attachment' => '附件',
  94. 'image' => '封面',
  95. 'flags' => '特性',
  96. 'flagsArr' => '特性',
  97. 'allowcomment' => '评论',
  98. 'summary' => '描述',
  99. 'content' => '内容',
  100. 'tags' => '标签',
  101. 'clicktimes' => '点击数',
  102. 'create_time' => '创建时间',
  103. 'lastedit_time' => '修改时间',
  104. 'manager_id' => '管理员ID',
  105. 'enable' => '可用',
  106. );
  107. }
  108. /**
  109. * Retrieves a list of models based on the current search/filter conditions.
  110. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
  111. */
  112. public function search()
  113. {
  114. // Warning: Please modify the following code to remove attributes that
  115. // should not be searched.
  116. $criteria=new CDbCriteria;
  117. $criteria->compare('id',$this->id);
  118. $criteria->compare('category_id',$this->category_id);
  119. $criteria->compare('title',$this->title,true);
  120. $criteria->compare('author',$this->author,true);
  121. $criteria->compare('origin',$this->origin,true);
  122. $criteria->compare('flags',$this->flags,true);
  123. $criteria->compare('tags',$this->tags,true);
  124. $criteria->compare('manager_id',$this->manager_id);
  125. $criteria->compare('enable',$this->enable);
  126. return new CActiveDataProvider($this, array(
  127. 'criteria'=>$criteria,
  128. 'pagination'=>array(
  129. 'pagesize'=>Yii::app()->user->getState('numPerPage'),
  130. 'currentPage'=>Yii::app()->user->getState('pageNum')-1,
  131. ),
  132. 'sort'=>array(
  133. 'defaultOrder'=>'create_time DESC', //设置默认排序是create_time倒序
  134. ),
  135. ));
  136. }
  137. /*
  138. * addClick 点击数加一
  139. */
  140. public function addClick(){
  141. //Yii::trace('a','cool.a');
  142. //以下花3.31秒
  143. //$this->updateByPk($this->id, array('clicktimes'=>$this->clicktimes+1));
  144. //Yii::trace('a','cool.b');
  145. //以下花5.9秒
  146. //$this->clicktimes+=1;
  147. //$this->update();
  148. //Yii::trace('a','cool.c');
  149. //以下花1.75秒,但@屏蔽不了错误,只能try
  150. try {
  151. Yii::app()->db->createCommand("update {{content}} set clicktimes=clicktimes+1 where id=".$this->id)->query();
  152. } catch (Exception $exc) {
  153. }
  154. //Yii::trace('a','cool.d');
  155. }
  156. /*
  157. * getFlags 返回特性的中文意义
  158. */
  159. public function getFlags(){
  160. $tempStr=$this->flags;
  161. $tempStr=str_replace('a', '头条', $tempStr);
  162. $tempStr=str_replace('b', '幻灯', $tempStr);
  163. $tempStr=str_replace('c', '推荐', $tempStr);
  164. return $tempStr;
  165. }
  166. public function beforeSave() {
  167. if(parent::beforeSave())
  168. {
  169. if($this->isNewRecord){
  170. $this->create_time=$this->lastedit_time=time();
  171. $this->clicktimes=0;
  172. $this->manager_id=Yii::app()->user->id;
  173. }
  174. else
  175. $this->lastedit_time=time();
  176. return true;
  177. }
  178. else
  179. return false;
  180. }
  181. public function afterSave(){
  182. parent::afterSave();
  183. if($this->isNewRecord)
  184. CFunc::writeLog("添加了文章 {".($this->title)."}");
  185. else
  186. CFunc::writeLog("修改了文章 {".($this->title)."}");
  187. return true;
  188. }
  189. public function afterDelete() {
  190. parent::afterDelete();
  191. CFunc::writeLog("删除了文章 {".($this->title)."}");
  192. return true;
  193. }
  194. }