Conversation
|
Why are you creating OneToOne Mappings? ManyToOne is the better choice i think. I agree the reveres engineering could be better. |
There was a problem hiding this comment.
Please remove this and use ManyToOne instead of OneToOne.
There was a problem hiding this comment.
Why do you still need this? I think it can be removed, because of DatabaseDriver.php#L306
There was a problem hiding this comment.
Hello, sorry I am in a heavy development process, so cannot react very quickly.
In your current code (which did not support pk as fk in reverse -engineering) you checked if ONE-TO-ONE assoc consisted just of 1 column, and if yes, you set the property "unique" (which is quite correct) - see above (lines 886-888).
Now in my code, I added one more additional check - if ONE-TO-ONE assoc consists of 1 column AND it is also a primary key, we do not set the unique property, because it is not needed here (PK is always unique). I did not want to have both Id and Unique present for one field.
There was a problem hiding this comment.
But it would still be required to ensure data-consistency. If you have a combined primary key and one field is additionally OneToOne, then there should be a unique key on it by default.
There was a problem hiding this comment.
No, bcz of DatabaseDriver.php#L307
One-To-One mapping is created only if all columns that make a combined primary key, are used in association.
In your case it would create Many-To-One, and consistency would be ok. :)
There was a problem hiding this comment.
Ah ok, now i get it. Yes then the line is necessary obviously :-)
The OneToOne mapping is used because this is "a primary key that is in the same time a foreign key". You can of course use ManyToOne with "unique" parameter, if you want, but it is a little weird-looking. Also in your code ManyToOne is a wrapper over OneToOne So maybe to get rid of OneToOne mappings at all and use ManyToOne with unique parameter on all Joined Columns? |
|
Ah we are getting somewhere, ManyToOne or OneToOne depends on the following cases:
You could optimize the code in this direction. |
|
This makes a cocktail in my head :) One/Multiple columns in a foreign key is determined by JoinedColumns directive: OneToOne and ManyToOne is the association mapping. OneToOne means that there is only one record on our side (like only one address is available per user). Both associations can be made with 1 joined column, or with N joined columns - i mean number of joined columns do not have any influence on whether it is OneToOne or ManyToOne mapping. |
|
Ah yes, but you can catch this case when the foreign keys point to the same table. So the condition for onetoone is: Primary Key only has foreign keys pointing to a single table. For ManyToOne its:
|
Update: In MySQL this is doable
In that case we would have 2 separate OneToOne associations. create table table3 |
|
Huh? What about CREATE TABLE ProductATtributes (product_id INT, attribute_name VARCHAR, attribute_value VARCHAR, PRIMARY KEY(product_id, attribute_name)) that is 1 fk and 1 "normal" column on the table. |
|
In your CREATE TABLE statement you did not mention any foreign keys, only primary key. |
|
Let's look at such example: PRIMARY KEY(product_id, attribute_name) My code:
Is this what you wanted to know? |
|
Any updates on this? |
1 similar comment
|
Any updates on this? |
|
Sorry, i was busy the last weeks so i couldn't catch up. 'product_id' is the foreign key in my example. Ok good that your code supports this. Let me review then i'll merge. |
There was a problem hiding this comment.
please squash the lines and put } else { here.
Make Reverse Engineering to support Primary Keys as Foreign Keys.
The origin of the problem:
While parsing database Doctrine2 creates 2 separate arrays - "field mappings" and "association mappings".
If the field is within table foreign keys, it goes to "association mappings", otherwise it goes to "field mappings".
With primary keys as foreign keys Doctrine2 behaves incorrectly - it adds it both to "field mappings" and "association mappings", creating a nasty MappingException::duplicateFieldMapping
Reworked the code so it adds it only to the "association mappings", creating a OneToOne mapping type.
I wonder why so few attention is payed to reverse engineering in the project.
When you introspect database it only creates unidirectional mappings, so you have to manually create an
inverse in other entities.
In Doctrine 1.x it was much-much better.
Reverse engineering is not evil. Is a thing really very important in many situations when you a have a huge existing database structure.