Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This is the model class for table "products".
- *
- * The followings are the available columns in table 'products':
- * @property integer $id
- * @property string $name
- * @property integer $category_id
- * @property string $image
- * @property string $thumbnail
- */
- class Products extends CActiveRecord
- {
- private $_categoryName;
- public function getCategoryName()
- {
- if ($this->category != null) {
- $this->_categoryName = $this->category->name;
- }
- return $this->_categoryName;
- }
- public
- function setCategoryName($value)
- {
- $this->_categoryName = $value;
- }
- /**
- * @return string the associated database table name
- */
- public
- function tableName()
- {
- return 'products';
- }
- /**
- * @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('name', 'required'),
- array('category_id', 'numerical', 'integerOnly' => true),
- array('name, image, thumbnail', 'length', 'max' => 255),
- array('id, name, categoryName', 'safe', 'on' => 'search'),
- );
- }
- /**
- * @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(
- 'category' => array(self::BELONGS_TO, 'Categories', 'category_id')
- );
- }
- /**
- * @return array customized attribute labels (name=>label)
- */
- public
- function attributeLabels()
- {
- return array(
- 'id' => 'ID',
- 'name' => 'Name',
- 'categoryName' => 'Category',
- 'image' => 'Image',
- 'thumbnail' => 'Thumbnail',
- );
- }
- /**
- * Retrieves a list of models based on the current search/filter conditions.
- *
- * Typical usecase:
- * - Initialize the model fields with values from filter form.
- * - Execute this method to get CActiveDataProvider instance which will filter
- * models according to data in model fields.
- * - Pass data provider to CGridView, CListView or any similar widget.
- *
- * @return CActiveDataProvider the data provider that can return the models
- * based on the search/filter conditions.
- */
- public
- function search()
- {
- $sort = new CSort();
- $sort->attributes = array(
- 'defaultOrder'=>'t.id DESC',
- 'id'=>array(
- 'asc'=>'t.id',
- 'desc'=>'t.id desc',
- ),
- 'name'=>array(
- 'asc'=>'t.name',
- 'desc'=>'t.name desc',
- ),
- 'categoryName'=>array(
- 'asc'=>'category.name',
- 'desc'=>'category.name desc',
- ),
- );
- $criteria = new CDbCriteria;
- $criteria->with = "category";
- $criteria->compare('id', $this->id);
- $criteria->compare('t.name', $this->name, true);
- $criteria->compare('category.name', $this->_categoryName, true);
- return new CActiveDataProvider($this, array(
- 'criteria' => $criteria,
- 'sort' => $sort
- ));
- }
- /**
- * Returns the static model of the specified AR class.
- * Please note that you should have this exact method in all your CActiveRecord descendants!
- * @param string $className active record class name.
- * @return Products the static model class
- */
- public
- static function model($className = __CLASS__)
- {
- return parent::model($className);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement