PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 14, 2022

[FIXED] Filter all find queries on entity CakePHP 3.6

 January 14, 2022     cakephp, cakephp-3.6, php, php-7     No comments   

Issue

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?


Solution

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;
}


Answered By - ascsoftw
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing