Filtering Returned Models

Hello everyone,

I am a keen developer and i’m currently working on quite a complicated project.

I have a table in a DB that stores settings. I get the settings using something like this




$site= SCHSites::model()->find("SOME criteria");


var_dump($site->settings);



Now settings is an array of models, a setting basically has a name and value.

All i want to do is filter the set of results by an attribute. Now i know i can do somthing along the lines of this in the SCHSite model


    public function getSettingByName($name){

        foreach ($this->settings as $value){

            if($value->name == $name){

                return $value->value;

            }

        }

        return false;

    }



but does Yii offer me a better solution ?

Thanks,

Dwayne

Maybe you could typecast/convert the settings of the site to a protected variable $_settings as assoziative array (name=value) afterFind() of SCHSites.




 public function getSettingByName($name,$default=false){

        return isset($this->_settings[$name]) ? $this->_settings[$name] : $default;

    }




Hello, thanks for the reply but my question wasn’t really related to the example. I meant it to illustrate what i was asking… Can Yii offer me a easy way to filter rows that are returned.

I don’t know another way with Yii.

If you query records from the db and you want to filter the received rows you always have to loop through the results.

Why don’t you filter the records when quering the db by criteria/sql?

In my example i used every time i need a setting i would be doing a query… i didn’t think this was the best idea. I guess Yii doesn’t support this out of the box … thats fine … thanks for your help.