Thanks for your help. I used the process and it worked for me dislaying the related category and i can search using them but I can’t search by the attributes of the main model. It keeps trowing exceptional error. Below is my code.
Subcategory Model
<?php
/**
-
The followings are the available columns in table ‘{{subcategory}}’:
-
@property integer $id
-
@property string $title
-
@property string $description
-
@property integer $category_id
*/
class Subcategory extends CActiveRecord
{
public $category_search;
/**
* @return string the associated database table name
*/
public function tableName()
{
return '{{subcategory}}';
}
/**
* @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('title', 'required'),
array('category_id', 'numerical', 'integerOnly'=>true),
array('title', 'length', 'max'=>256),
array('description', 'length', 'max'=>2000),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, title, description, category_id, category_search', '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, 'Category', 'category_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'title' => 'Title',
'description' => 'Description',
'category_id' => 'Category',
);
}
/**
* 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()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array( 'category' );
$criteria->compare('id',$this->id);
$criteria->compare('title',$this->title,true);
$criteria->compare('description',$this->description,true);
$criteria->compare('category_id',$this->category_id);
$criteria->compare( 'category.title', $this->category_search, true );
return new CActiveDataProvider( 'Subcategory', array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'category_search'=>array(
'asc'=>'category.title',
'desc'=>'category.title DESC',
),
'*',
),
),
));
}
/**
* 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 Subcategory the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
Subcategory Admin Page
<?php
/* @var $this SubcategoryController */
/* @var $model Subcategory */
$this->breadcrumbs=array(
'Subcategories'=>array('index'),
'Manage',
);
$this->menu=array(
array('label'=>'List Subcategory', 'url'=>array('index')),
array('label'=>'Create Subcategory', 'url'=>array('create')),
);
Yii::app()->clientScript->registerScript(‘search’, "
$(’.search-button’).click(function(){
$('.search-form').toggle();
return false;
});
$(’.search-form form’).submit(function(){
$('#subcategory-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage Subcategories</h1>
<p>
You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php echo CHtml::link(‘Advanced Search’,’#’,array(‘class’=>‘search-button’)); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial(’_search’,array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget(‘zii.widgets.grid.CGridView’, array(
'id'=>'subcategory-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'title',
'description',
// array(
// ‘header’=>‘Category’,
// ‘value’=>‘CHtml::encode($data->category->title)’,//This will use the relationship and get all the details of the paticular user from users table
// ),
// array( 'name'=>'category_search', 'value'=>'$data->category->title' ),
//post strictly required user but in any case that model has not required user(or another relation) you should replace with it
array( 'name'=>'category_search', 'value'=>'$data->category ? $data->category->title: "-"' ),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Searching by title and description pop up an error. How do I fix this.