Hi all,
I tried to modify a search function so it able to search in multiple table instead of one.
this is the current search code
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'profile';
}
/**
* @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('date_create', 'required'),
array('status, category', 'numerical', 'integerOnly'=>true),
array('image', 'length', 'max'=>255),
array('title, description', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('ID, title, image, description, category', '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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'ID' => 'ID',
'title' => 'Title',
'image' => 'Image',
'description' => 'Description',
'status' => 'Status',
'date_create' => 'Date Create',
'category' => 'Category',
);
}
/**
* 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->compare('ID',$this->ID);
$criteria->compare('title',$this->title,true);
$criteria->compare('image',$this->image,true);
$criteria->compare('description',$this->description,true);
$criteria->compare('status',$this->status);
$criteria->compare('date_create',$this->date_create,true);
$criteria->compare('category',$this->category);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
I have another table that I also want to be include in the search.
table: category
with column name: id, name
so i tried to modify the following
public function tableName()
{
return array('profile', 'category'); // Add for category table
}
/**
* @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('date_create', 'required'),
array('status, category', 'numerical', 'integerOnly'=>true),
array('image', 'length', 'max'=>255),
array('title, description', 'safe'),
array('id', 'name');// Add for category table
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('ID, title, image, description, category, id, name', '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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'ID' => 'ID',
'title' => 'Title',
'image' => 'Image',
'description' => 'Description',
'status' => 'Status',
'date_create' => 'Date Create',
'category' => 'Category',
);
}
/**
* 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->compare('ID',$this->ID);
$criteria->compare('title',$this->title,true);
$criteria->compare('image',$this->image,true);
$criteria->compare('description',$this->description,true);
$criteria->compare('status',$this->status);
$criteria->compare('date_create',$this->date_create,true);
$criteria->compare('category',$this->category);
$criteria->compare('id',$this->id); // Add for category table
$criteria->compare('name',$this->name); // Add for category table
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
I modified the above but it didn’t work.
Sorry for noob question, im not a programmer, and tried to learn as i go since i can’t contact the developer anymore