Advertisement
Guest User

Untitled

a guest
May 30th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.89 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This is the model class for table "products".
  5.  *
  6.  * The followings are the available columns in table 'products':
  7.  * @property integer $id
  8.  * @property string $name
  9.  * @property integer $category_id
  10.  * @property string $image
  11.  * @property string $thumbnail
  12.  */
  13. class Products extends CActiveRecord
  14. {
  15.  
  16.  
  17.     private $_categoryName;
  18.  
  19.     public function getCategoryName()
  20.     {
  21.         if ($this->category != null) {
  22.             $this->_categoryName = $this->category->name;
  23.         }
  24.  
  25.         return $this->_categoryName;
  26.  
  27.     }
  28.  
  29.  
  30.     public
  31.     function setCategoryName($value)
  32.     {
  33.         $this->_categoryName = $value;
  34.     }
  35.  
  36.  
  37.     /**
  38.      * @return string the associated database table name
  39.      */
  40.     public
  41.     function tableName()
  42.     {
  43.         return 'products';
  44.     }
  45.  
  46.     /**
  47.      * @return array validation rules for model attributes.
  48.      */
  49.     public
  50.     function rules()
  51.     {
  52.         // NOTE: you should only define rules for those attributes that
  53.         // will receive user inputs.
  54.         return array(
  55.             array('name', 'required'),
  56.             array('category_id', 'numerical', 'integerOnly' => true),
  57.             array('name, image, thumbnail', 'length', 'max' => 255),
  58.             array('id, name, categoryName', 'safe', 'on' => 'search'),
  59.         );
  60.     }
  61.  
  62.     /**
  63.      * @return array relational rules.
  64.      */
  65.     public
  66.     function relations()
  67.     {
  68.         // NOTE: you may need to adjust the relation name and the related
  69.         // class name for the relations automatically generated below.
  70.         return array(
  71.             'category' => array(self::BELONGS_TO, 'Categories', 'category_id')
  72.         );
  73.     }
  74.  
  75.     /**
  76.      * @return array customized attribute labels (name=>label)
  77.      */
  78.     public
  79.     function attributeLabels()
  80.     {
  81.         return array(
  82.             'id' => 'ID',
  83.             'name' => 'Name',
  84.             'categoryName' => 'Category',
  85.             'image' => 'Image',
  86.             'thumbnail' => 'Thumbnail',
  87.         );
  88.     }
  89.  
  90.     /**
  91.      * Retrieves a list of models based on the current search/filter conditions.
  92.      *
  93.      * Typical usecase:
  94.      * - Initialize the model fields with values from filter form.
  95.      * - Execute this method to get CActiveDataProvider instance which will filter
  96.      * models according to data in model fields.
  97.      * - Pass data provider to CGridView, CListView or any similar widget.
  98.      *
  99.      * @return CActiveDataProvider the data provider that can return the models
  100.      * based on the search/filter conditions.
  101.      */
  102.  
  103.     public
  104.     function search()
  105.     {
  106.         $sort = new CSort();
  107.         $sort->attributes = array(
  108.             'defaultOrder'=>'t.id DESC',
  109.             'id'=>array(
  110.                 'asc'=>'t.id',
  111.                 'desc'=>'t.id desc',
  112.             ),
  113.             'name'=>array(
  114.                 'asc'=>'t.name',
  115.                 'desc'=>'t.name desc',
  116.             ),
  117.  
  118.             'categoryName'=>array(
  119.                 'asc'=>'category.name',
  120.                 'desc'=>'category.name desc',
  121.             ),
  122.         );
  123.         $criteria = new CDbCriteria;
  124.         $criteria->with = "category";
  125.         $criteria->compare('id', $this->id);
  126.         $criteria->compare('t.name', $this->name, true);
  127.         $criteria->compare('category.name', $this->_categoryName, true);
  128.  
  129.         return new CActiveDataProvider($this, array(
  130.             'criteria' => $criteria,
  131.             'sort' => $sort
  132.         ));
  133.     }
  134.  
  135.  
  136.     /**
  137.      * Returns the static model of the specified AR class.
  138.      * Please note that you should have this exact method in all your CActiveRecord descendants!
  139.      * @param string $className active record class name.
  140.      * @return Products the static model class
  141.      */
  142.     public
  143.     static function model($className = __CLASS__)
  144.     {
  145.         return parent::model($className);
  146.     }
  147.  
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement