Hello,
I am struggling with an error now for hours. I am trying to contain a associated table(this table belongsTo the associated one) by default (using the beforeFind Callback) and want to select only one field, but cakephp is throwing an typeerror constantly. This is the code the error comes from:
public function beforeFind(Event $event, Query $query, \ArrayObject $options){
$query->contain(['VoucherTypes' => function($q){
return $q->select('handle');
}]);
}
This is the error I get:
Uncaught TypeError: Argument 1 passed to App\\Model\\Table\\VoucherTypesTable::App\\Model\\Table\\{closure}() must implement interface Cake\\Datasource\\ResultSetInterface, instance of Cake\\Collection\\Collection given, called in C:\\xampp\\htdocs\\Radbonus_Verwaltung\\vendor\\cakephp\\cakephp\\src\\ORM\\Association.php on line 837 and defined in C:\\xampp\\htdocs\\Radbonus_Verwaltung\\src\\Model\\Table\\VoucherTypesTable.php:31\nStack trace:\n#0 C:\\xampp\\htdocs\\Radbonus_Verwaltung\\vendor\\cakephp\\cakephp\\src\\ORM\\Association.php(837): App\\Model\\Table\\VoucherTypesTable-\u003EApp\\Model\\Table\\{closure}(Object(Cake\\Collection\\Collection))\n#1 C:\\xampp\\htdocs\\Radbonus_Verwaltung\\vendor\\cakephp\\cakephp\\src\\Datasource\\QueryTrait.php(500): Cake\\ORM\\Association-\u003ECake\\ORM\\{closure}(Object(Cake\\ORM\\ResultSet))\n#2 C:\\xampp\\htdocs\\Radbonus_Verwaltung\\vendor\\cakephp\\cakephp\\src\\ORM\\Query.php(1181): Cake\\ORM\\Query-\u003E_applyDecorators(Object(Cake\\ORM\\ResultSet))\n#3 C:\\xampp\\htdocs\\Radbonus_Verwaltung\\vendor\\cakephp\\cakephp\\src\\Datasource\\QueryTrait.php(276): C
Uncaught TypeError: Argument 1 passed to App\\Model\\Table\\VoucherTypesTable::App\\Model\\Table\\{closure}() must implement interface Cake\\Datasource\\ResultSetInterface, instance of Cake\\Collection\\Collection given, called in C:\\xampp\\htdocs\\Radbonus_Verwaltung\\vendor\\cakephp\\cakephp\\src\\ORM\\Association.php on line 837 and defined in C:\\xampp\\htdocs\\Radbonus_Verwaltung\\src\\Model\\Table\\VoucherTypesTable.php:31\nStack trace:\n#0 C:\\xampp\\htdocs\\Radbonus_Verwaltung\\vendor\\cakephp\\cakephp\\src\\ORM\\Association.php(837): App\\Model\\Table\\VoucherTypesTable-\u003EApp\\Model\\Table\\{closure}(Object(Cake\\Collection\\Collection))\n#1 C:\\xampp\\htdocs\\Radbonus_Verwaltung\\vendor\\cakephp\\cakephp\\src\\Datasource\\QueryTrait.php(500): Cake\\ORM\\Association-\u003ECake\\ORM\\{closure}(Object(Cake\\ORM\\ResultSet))\n#2 C:\\xampp\\htdocs\\Radbonus_Verwaltung\\vendor\\cakephp\\cakephp\\src\\ORM\\Query.php(1181): Cake\\ORM\\Query-\u003E_applyDecorators(Object(Cake\\ORM\\ResultSet))\n#3 C:\\xampp\\htdocs\\Radbonus_Verwaltung\\vendor\\cakephp\\cakephp\\src\\Datasource\\QueryTrait.php(276): C
The interesting thing is, that even when I use the array approach instead of the closure approach, the error is the same:
public function beforeFind(Event $event, Query $query, \ArrayObject $options){
$query->contain(['VoucherTypes' => [
'fields'=> ['handle']
]]);
}
After reading the Error message again, I found, that the error had nothing to do with the class which contains the beforeFind method and only contains the VoucherTypes class but instead it had something to do with an implementation error in the VoucherTypes beforeFind: