Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bin/cake bake all --everything yields infinite recursion for this use case #1029

Closed
geoidesic opened this issue Jan 28, 2025 · 3 comments
Closed
Labels

Comments

@geoidesic
Copy link
Contributor

Description

I'm getting infinite recursion when baking.
PeopleTable

class PeopleTable extends BakedPeopleTable
{
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->belongsToMany('AdministrativeOrganisations', [
            'through' => 'OrganisationsAdministratives',
            'className' => 'Organisations',
            'foreignKey' => 'administrative_id',
            'targetForeignKey' => 'organisation_id'
        ]);
        $this->belongsToMany('RepresentativeOf', [
            'through' => 'OrganisationsRepresentatives',
            'className' => 'Organisations',
            'foreignKey' => 'representative_id',
            'targetForeignKey' => 'organisation_id'
        ]);

        $this->belongsToMany('Administratives', ['through' => 'PeopleAdministratives', 'className' => 'People', 'foreignKey' => 'person_id', 'targetForeignKey' => 'administrative_id']);
        $this->belongsToMany('AdministrativesClients', ['through' => 'PeopleAdministratives', 'className' => 'People', 'foreignKey' => 'administrative_id', 'targetForeignKey' => 'person_id']);
        $this->belongsToMany('Representatives', ['through' => 'PeopleRepresentatives', 'className' => 'People', 'foreignKey' => 'person_id', 'targetForeignKey' => 'representative_id']);
        $this->belongsToMany('RepresentativesClients', ['through' => 'PeopleRepresentatives', 'className' => 'People', 'foreignKey' => 'representative_id', 'targetForeignKey' => 'person_id']);

OrganisationsTable

class OrganisationsTable extends BakedOrganisationsTable
{
    public function initialize(array $config): void
    {
        parent::initialize($config);
    
        $this->belongsToMany('Administratives', [
            'through' => 'OrganisationsAdministratives', 
            'className' => 'People', 
            'foreignKey' => 'organisation_id', 
            'targetForeignKey' => 'administrative_id'
        ]);

        $this->belongsToMany('Representatives', [
            'through' => 'OrganisationsRepresentatives', 
            'className' => 'People', 
            'foreignKey' => 'organisation_id', 
            'targetForeignKey' => 'representative_id'
        ]);

The idea is that both People and Organisations can have representatives or administratives (or both). Reps and admins are People too.
So in People it's a self-referencing m2m via a pivot table.
In Organisations it's an m2m to People via a pivot table.

Migrations:

 $table = $this->table('organisations_administratives');
        $table
            ->addColumn('organisation_id', 'uuid', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('administrative_id', 'uuid', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('created', 'datetime', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('modified', 'datetime', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addIndex(
                ['organisation_id']
            )
            ->addIndex(
                ['administrative_id']
            );
            
        $table->create();
    }
        $table = $this->table('organisations_representatives');
        $table
            ->addColumn('organisation_id', 'uuid', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('representative_id', 'uuid', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('created', 'datetime', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('modified', 'datetime', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addIndex(
                ['organisation_id']
            )
            ->addIndex(
                ['representative_id']
            );
            
        $table->create();
        $table = $this->table('people_administratives');
        $table
            ->addColumn('person_id', 'uuid', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('administrative_id', 'uuid', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('created', 'datetime', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('modified', 'datetime', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addIndex(
                ['person_id']
            )
            ->addIndex(
                ['administrative_id']
            );
            
        $table->create();
        $table = $this->table('people_representatives');
        $table
            ->addColumn('person_id', 'uuid', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('representative_id', 'uuid', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('created', 'datetime', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('modified', 'datetime', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addIndex(
                ['person_id']
            )
            ->addIndex(
                ['representative_id']
            );
            
        $table->create();
```php

        $this->table('people', ['id' => false, 'primary_key' => ['id']])
            ->addColumn('id', 'uuid', [
                'default' => null,
                'limit' => null,
                'null' => false,
            ])
            ->addColumn('active', 'boolean', [
                'default' => false,
                'limit' => null,
                'null' => false,
            ])
            ->addColumn('role_id', 'string', [
                'default' => '5',
                'limit' => 255,
                'null' => 4,
                'signed' => false,
            ])
            ->addColumn('csvimport_id', 'integer', [
                'limit' => 11,
                'null' => true,
                'signed' => false,
            ])
            ->addColumn('bankdetail_id', 'uuid', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('term_id', 'uuid', [
                'default' => null,
                'limit' => null,
                'null' => true,
                'comment' => 'Terms & Conditions'
            ])
            ->addColumn('name', 'string', [
                'default' => null,
                'limit' => 32,
                'null' => false,
            ])
            ->addColumn('middlename', 'string', [
                'default' => null,
                'limit' => 32,
                'null' => true,
            ])
            ->addColumn('surname', 'string', [
                'default' => null,
                'limit' => 32,
                'null' => false,
            ])
            ->addColumn('dateofbirth', 'date', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('email', 'string', [
                'limit' => 255,
                'null' => true,
                'default' => null,
            ])
            ->addColumn('password', 'string', [
                'default' => null,
                'limit' => 255,
                'null' => true,
            ])

            ->addColumn('created', 'datetime', [
                'default' => 'CURRENT_TIMESTAMP',
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('modified', 'datetime', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addIndex(
                [
                    'email',
                ],
                ['unique' => true]
            )
            ->create();
        $this->table('organisations', ['id' => false, 'primary_key' => ['id']])
            ->addColumn('id', 'uuid', [
                'default' => null,
                'limit' => null,
                'null' => false,
            ])
            ->addColumn('name', 'string', [
                'default' => null,
                'limit' => 255,
                'null' => false,
            ])
            ->addColumn('email', 'string', [
                'default' => null,
                'limit' => 255,
                'null' => true,
            ])
            ->addColumn('phone', 'string', [
                'default' => null,
                'limit' => 255,
                'null' => true,
            ])
            ->addColumn('address1', 'string', [
                'default' => null,
                'limit' => 255,
                'null' => true,
            ])
            ->addColumn('address2', 'string', [
                'default' => null,
                'limit' => 255,
                'null' => true,
            ])
            ->addColumn('town', 'string', [
                'default' => null,
                'limit' => 255,
                'null' => true,
            ])
            ->addColumn('country', 'json', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('incorporation_country', 'json', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('county', 'json', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('postcode', 'string', [
                'default' => null,
                'limit' => 255,
                'null' => true,
            ])
            ->addColumn('registration_number', 'string', [
                'default' => null,
                'limit' => 255,
                'null' => true,
            ])
            ->addColumn('active', 'boolean', [
                'default' => false,
                'limit' => null,
                'null' => false,
            ])
            ->addColumn('created', 'datetime', [
                'default' => 'CURRENT_TIMESTAMP',
                'limit' => null,
                'null' => true,
            ])
            ->addColumn('modified', 'datetime', [
                'default' => null,
                'limit' => null,
                'null' => true,
            ])
            ->create();

Bake Version

2.9.3

PHP Version

No response

@LordSimal
Copy link
Contributor

I can't reproduce your problem.

I am on CakePHP 4.5.9, Bake 2.9.3, created one migration containing all your tables from above, executed it via bin/cake migrations migrate and executed bin/cake bake all --everything

It correctly generates all the files from the default bake template. See my terminal output here

log.txt

@geoidesic
Copy link
Contributor Author

geoidesic commented Jan 29, 2025

By not implementing the provided Model/Table associations, are you implying that bake ignores custom associations on the Table class?

@geoidesic
Copy link
Contributor Author

Turns out this was caused by things unrelated to bake. Thanks for the help in getting it figured out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants