Ok, Here we go
Model News
<?php
/**
* This is the model class for table "news".
*
* The followings are the available columns in table 'news':
* @property string $id
* @property integer $section_id
* @property string $title_ru
* @property string $title_en
* @property string $text_ru
* @property string $text_en
* @property integer $likes
*/
class News extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return News the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'news';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('section_id', 'required'),
array('title_en', 'oneOfTwoRequered', 'field'=>'title_ru'),
array('title_ru', 'oneOfTwoRequered', 'field'=>'title_en'),
array('title_en', 'requeredByAnother', 'field'=>'text_en'),
array('text_en', 'requeredByAnother', 'field'=>'title_en'),
array('title_ru', 'requeredByAnother', 'field'=>'text_ru'),
array('text_ru', 'requeredByAnother', 'field'=>'title_ru'),
array('section_id, likes', 'numerical', 'integerOnly'=>true),
array('title_ru, title_en', 'length', 'max'=>255),
array('id, section, section_id, title_ru, title_en, text_ru, text_en, likes, popularity, create_time, isDraft', 'safe'),
);
}
public function requeredByAnother ($attribute, $params)
{
if((!empty($this->$params['field']))&&(empty($this->$attribute)))
{
$this->addError($attribute, Yii::t('news', 'This field is required, if {field} is not empty', array('{field}'=>News::getAttributeLabel($params['field']))));
return false;
}
return true;
}
public function oneOfTwoRequered ($attribute, $params)
{
if((empty($this->$params['field']))&&(empty($this->$attribute)))
{
$this->addError($attribute, Yii::t('news', 'At least 1 of the field must be filled up properly'));
return false;
}
return true;
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'section'=>array(self::BELONGS_TO, 'Sections', 'section_id'),
'links'=>array(self::HAS_MANY, 'Links', 'news_id'),
'newstags'=>array(self::HAS_MANY, 'NewsTags'.ucfirst((Yii::app()->language=='ru')?('ru')<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />'en')), 'news_id'),
'newstags_en'=>array(self::HAS_MANY, 'NewsTagsEn', 'news_id'),
'newstags_ru'=>array(self::HAS_MANY, 'NewsTagsRu', 'news_id'),
'tags'=>array(self::HAS_MANY, 'Tags'.ucfirst((Yii::app()->language=='ru')?('ru')<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />'en')), array('tags_id'=>'id'), 'through'=>'newstags'),
);
}
public function scopes()
{
return array(
'published'=>((Yii::app()->user->isGuest)?(array('condition'=>'isDraft=0')):(array())),
'recently'=>array(
'condition'=>'`title_'.((Yii::app()->language=='ru')?('ru')<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />'en')).'` <> ""',
'order'=>'create_time DESC, id DESC',
'with'=>array('links','newstags'=>array('news_id'=>'t.id'),'newstags.tags'),
),
'popular'=>array(
'condition'=>'`title_'.((Yii::app()->language=='ru')?('ru')<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />'en')).'` <> ""',
'order'=>'popularity DESC',
'with'=>array('links','newstags'=>array('news_id'=>'t.id'),'newstags.tags'),
),
'terrible'=>array('condition'=>'`section_id` = 1',),
'comedy'=>array('condition'=>'`section_id` = 2',),
'weird'=>array('condition'=>'`section_id` = 3',),
'russia'=>array('condition'=>'`section_id` = 5',),
'actual'=>array(
'condition'=>'`title_'.((Yii::app()->language=='ru')?('ru')<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />'en')).'` <> ""',
),
'wthrelations'=>array(
'with'=>array('links','tags'),
),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'section_id' => Yii::t('news', 'Section'),
'section' => Yii::t('news', 'Section'),
'create_time' => Yii::t('news', 'Create Time'),
'title_ru' => Yii::t('news', 'Title Russian'),
'title_en' => Yii::t('news', 'Title English'),
'title' => Yii::t('news', 'Title'),
'text_ru' => Yii::t('news', 'Text Russian'),
'text_en' => Yii::t('news', 'Text English'),
'tags_ru' => Yii::t('news', 'Tags Russian'),
'tags_en' => Yii::t('news', 'Tags English'),
'likes' => Yii::t('news', 'Likes'),
'isDraft' => Yii::t('news', 'Draft'),
'popularity' => Yii::t('news', 'Popularity'),
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->with=array('section');
$criteria->compare('t.id',$this->id,true);
$criteria->compare('section.id', $this->section,true);
$criteria->compare('t.section_id',$this->section_id);
$criteria->compare('t.title_ru',$this->title_ru,true);
$criteria->compare('t.title_en',$this->title_en,true);
$criteria->compare('t.text_ru',$this->text_ru,true);
$criteria->compare('t.text_en',$this->text_en,true);
$criteria->compare('t.isDraft',$this->isDraft,true);
$criteria->compare('t.likes',$this->likes);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
continue in next post