0

I would like to ask, if there is way to (let's say in model class) set where condition that has to be in every single query.

Example: Model A has field awesome. I want when using $this->A->find('all') to have sql query with WHERE awesome=1 , but I want this condition to every find without specifying it every time.

As far as I searched it's possible when specifying associations between two models

pfulop
  • 1,009
  • 13
  • 24
  • possible duplicate of [Defining global conditions in Model](http://stackoverflow.com/questions/17543774/defining-global-conditions-in-model) – ndm Jan 12 '15 at 17:52
  • You can look into ideas discussed here: [cakephp-and-namedscope-for-dry-conditions](http://www.dereuromark.de/2014/02/15/cakephp-and-namedscope-for-dry-conditions/) – mark Jan 12 '15 at 18:01

3 Answers3

3

Use beforeFind callback in AppModel.php.
You can modify $query params...

kicaj
  • 2,881
  • 5
  • 42
  • 68
1

You'll want to create a custom Behavior.

Then, every model that you'd like to add that condition to should utilize that behavior (via the $actsAs variable.

In the behavior, you can add a beforeFind() method. In that method, you can add anything you want to the 'conditions' array.

Therefore, every find() you run on the models will run that beforeFind(), and get the conditions added to it before running the actual find.

Dave
  • 28,833
  • 23
  • 113
  • 183
-1

To use conditions in cakePHP

$client = $this->Client->find('all', array('conditions' =>array('LastName like' => "%something%")));
ErasmoOliveira
  • 1,416
  • 2
  • 20
  • 40
  • 1
    I am sorry but this is not Answer I am looking for. This is conditions per one find, I want them global. – pfulop Jan 12 '15 at 17:36