1 | <?php |
||
20 | class Comments extends CiiModel |
||
|
|||
21 | { |
||
22 | /** |
||
23 | * API Attribute for retrieving the counts |
||
24 | * @var int |
||
25 | */ |
||
26 | public $count = 0; |
||
27 | |||
28 | /** |
||
29 | * AfterSave IsNewRecord |
||
30 | * @var boolean |
||
31 | */ |
||
32 | private $_isNewRecord = false; |
||
33 | |||
34 | /** |
||
35 | * Returns the static model of the specified AR class. |
||
36 | * @param string $className active record class name. |
||
37 | * @return Comments the static model class |
||
38 | */ |
||
39 | public static function model($className=__CLASS__) |
||
43 | |||
44 | /** |
||
45 | * @return string the associated database table name |
||
46 | */ |
||
47 | public function tableName() |
||
51 | |||
52 | /** |
||
53 | * @return array validation rules for model attributes. |
||
54 | */ |
||
55 | public function rules() |
||
56 | { |
||
57 | // NOTE: you should only define rules for those attributes that |
||
58 | // will receive user inputs. |
||
59 | return array( |
||
60 | array('content_id, author_id, comment', 'required'), |
||
61 | array('content_id, author_id', 'numerical', 'integerOnly'=>true), |
||
62 | // The following rule is used by search(). |
||
63 | array('id, content_id, author_id, comment, created, updated', 'safe', 'on'=>'search'), |
||
64 | ); |
||
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 | 'content' => array(self::BELONGS_TO, 'Content', 'content_id'), |
||
76 | 'author' => array(self::BELONGS_TO, 'Users', 'author_id'), |
||
77 | ); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return array customized attribute labels (name=>label) |
||
82 | */ |
||
83 | public function attributeLabels() |
||
94 | |||
95 | /** |
||
96 | * Retrieves a list of models based on the current search/filter conditions. |
||
97 | * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. |
||
98 | */ |
||
99 | public function search() |
||
114 | |||
115 | /** |
||
116 | * Returns the API attributes for the model |
||
117 | * @return array |
||
118 | */ |
||
119 | public function getApiAttributes($params = array(), $relations = false) |
||
120 | { |
||
121 | $data = parent::getApiAttributes($params, $relations); |
||
122 | $user = Users::model()->findByPk($this->author_id); |
||
123 | $attributes = $user->getApiAttributes(); |
||
124 | $data['user'] = array( |
||
125 | 'firstName' => $attributes['firstName'], |
||
126 | 'lastName' => $attributes['lastName'], |
||
127 | 'username' => $attributes['username'], |
||
128 | ); |
||
129 | |||
130 | $content = Content::model()->findByPk($data['content_id']); |
||
131 | $data['content'] = array( |
||
132 | 'id' => $data['content_id'], |
||
133 | 'title' => $content->title, |
||
134 | 'slug' => $content->slug |
||
135 | ); |
||
136 | |||
137 | // If this user cannot comment without approval |
||
138 | if ($user->getReputation() < 100) |
||
139 | $data['banned_comment'] = true; |
||
140 | |||
141 | return $data; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Set the created and updated records |
||
146 | * @see CiiModel::beforeSave(); |
||
147 | */ |
||
148 | public function beforeSave() |
||
149 | { |
||
150 | if ($this->isNewRecord) |
||
151 | $this->_isNewRecord = true; |
||
152 | |||
153 | if (Content::model()->findByPk($this->content_id)->commentable) |
||
154 | return parent::beforeSave(); |
||
155 | else |
||
156 | return false; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * After a new comment is posted, set the reputation += 10 |
||
161 | * @see parent::afterSave(); |
||
162 | */ |
||
163 | public function afterSave() |
||
173 | } |
||
174 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.