I used Gii’s CRUD generator and in general it worked well, but when I go to the “manage” page I can see the CGridView listing but searches do not work.
The model:
<?php
* The followings are the available columns in table 'shop_products':
* @property integer $id
* @property string $sku
* @property double $price
* @property integer $tax_rate_id
* @property integer $manufacturer_id
* @property string $photo
* @property string $url
* @property double $width
* @property double $length
* @property double $height
* @property double $weight
* @property integer $is_active
* @property integer $mstamp
* @property integer $cstamp
*/
class Product extends CActiveRecord
{
//...
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('tax_rate_id', 'required'),
array('tax_rate_id, manufacturer_id, is_active, mstamp, cstamp', 'numerical', 'integerOnly'=>true),
array('price, width, length, height, weight', 'numerical'),
array('sku', 'length', 'max'=>45),
array('photo', 'length', 'max'=>255),
array('url', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('sku, price, manufacturer_id, is_active', 'safe', 'on'=>'search'),
);
}
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(
'l10n' => array(self::HAS_ONE, 'ProductL10n', 'product_id', 'alias' => 'l10n', 'on' => 'l10n.lang_id="el"'),
'categories' => array(self::MANY_MANY, 'Category', 'shop_category_products(product_id, category_id)'),
'attributes' => array(self::HAS_MANY, 'ProductAttribute', 'product_id'),
'types' => array(self::HAS_MANY, 'ProductType', 'product_id'),
'taxRate' => array(self::BELONGS_TO, 'TaxRate', 'tax_rate_id'),
'manufacturer' => array(self::BELONGS_TO, 'Manufacturer', 'manufacturer_id'),
);
}
//...
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('is_active',$this->is_active);
$criteria->compare('sku',$this->sku,true);
$criteria->compare('price',$this->price);
$criteria->compare('manufacturer_id',$this->manufacturer_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
The view:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'product-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'is_active',
'photo',
array(
'header' => 'Product name',
'name' => 'id',
'value' => '$data->l10n->caption',
),
'sku',
'price',
array(
'header' => 'Manufacturer',
'name' => 'manufacturer_id',
'value' => '$data->manufacturer->l10n->caption',
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
I have printed the $_REQUEST and everything is ok, searching for price=100 shows $_REQUEST has key of “price” with value “100”, but $this->price is null as var_dump reports in the model’s search() method, all after inspecting the response when searching.