N
N
nepster-web2013-12-18 21:28:25
Yii
nepster-web, 2013-12-18 21:28:25

Yii CDbCriteria and data fetch?

I work with Yii and in the admin panel I have the following URL:
site.ru/controller/action/active:1;trusted:1 ...
Line: active:1;trusted:1 we have the data to select.
For example, I want to get all active and trusted users.
It will look something like this:

$criteria = new CDbCriteria;
        $criteria->limit     = ProcessingData::load()->limit();
        
         if(isset($params['active']) && is_numeric($params['active']))
               $criteria->compare('active',$params['active']);

         if(isset($params['trusted']) && is_numeric($params['active']))
               $criteria->compare('trusted',$params['trusted']);
        
        $criteria->order     = 'date DESC';
        $criteria->offset    = ProcessingData::load()->offset($params['page'],$criteria->limit);

But sometimes there are 5 and 7 such data, and you constantly have to process ifs and list arguments.
Can you please tell me if there is an easier way?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
N
Nokyta, 2013-12-18
@Nokyta

Why didn't the standard search() method in the model work? We drive everything that is needed from the get into the attributes of the model, validate it and then get the data from the search.

Y
Yuri, 2013-12-18
@error500

Can you try something like this

foreach( $params as $key=>$value )
{
    if(is_numeric($value))
    {
        $criteria->compare($key,$value);
     }
}

well it in a case if numbers always come, naturally.

N
nepster-web, 2013-12-18
@nepster-web

here again, how do we know that $key will contain the value existing in the base column?

E
Eugene, 2013-12-18
@Nc_Soft

Do ifami, there will be fewer problems.

Y
Yuri, 2013-12-19
@error500

I don’t program in Yii at all, but if entities are used there as in the doctrine or some other things, then you can try something like this:

foreach( $params as $key=>$value )
{
    //if( property_exists(new Class, $key) && is_numeric($value) )
    if( property_exists('Ваш класс', $key) && is_numeric($value) )
    {
         $criteria->compare($key,$value);
     }
}

Well, or whatever you like more, there are a lot of options.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question