Answer the question
In order to leave comments, you need to log in
SetAttributes in Yii, search problem?
There is such a problem, I need to search for users by certain parameters.
You need to search for those parameters that are in $params.
It can be user_id and trusted, it can be just user_id and so on.
So, there are no problems with strings. But with statuses, especially where there are default fields, a problem arises.
My controller:
$UsersModel = new UsersModel;
$UsersModel->scenario = 'control_search';
$UsersModel->setAttributes($params);
$criteria = new CDbCriteria;
$criteria->limit = ProcessingData::load()->limit();
$criteria->order = '1date_register DESC';
$criteria->offset = ProcessingData::load()->offset($params['page'],$criteria->limit);
if($UsersModel->user_id)
$criteria->compare('user_id',$UsersModel->user_id);
if($UsersModel->trusted || (string)$UsersModel->trusted === '0')
$criteria->compare('trusted',$UsersModel->trusted);
if($UsersModel->active || (string)$UsersModel->active === '0')
$criteria->compare('active',$UsersModel->active);
$criteria->compare('login',$UsersModel->login, true);
$criteria->compare('name',$UsersModel->name, true);
$criteria->compare('mail',$UsersModel->mail, true);
if($UsersModel->trusted || (string)$UsersModel->trusted === '0')
$criteria->compare('trusted',$UsersModel->trusted);
print_r($UsersModel->getAttributes());
Answer the question
In order to leave comments, you need to log in
After creating the model object, clear the attributes $model->unsetAttributes();
Read about Yii's search() method so you don't mess around with scripting.
The first thing that comes to mind
if(isset($params['UsersModel']['user_id']))
$criteria->compare('user_id',$UsersModel->user_id);
I understand correctly - the code that you provided is partly what gii generated for you, right?
$criteria = new CDbCriteria;
$criteria->limit = ProcessingData::load()->limit();
$criteria->order = '1date_register DESC';
$criteria->offset = ProcessingData::load()->offset($params['page'],$criteria->limit);
foreach($params as $attr => $value)
$criteria->compare($attr, $value)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question