How to change an association strategy on-the-fly?

I would like to change an association strategy (hasMany) on the fly to “in” (default) to “select”. Because this will correct the result for this situation:

" Get all publishers and only the first five books ":

$publishersTable = TableRegistry::getTableLocator()->get('Publishers');

$publishersTable->getAssociation('Books')->setStrategy('select');       
        $query = $publishersTable->find()
                ->contain(['Books'=> function(Query $q){

                    return $q->limit(5);

                }]);

Unfortunately, Cake still using “in” to run the query and not “separated queries” and the result is only 5 publishers (and not all publishers with the first 5 books).

Is it possible to change the strategy on-the-fly?
Thanks in advance !

1 Like