6. The Advanced Stuff
6.3. Named Scopes
6.2. Primary Keys
« Previous
6.4. Relations
Next »

6.3. Named Scopes

AuthorDariusz Górecki

Now you can use AR style named scopes just define scopes method in yours model:

class Client extends EMongoDocument
{
    // (...)
 
    public function scopes()
    {
        return array(
            'scopeName'=>array(/* Array for criteria object creation see Criteria from array topic */),
        );
    }
 
    public function defaultScope()
    {
        return array(
            'conditions'=>array(
                'active'=>array('==', true),
            ),
        );
    }
 
    // (...)
}
 
// now You can:
// this will find only clients that are active (default scope) and clients matching 'scopeName'
$all = Client::model()->scopeName()->findAll();

Parameterized Named Scopes

class Client extends EMongoDocument
{
    // (...)
 
    public function byStatus($status)
    {
        $criteria = $this->getDbCriteria();
        $criteria->status = $status;
        $this->setDbCriteria($criteria);
 
        return $this;
    }
 
    // (...)
}
 
// now You can:
// this will find only clients that have status set to parameter value:
$all = Client::model()->byStatus(1)->findAll();

You can chain multiple named scopes

example: Client::model()->active()->byStatus(1)->findAll();

6.3. Named Scopes
6. The Advanced Stuff
« Previous
6.2. Primary Keys
Next »
6.4. Relations