cakephp icon indicating copy to clipboard operation
cakephp copied to clipboard

[4.3.1][ORM] Table::loadinto() fail with The `includeFields` association is not defined

Open Cyrille37 opened this issue 3 years ago • 9 comments

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

Cyrille37 avatar Mar 04 '22 07:03 Cyrille37

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 ?

Cyrille37 avatar Mar 04 '22 14:03 Cyrille37

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.

ndm2 avatar Mar 04 '22 14:03 ndm2

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.

markstory avatar Mar 04 '22 14:03 markstory

Another solution is to add 'includeFields' => 1 to EagerLoader::_containOptions (Idea from ADmad on Slack).

Cyrille37 avatar Mar 09 '22 08:03 Cyrille37

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'] ;
    }
    ...

Cyrille37 avatar Mar 09 '22 10:03 Cyrille37

Hello :-) This bug seems to be not important... But it's a core ORM behavior. Do you have any comment about it ?

Cyrille37 avatar May 12 '22 14:05 Cyrille37

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.

markstory avatar May 12 '22 19:05 markstory

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?

othercorey avatar Sep 11 '22 20:09 othercorey

Thank you for your interest :-)

I'll try to bring more soon.

Cyrille37 avatar Sep 12 '22 08:09 Cyrille37