Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining global conditions in Model

Is it possible to define global conditions for Model ?

I have 2 Models: User and Student. In database both of them are using table users but each student has set parent_id to its owner (which is set in the same table) while each user has parent_id set to Null.

When I use for example

$this->find('all'); 

in Student Model I want to force Cake to return only those records from database table users where parent_id != Null .

So the question is - can I define somehow global conditions in the Model? Something like that:

public $conditions = array('Student.parent_id !=' => Null);

like image 906
Ziemo Avatar asked Jul 09 '13 08:07

Ziemo


People also ask

What is global conditions?

In Configuration Manager, global conditions are rules that represent business or technical conditions that you can use to specify how an application is provided and deployed to client devices. Global conditions are accessed from the Requirements page of the Create Deployment Type Wizard.

What is the number of default global conditions available in SCCM console?

Let's learn how to create SCCM Global Conditions using the admin console. There are 22 out-of-the-box global conditions available and ready to use.


1 Answers

Use beforeFind

You can use before find to modify all queries issued for a model:

function beforeFind(array $queryData) {
    $queryData['conditions'][]['NOT'][$this->alias . '.parent_id'] = null;
    return $queryData;
}

Be careful using this technique to not overwrite existing conditions (note the extra []) otherwise a query for "not parent_id 2" becomes "not parent_id null".

like image 96
AD7six Avatar answered Oct 02 '22 11:10

AD7six