propelorm / Propel2

Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP
http://propelorm.org/
MIT License
1.26k stars 399 forks source link

Generated from database table relations suddenly different #1739

Closed msetten closed 3 years ago

msetten commented 3 years ago

I usually generate propel code via the tool provider to create it from an existing database. Suddenly, code that used to work, doesn't work anymore, for example when joining tables in a query like:

$results = PlayerQuery::create()->join('Player.User')->find();

The relations are no longer the same even though the database hasn't change.

I compared the resulting files, and noticed the following difference in a TableMap file:

BEFORE:

public function buildRelations() {
$this->addRelation('User', '\\User', RelationMap::MANY_TO_ONE, array 

...

NOW:

public function buildRelations() {
$this->addRelation('UserRelatedByUserId', '\\User', RelationMap::MANY_TO_ONE, array

...

Has this change been on purpose? If so, how do I change my queries seeing that the documentation doesn't reflect any of these updates?

mringler commented 3 years ago

I am not aware of recent changes to the way relations are named.

The "EntityRelatedByForeignKeyName" naming schema is used, when the same table is referenced by more than one foreign key, and the entity name itself is not enough to identify the relation. Is there another relation from you Player table to the User table?

msetten commented 3 years ago

The "EntityRelatedByForeignKeyName" naming schema is used, when the same table is referenced by more than one foreign key, and the entity name itself is not enough to identify the relation. Is there another relation from you Player table to the User table?

No there is only one reference from Player to User. Though, I do have a relation the other way around to, so a User can have a default Player. It seems that I may have added the foreign key for that after the last code generation. But I find it a bit strange that a relationship the other way around also affects the naming schema for the other direction.

After having removed that foreign key from User to Player, the generated code works again with my original code.

mringler commented 3 years ago

Right, yes, I forgot this in my initial reply. Relation are registered on both sides, does not matter which sides holds the fk in a many to one relation. Makes perfect sense if you think about it. So if you check, there should be a relation like UsersRelatedByDefaultPlayerIdon your Player relations. It's an awkward name, but the best Propel can guess. I think it is correct to not just call them User and Users, as that would be horribly confusing.

In schema.xml, you can name the relations yourself through the phpName and refPhpName attributes of the foreign key element. But it sounds like you introduce changes to your database directly in SQL and then reverse engineer the schema.xml file, overriding any manual configuration. The recommended (and superior) way to introduce changes to your database is through migrations, which solves this problem.

msetten commented 3 years ago

Thanks for the help. I'll look into the use of migrations.

dereuromark commented 3 years ago

Closing then for now.