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

Make sure custom relations work out with new alias strictness. #1015

Merged
merged 10 commits into from
Nov 28, 2024

Conversation

dereuromark
Copy link
Member

@dereuromark dereuromark commented Nov 21, 2024

Refs #1012

So basically 5.1 and the more strict relation-handling ("Association alias Countries is already set") kinda broke the non documented feature of custom relationship baking.
Even though it was just baked wrongly with always the same Countries relation.

This PR adds the expected aliasing (back) ensuring there is no collision, and having the correct name in the process for most cases:

[
  'belongsTo' => [
    (int) 0 => [
      'alias' => 'Countries',
      'foreignKey' => 'country_id',
      'className' => 'Countries'
    ],
    (int) 1 => [
      'alias' => 'BillingCountries',
      'foreignKey' => 'billing_country_id',
      'className' => 'Countries'
    ],
    (int) 2 => [
      'alias' => 'ShippingCountries',
      'foreignKey' => 'shipping_country_id',
      'className' => 'Countries'
    ]
  ],
  'hasOne' => [],
  'hasMany' => [],
  'belongsToMany' => []
]

as example for

Countries

  • id

Users

  • id
  • country_id
  • billing_country_id
  • shipping_country_id

src/Command/ModelCommand.php Outdated Show resolved Hide resolved
src/Command/ModelCommand.php Outdated Show resolved Hide resolved
@dereuromark
Copy link
Member Author

It actually works:

        $this->belongsTo('Countries', [
            'foreignKey' => 'country_id',
            'className' => 'Countries',
        ]);
        $this->belongsTo('BillingCountries', [
            'foreignKey' => 'billing_country_id',
            'className' => 'Countries',
        ]);
        $this->belongsTo('ShippingCountries', [
            'foreignKey' => 'shipping_country_id',
            'className' => 'Countries',
        ]);

Maybe someone wants to add a test case for it? E.g. @luizcmarin ?
You can also test it and confirm

@dereuromark
Copy link
Member Author

Turns out there is already an existing test case that so far was already invalid (exception would have happened), and now working:
9631dd0

@dereuromark dereuromark changed the title WIP! Make sure custom relations work out with new alias strictness. Make sure custom relations work out with new alias strictness. Nov 25, 2024
@dereuromark
Copy link
Member Author

@ADmad I found a way to make it work.

correction due to the error shown below.


Creating file C:\marin\www\GitHub\lixo\tests\TestCase\Model\Table\AnnouncementsTableTest.php
Wrote `C:\marin\www\GitHub\lixo\tests\TestCase\Model\Table\AnnouncementsTableTest.php`
Done
One moment while associations are detected. 2024-11-26 10:24:32 warning: Undefined variable $className
Trace:
Bake\Command\ModelCommand->findBelongsTo() C:\marin\www\GitHub\trash\vendor\cakephp\bake\src\Command\ModelCommand.php, line 247
Bake\Command\ModelCommand->getAssociations() C:\marin\www\GitHub\trash\vendor\cakephp\bake\src\Command\ModelCommand.php, line 166
Bake\Command\ModelCommand->getTableContext() C:\marin\www\GitHub\trash\vendor\cakephp\bake\src\Command\ModelCommand.php, line 119
Bake\Command\ModelCommand->bake() C:\marin\www\GitHub\lixo\vendor\cakephp\bake\src\Command\ModelCommand.php, line 101
Bake\Command\ModelCommand->execute() C:\marin\www\GitHub\trash\vendor\cakephp\bake\src\Command\AllCommand.php, line 116
Bake\Command\AllCommand->execute() C:\marin\www\GitHub\trash\vendor\cakephp\cakephp\src\Console\BaseCommand.php, line 204
Cake\Console\BaseCommand->run() C:\marin\www\GitHub\trash\vendor\cakephp\cakephp\src\Console\CommandRunner.php, line 330
Cake\Console\CommandRunner->runCommand() C:\marin\www\GitHub\trash\vendor\cakephp\cakephp\src\Console\CommandRunner.php, line 168
Cake\Console\CommandRunner->run() C:\marin\www\GitHub\lixo\bin\cake.php, line 10
[main]
@luizcmarin
Copy link
Contributor

Maybe someone wants to add a test case for it? E.g. @luizcmarin ? You can also test it and confirm

#1018

@luizcmarin
Copy link
Contributor

I created a small test with some tables where errors occur. You may want to take note of this inconvenience.

invoices.txt
image

@dereuromark
Copy link
Member Author

I wonder if thats directly related to the issue here (was working before and now not)
It rather looks like a different topic and probably should be checked out independently (e.g. after merge of this).

@luizcmarin
Copy link
Contributor

I wonder if thats directly related to the issue here (was working before and now not) It rather looks like a different topic and probably should be checked out independently (e.g. after merge of this).

In fact, bake never finished generating the files due to this problem. This also happened with other tables. I isolated these attached tables for you to test, if you wish. But it happens with others too. Does this happen to you?

I'll send you the full script, in case you want to try it.

kancats.txt

@dereuromark
Copy link
Member Author

You need to actually share the commands you use
because the SQL table itself does not provide the info to replicate

For me both "bake all Countries" and then "bake all Users" works just fine now with the upgrade here.
Same for "Proposals" and "ProposalItems" of this schema.

So whats the concrete command that breaks, and what have been generated before, or if manually adjusted, what does that file look like before running bake.

Again: I am fairly certain this is outside the scope of this specific issue here, and should not block the relation fix for the current bug in the system.

@luizcmarin
Copy link
Contributor

I do not rule out the possibility of errors here. Please, if everything is ok in your tests, continue. I will do new tests after updating.

The command I use is simply: " bin\cake bake all --everything". Nothing more than that.

@dereuromark
Copy link
Member Author

I can partically reproduce it
The issue you are having is probably due to some invalid setup in your DB schema

    $this->hasMany('Proposals', [
        'foreignKey' => 'proposal_id',
        'className' => 'Invoices',
    ]);

This wrongly is a proposal_id, but should be something else at least to make clear this is from invoices table referenced, and also must always be different from the actual proposal_id since Proposals is their own table.
So you are creating on purpose a setup that bake everything cannot solve for you.
You need to fix your table setup here, e.g. use invoice_proposal_id or alike to make it unique.

@luizcmarin
Copy link
Contributor

Thank you very much for the tip. This means more work for me and less for you :) :(...
I'll review my scheme. I think this solves everything.

@dereuromark
Copy link
Member Author

Well, #1019 will make it easier for you once merged :)

@dereuromark
Copy link
Member Author

All good? Can we squash merge?

Copy link
Member

@ADmad ADmad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good apart from the minor nitpick of function name.

@dereuromark dereuromark merged commit 0f28cba into 3.x Nov 28, 2024
7 of 9 checks passed
@dereuromark dereuromark deleted the 3.x-alias-uniqueness branch November 28, 2024 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants