Search code examples
cakephp

Cannot add subquery to query


I am getting error when I am doing this

$excludedFlows = $this->FlowsPredefined->find('all',[
                        'conditions' => [
                            'category' => self::INCREASE_CUSTOMER_LOYALTY_FLOWS_CATEGORY,
                            'is_new' => 1,
                            'channel' => 'all'
                        ]
                    ])
                    ->select('id')
                    ->all();
                    $conditions['FlowsPredefined.id NOT IN'] = $excludedFlows;

The error is :

Impossible to generate condition with empty list of values for field (FlowsPredefined.id)

The excludedFlows variable is not empty. I have 6 results in it. How to execute NOT IN in cakePHP ?


Solution

  • You're not passing a query, but a result set, as you're calling all(), and result sets are not supported as condition values.

    Either do not call all() if you want to actually use a subquery, or, if applicable (you don't want to do this with huge numbers or results), extract the IDs into an array, like ->all()->extract('id')->toList().