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

Wednesday, March 9, 2022

[FIXED] get records with 2 not like conditions in cakephp3

 March 09, 2022     cakephp, cakephp-3.0     No comments   

Issue

This query doesnt work as expected. What happens it will select records with the word assessment in it. I have tried a few variations but as you can see the query should only get records with the word 'maths' and exclude records with 'scholarship' and 'assessment'.

I cant get this to work in cakephp3. What happens is that i get records with the words assessment or scholarship.

$subjectMaths = $this->Students->Subjects->find('list')
    ->select(['id', 'name'])
    ->where([   
        'Subjects.name not LIKE'  => '%assessment%' ,
        'Subjects.name LIKE'  => '%maths%', 
        'Subjects.name not LIKE'  => '%scholarship%' 
    ])  
    ->order(['Subjects.name' => 'asc']);


$subjectMaths = $this->Students->Subjects->find('list')
    ->select(['id', 'name'])
    ->where([   
        'Subjects.name LIKE'  => '%maths%', 
        'OR'=> [
            ['Subjects.name not LIKE'  => '%assessment%' ],
            [ 'Subjects.name not LIKE'  => '%scholarship%',] 
        ]
    ])  
    ->order(['Subjects.name' => 'asc']);

Solution

the proble here is that you have two identical array keys Subjects.name not LIKE. The second overwrites the first

So you havw to use AndWhere()

$subjectMaths = $this->Students->Subjects->find('list')
    ->select(['id', 'name'])
    ->where(['Subjects.name not LIKE'  => '%assessment%'])
    ->andWhere(['Subjects.name LIKE'  => '%maths%']) 
    ->andWhere(['Subjects.name not LIKE'  => '%scholarship%']) 
    ->order(['Subjects.name' => 'asc']);


Answered By - arilia
  • 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