How can I dynamically build the contain in the new cakephp 3 query builder. This is what I have now:
$query = $dbTable->find()
->select($contain['select']['fields'])
->contain(function($q) use($array,$contain){
$new = [];
foreach($array as $v){
if(isset($contain['contains'][$v])){
$fields = $contain['contains'][$v];
$new[$v] = $q->select($fields);
}
}
return $new;
});
But I am getting several errors with this:
Warning (2): Illegal offset type in isset or empty [CORE\src\ORM\EagerLoader.php, line 198]
Warning (2): strpos() expects parameter 1 to be string, object given [CORE\src\ORM\EagerLoader.php, line 203]
Warning (2): Illegal offset type [CORE\src\ORM\EagerLoader.php, line 223]
Warning (2): Illegal offset type [CORE\src\ORM\EagerLoader.php, line 224]
As already mentioned by Lorenzo, that's not how it works,
contain()
doesn't accept callables, just look at the docs:http://api.cakephp.org/3.0/class-Cake.ORM.Query.html#_contain
Also your code would invoke
select()
multiple times on one and the same query, that wouldn't work anyways.However, looking at your code it seems that you could simply make use of the
fields
option, ie build a simple array to pass tocontain()
. This is shown in the docs, the example however will trigger an error as it seems to be necessary to explicitly set the foreign key field too: