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

Tuesday, February 22, 2022

[FIXED] How to delete data in custom finder method in CakePHP 3

 February 22, 2022     cakephp-3.0, cakephp-3.1, cakephp-3.x     No comments   

Issue

I want to delete data in custom finder method.

Custom finder method document

My code:

public function findPREACTIVE(Query $query, array $options) {
    $query->delete()
            ->where(['member_status' => -1])
            ->andWhere(['registered >= DATE_SUB(NOW(), INTERVAL 72 HOUR)'])->execute();

    return $query
                    ->where(['email' => $options['email'], 'token_key' => $options['token_key']])
                    ->andWhere(['member_status' => -1])
                    ->andWhere(['registered < DATE_SUB(NOW(), INTERVAL 72 HOUR)']);
}

When i call this finder, i get error:

You cannot call all() on a non-select query. Use execute() instead.

Is there have solution for this case?


Solution

You'll have to issue a separate query. What you are doing there will mess up the finder query, which is ment to be a select query.

$this
    ->query()
    ->delete()
    ->where(['member_status' => -1])
    ->andWhere(['registered >= DATE_SUB(NOW(), INTERVAL 72 HOUR)'])->execute();

Use applyOptions() in case you need the finder options to be applied to the delete query too.

$this
    ->query()
    ->delete()
    ->applyOptions($options)
    // ...


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