0

let's say that I have an articles database and I want to only display articles that are published on the site (where published = 1). Instead of adding the condition in every find queries like the following:

$articles = $this->Articles->find('all')->where(['published' => 1]);

Is there a way that I can automatically apply this condition on all the find queries in the whole application at one place? If so how?

user765368
  • 19,590
  • 27
  • 96
  • 167

1 Answers1

2

You can use beforeFind. This will be fired before every find query on your Article Model. Here is the documentation

Here is how to use it

public function beforeFind($event, $query, $options, $primary)
{

    $query->where(['article.visible' => 1]);

    return $query;
}
ascsoftw
  • 3,466
  • 2
  • 15
  • 23