N
N
nepster-web2013-12-20 04:58:52
Yii
nepster-web, 2013-12-20 04:58:52

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);

Now what is the point, for example, I want to show all users who have trusted = 1, that is, only this check should be performed
if($UsersModel->trusted || (string)$UsersModel->trusted === '0')
            $criteria->compare('trusted',$UsersModel->trusted);

But all numerical checks are constantly performed, since I have default values ​​​​in the database.
if I do not pass the active parameter for example and print out the attributes,
print_r($UsersModel->getAttributes());
then this active will be = the default value from the table (that is, 0). And this whole thing spoils my search.
Can you please tell me how to fix the situation, for example, disable default values ​​in attributes?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Skuridin, 2013-12-20
@nepster-web

After creating the model object, clear the attributes $model->unsetAttributes();
Read about Yii's search() method so you don't mess around with scripting.

S
Sergey Velichko, 2013-12-20
@soldat58

The first thing that comes to mind

if(isset($params['UsersModel']['user_id']))
    $criteria->compare('user_id',$UsersModel->user_id);

_
_ _, 2013-12-20
@AMar4enko

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)

will solve all your problems. The only thing is, before that, go through $params and manually cast the fields to the required types where necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question