cakephp
cakephp copied to clipboard
[4.3.1][ORM] Table::loadinto() fail with The `includeFields` association is not defined
Description
Hello
In some case $aTable->loadInto( $this, [ 'bTable.cTable' ] ); failed with error "The includeFields association is not defined on UserGroups. Valid associations are: Parent, SiblingChildren".
Sorry, I've no simple TestCase/UnitTest :(
But if I comment $config['includeFields'] = true; in Cake\ORM\LazyEagerLoader::_getQuery() in attachableAssociations() loop every things work without error.
// vendor/cakephp/cakephp/src/ORM/LazyEagerLoader.php
namespace Cake\ORM;
class LazyEagerLoader
{
...
protected function _getQuery(CollectionInterface $objects, array $contain, Table $source): Query
{
...
foreach ($query->getEagerLoader()->attachableAssociations($source) as $loadable) {
$config = $loadable->getConfig();
// $config['includeFields'] = true; <- COMMENT HERE
$loadable->setConfig($config);
}
return $query;
}
Effectively if not commented, the EagerLoader::_normalizeContain() try to addAssociation( "includeFields" ) which is a forced configuration not a table :-(
// vendor/cakephp/cakephp/src/ORM/EagerLoader.php
namespace Cake\ORM;
class EagerLoader
{
...
protected function _normalizeContain(Table $parent, string $alias, array $options, array $paths): EagerLoadable
{
...
foreach ($extra as $t => $assoc) {
Log::debug(__METHOD__.' $t: '.$t); // <-- Here $t == "includeFields" ;
$eagerLoadable->addAssociation( $t, $this->_normalizeContain($table, $t, $assoc, $paths)
);
}
return $eagerLoadable;
}
CakePHP Version
4.3.1
PHP Version
7.4
I know code is missing for reproduction, but the project is very complex and I've to create a dedicated one to re-try the possible bug.
Any way, in the Cake code it's very strange that the forced $config "includeFields" key is used as a Table alias, isn't it ?
You don't necessarily have to provide a complete working sample, the association configurations of your 3 involved tables might already be enough for someone to be able to reproduce the problem.
Commenting out that line doesn't cause any tests to fail, but we might be missing assertions. Having a way to reproduce the issue would help here.
Another solution is to add 'includeFields' => 1 to EagerLoader::_containOptions (Idea from ADmad on Slack).
I've no complete code, only that the issue occurs on a loadInto() with a sub relation :
TableRegistry::getTableLocator()->get(
$this->getSource()
)->loadInto( $this, [
'Supports.UserGroups'
] );
Used in an Entity accessor :
class Device extends Entity
{
...
public function _getSupport() : ?Support
{
if( ! array_key_exists( 'support', $this->_fields ) )
{
TableRegistry::getTableLocator()->get(
$this->getSource()
)->loadInto( $this, [
'Supports.UserGroups'
] );
if( !isset($this->_fields['support']) )
$this->_fields['support'] = null ;
}
return $this->_fields['support'] ;
}
...
Hello :-) This bug seems to be not important... But it's a core ORM behavior. Do you have any comment about it ?
This bug seems to be not important... But it's a core ORM behavior.
It isn't an issue I've run into with my work, and it doesn't seem like others have run into it much either. If it is important to you, you could send a pull request with a test that covers the change in behavior.
You don't necessarily have to provide a complete working sample, the association configurations of your 3 involved tables might already be enough for someone to be able to reproduce the problem.
We still need more information. Can you provide the association configs for the tables?
Thank you for your interest :-)
I'll try to bring more soon.