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

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

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from

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 already 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

This would still need to get finished up inside Bake Model command of course.
Maybe someone wants to help out here.

* @param array<string, mixed> $association
* @return string
*/
protected function alias(array $association): string
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
protected function alias(array $association): string
protected function createAssociationAlias(array $association): string

alias is a pretty general term, I think a more specific name could be helpful for future us.

foreach ($associationsPerType as $k => $association) {
$alias = $association['alias'];
if (in_array($alias, $existing, true)) {
$alias = $this->alias($association);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$alias = $this->alias($association);
$alias = $this->createAssociationAlias($association);

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

Successfully merging this pull request may close these issues.

2 participants