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

Tuesday, February 22, 2022

[FIXED] using find() in cakephp3

 February 22, 2022     cakephp, cakephp-3.0, datasource, sql     No comments   

Issue

How can I use find() to execute queries such as :

select * from table1 
where status =1 and (title like '%keyword%' OR content like '%keyword%');

in cakephp 3


Solution

By using functions as the parameters to orWhere() and andWhere(), you can compose conditions together with the expression objects:

$query = $table1->find()
        ->where(['status' => 1])
        ->andWhere(function ($exp) {
            return $exp->or_([
                'title LIKE' => '%keyword%',
                'content LIKE' => '%keyword%'
            ]);
 });

or you can simply use below code

$query = $table1->find()
    ->where([
        'status' => 1,
        'OR' => [['title LIKE' => '%keyword%'], ['content LIKE' => '%keyword%']],
    ]);

For details please see link



Answered By - Alimon Karim
  • 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