cakephp / cakephp

CakePHP: The Rapid Development Framework for PHP - Official Repository
http://cakephp.org
MIT License
8.68k stars 3.44k forks source link

Inflector not correctly observed when baking association tables #17687

Closed Manuel1948 closed 1 month ago

Manuel1948 commented 1 month ago

Description

My inflector rules (in bootstrap(_cli).php):

Inflector::rules('plural', ['/^(benutzerrolle|verrechnungsstufe|kunde)$/i' => '\1n']);
Inflector::rules('plural', ['/^(buchung|zeit)$/i' => '\1en']);
Inflector::rules('plural', ['/^(kind)$/i' => '\1er']);
Inflector::rules('plural', ['/^(haustier)$/i' => '\1e']);
Inflector::rules('irregular', ['einsatz' => 'einsaetze','firma' => 'firmen','betreuerin' => 'betreuerinnen']);
Inflector::rules('uninflected', ['benutzer']);

The baked table of KinderKunden is correct, but each of them has a wrong (plural) belongsToMany-foreignKey for example:

Kinder:

public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('kinder');
        $this->setDisplayField('vorname');
        $this->setPrimaryKey('id');

        $this->belongsToMany('Kunden', [
            'foreignKey' => 'kinder_id',
            'targetForeignKey' => 'kunden_id',
            'joinTable' => 'kinder_kunden',
        ]);
    }

and

Kunden:

public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('kunden');
        $this->setDisplayField('vorname');
        $this->setPrimaryKey('id');

        $this->belongsToMany('Kinder', [
            'foreignKey' => 'kunden_id',
            'targetForeignKey' => 'kinder_id',
            'joinTable' => 'kinder_kunden',
        ]);
    }

It has to be kind_id and kunde_id.

CakePHP Version

5.0.8

PHP Version

8.3

markstory commented 1 month ago

Foreign key names are generated with Inflector::singular. You'll need more inflection rules. See https://github.com/cakephp/bake/blob/976dbe36a42feaf6d32163954729220e5cc21f3f/src/Command/ModelCommand.php#L600-L620 and https://github.com/cakephp/cakephp/blob/c708617aa89b8aa6145c5296eb7113033d37a6ef/src/Core/ConventionsTrait.php#L60

Manuel1948 commented 1 month ago

Foreign key names are generated with Inflector::singular. You'll need more inflection rules. See https://github.com/cakephp/bake/blob/976dbe36a42feaf6d32163954729220e5cc21f3f/src/Command/ModelCommand.php#L600-L620 and

https://github.com/cakephp/cakephp/blob/c708617aa89b8aa6145c5296eb7113033d37a6ef/src/Core/ConventionsTrait.php#L60

thank you, didn't recognized it because there is not so much documentation about the inflectors.