shaaz

Untitled

Jun 14th, 2011
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.76 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This is the model class for table "{{post}}".
  4.  *
  5.  * The followings are the available columns in table '{{post}}':
  6.  * @property integer $id
  7.  * @property string $title
  8.  * @property string $content
  9.  * @property string $tags
  10.  * @property integer $status
  11.  * @property integer $create_time
  12.  * @property integer $update_time
  13.  * @property integer $author_id
  14.  *
  15.  * The followings are the available model relations:
  16.  * @property Comment[] $comments
  17.  * @property User $author
  18.  */
  19. class Post extends CActiveRecord
  20. {
  21.  
  22.     const STATUS_DRAFT=1;
  23.     const STATUS_PUBLISHED=2;
  24.     const STATUS_ARCHIVED=3;
  25.     /**
  26.      * Returns the static model of the specified AR class.
  27.      * @return Post the static model class
  28.      */
  29.     public static function model($className=__CLASS__)
  30.     {
  31.         return parent::model($className);
  32.     }
  33.  
  34.     /**
  35.      * @return string the associated database table name
  36.      */
  37.     public function tableName()
  38.     {
  39.         return '{{post}}';
  40.     }
  41.  
  42.     /**
  43.      * @return array validation rules for model attributes.
  44.      */
  45.     public function rules()
  46.     {
  47.         // NOTE: you should only define rules for those attributes that
  48.         // will receive user inputs.
  49.         return array(
  50.             array('title, content, status', 'required'),
  51.             array('status, create_time, update_time, author_id', 'numerical', 'integerOnly'=>true),
  52.             array('title', 'length', 'max'=>128),
  53.             array('tags', 'safe'),
  54.             // The following rule is used by search().
  55.             // Please remove those attributes that should not be searched.
  56.             array('id, title, content, tags, status, create_time, update_time, author_id', 'safe', 'on'=>'search'),
  57.         );
  58.     }
  59.  
  60.     /**
  61.      * @return array relational rules.
  62.      */
  63.     public function relations()
  64.     {
  65.         // NOTE: you may need to adjust the relation name and the related
  66.         // class name for the relations automatically generated below.
  67.         return array(
  68.         'author' => array(self::BELONGS_TO, 'User', 'author_id'),
  69.         'comments' => array(self::HAS_MANY, 'Comment', 'post_id',
  70.             'condition'=>'comments.status='.Comment::STATUS_APPROVED,
  71.             'order'=>'comments.create_time DESC'),
  72.         'commentCount' => array(self::STAT, 'Comment', 'post_id',
  73.             'condition'=>'status='.Comment::STATUS_APPROVED),
  74.            
  75.     );
  76.     }
  77.  
  78.     /**
  79.      * @return array customized attribute labels (name=>label)
  80.      */
  81.     public function attributeLabels()
  82.     {
  83.         return array(
  84.             'id' => 'ID',
  85.             'title' => 'Title',
  86.             'content' => 'Content',
  87.             'tags' => 'Tags',
  88.             'status' => 'Status',
  89.             'create_time' => 'Create Time',
  90.             'update_time' => 'Update Time',
  91.             'author_id' => 'Author',
  92.         );
  93.     }
  94.    
  95.    
  96.     protected function beforeSave()
  97.       {
  98.             if(parent::beforeSave())
  99.             {
  100.                 if($this->isNewRecord)
  101.                 {
  102.                     $this->create_time=$this->update_time=date('H:i:s');
  103.                     $this->author_id=Yii::app()->user->id;
  104.                 }
  105.                 else
  106.                     $this->update_time=date('H:i:s');
  107.                 return true;
  108.             }
  109.             else
  110.                 return false;
  111.      }
  112.  
  113.     /**
  114.      * Retrieves a list of models based on the current search/filter conditions.
  115.      * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
  116.      */
  117.     public function search()
  118.     {
  119.         // Warning: Please modify the following code to remove attributes that
  120.         // should not be searched.
  121.  
  122.         $criteria=new CDbCriteria;
  123.  
  124.         $criteria->compare('id',$this->id);
  125.         $criteria->compare('title',$this->title,true);
  126.         $criteria->compare('content',$this->content,true);
  127.         $criteria->compare('tags',$this->tags,true);
  128.         $criteria->compare('status',$this->status);
  129.         $criteria->compare('create_time',$this->create_time);
  130.         $criteria->compare('update_time',$this->update_time);
  131.         $criteria->compare('author_id',$this->author_id);
  132.  
  133.         return new CActiveDataProvider(get_class($this), array(
  134.             'criteria'=>$criteria,
  135.         ));
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment