mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
DDC-881: DDC-117: Linked Objects with composite key #1093
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Nov 18, 2010).
Jira issue originally created by user felicitus:
I'm currently playing around with DDC-117. I came across a general
problem which occurs with relations.
Given the following two classes, should a
@OneToOne/@OneToMany/@ManyToMany relation consider a composite ID or is
that not planned with DDC-117?
In the past, we assumed that an object is identified unique with an @Id
column. However, if we support composite keys, an object needs to be
identified by two or more columns, which we need to take into
consideration when building queries and foreign keys. Is that correct?
I know this might become a bit tricky, because $content refers to a
single instance of Content, but we actually need two columns to identify
it. That's how it looks if generated with Doctrine2 (DDC-117):
mysql> describe Content;
-------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
-------------------------------------------------
| id | int(11) | NO | PRI | NULL | |
| version | int(11) | NO | PRI | NULL | |
| document_id | int(11) | YES | MUL | NULL | |
-------------------------------------------------
It would be nice if we could discuss this one, as I feel it is important for an ORM.
@doctrinebot commented on GitHub (Nov 18, 2010):
Comment created by @beberlei:
Hm i think this issue appears becaues "version" in your mapping is defined twice in the "Content" entity.
You have rename the join column and it should work. Howevr ClassMetadata should throw an exception.
@doctrinebot commented on GitHub (Nov 18, 2010):
Comment created by felicitus:
So in theory, doctrine should handle my example with DDC-117? Will try that out later with unique field names and report back. If that would work already, this would be amazing!
@doctrinebot commented on GitHub (Nov 18, 2010):
Comment created by @beberlei:
btw your mapping is somewhat wrong. you cannot have two @ManyToOne that connect the same two entities with each other. One has to be @OneToMany
@doctrinebot commented on GitHub (Nov 24, 2010):
Comment created by felicitus:
Benjamin, I finally had time to check out more things. In fact, Doctrine with DDC-117 completely ignores multiple foreign keys.
Even if I replace @ManyToOne with @OneToMany, and version with fversion in Content, Doctrine still only creates a reference (and columns!) with content_id only. I would have expected that that I find at least content_id and content_version within the Document table
I also tried to specify mappedBy="document,version", but this also didn't work.
So again the question: Is my initial example meant to be possible with DDC-117, or is it not?
@doctrinebot commented on GitHub (Nov 24, 2010):
Comment created by felicitus:
I just created a better test.
This is a very basic mapping example, where one user may have many phone numbers. Also we have a PhoneCall entity which stores the call made. Since a single phone number isn't just identified by id, but also by user_id, Doctrine should create two fields in PhoneCall for that relation: phonenumber_id and phonenumber_user_id, so that we can retrieve the PhoneNumber object by PhoneCall's phonenumber property.
If you agree that my example should be working, I'm willing to write a test case and see if I can contribute code to make that happen.
@doctrinebot commented on GitHub (Nov 24, 2010):
Comment created by felicitus:
I'm preparing a real-world test script; no need to respond for now.
@doctrinebot commented on GitHub (Nov 25, 2010):
Comment created by felicitus:
As promised, here's the real-world test script. Note that I adjusted the code for the example entities due to DDC-891.
During the flush, Doctrine fails with
Integrity constraint violation: 1062 Duplicate entry '2' for key 'PhoneCall*phonenumber_id*uniqbecause Doctrine does not create the composite primary key (which is id and user_id) as foreign key (Doctrine only adds id).
@doctrinebot commented on GitHub (Dec 28, 2010):
Comment created by @beberlei:
The problem here is your @OneToOne relation. It implicitly sets a unique index. The primary key generated by your mapping is ok.
Try to modify your code stating @OneToOne(unique=false)
Additionally you have to specifiy the join columns. You are using the default, which obviously doesn't work for a composite approach (that is not default):
That would be the right approach if there were not a bug in SchemaTool i need to fix for this to work :-)
@doctrinebot commented on GitHub (Dec 29, 2010):
Comment created by @beberlei:
This is a really tricky issue, we need to think about it a little longer.
@doctrinebot commented on GitHub (Jan 2, 2011):
Comment created by @beberlei:
Fixed.
@doctrinebot commented on GitHub (Jan 2, 2011):
Issue was closed with resolution "Fixed"