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

Tuesday, March 15, 2022

[FIXED] Updating query using where and matching with Cakephp 3.X - is it possible?

 March 15, 2022     cakephp, cakephp-3.0, join, query-builder, sql-update     No comments   

Issue

Trying to update some rows based on related data, using matching. But only the "where" clause is working. The "matching" is ignored!

Am I, doing something wrong?

$tbTU = TableRegistry::get('TaskUsers');
$query = $tbTU->query()
    ->where([
        'TaskUsers.service_order_id' => $so_id,
        'TaskUsers.status IN' => [0, 4, 7, 9],
    ]) 
    ->contain(['Tasks'])
    ->matching('Tasks', function($q) {
        return $q->where([
            'Tasks.task_type IN' => [1, 4, 5]
        ]); 
    });

return $query
    ->update()
    ->set([
        'status' => 3,
        'user_id' => NULL,
        'modified' => Time::now()
    ])
    ->execute();

Thanks!!


Solution

It seems that the answer to your question is no, according to this issue

As @ndm says in the comments you can use a subquery depending on your DBMS

I would do something like this:

$subquery = $this->TaskUsers->Tasks->find()
    ->select(['id'])
    ->where(['Tasks.task_type IN' => [1, 4, 5]]);

$tbTU->query()
    ->where([
        'TaskUsers.service_order_id' => $so_id,
        'TaskUsers.status IN' => [0, 4, 7, 9],
        'TaskUsers.task_id IN' => $subquery
    ])
});


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