I think there is no information in nowhere about this issue, so I publish it here to discuss:
Suppose a record from a table with some field(s), part of a composite foreign key, having null value while the others fields of the composite foreign key having correct values. In this case Doctrine returnes a related object instead of ... nothing. I think it's an error because this situation can appear when a field belongs to more than one composite foreign key.
An fictious example to explain better:
Imagine this tables (* marks the primary key),
Person:
*id_person
name
surname
id_country
id_city
id_district
Country
*id_country
name
City
*id_country
*id_city
name
District
*id_country
*id_city
*id_district
name
We want district optional to Persons, so :
Country:
id_country name
34 Spain
52 México
... ...
> I think there is no information in nowhere about this issue, so I publish it here to discuss:
> Suppose a record from a table with some field(s), part of a composite foreign key, having null value while the others fields of the composite foreign key having correct values. In this case Doctrine returnes a related object instead of ... nothing. I think it's an error because this situation can appear when a field belongs to more than one composite foreign key.
I've never seen an association like this, using 3 @joinColumns. I don't think this is supported (but I could be wrong).
Why don't you try the following setup:
Person <- ManyToOne -> City
Person <- ManyToOne -> District
Country <- OneToMany -> City
City <- OneToMany -> Person
City <- ManyToOne -> Country
City <- OneToMany -> District
District <- ManyToOne -> Person
District <- ManyToOne -> City
So a District belongs to a City, and a City belongs to a Country.
A person has either a District or a City (always one of these, but never both). You can enforce this in the entity code.
In Person you can make the following convenience functions:
getCity() : If Person has related City, return that city. Else return the City of the related District.
getCountry() : If Person has related City, return Country of that City. Else return the Country of the City of the related District.
-- Jasper N. Brouwer
On 09-11-2012, at 08:33, Angel B.A. <ank...@gmail.com> wrote:
The associations in the real code posted in previous message works perfectly when I persists the related objects, only fails recovering the objects from the DB because Doctrine treats a composite foreign key as valid even if the values are incomplete, so it takes any object thats satisfies this "partial" foreign key. I think this should trigger an error or ignore the incomplete values, at the very least.
However, from the beginning I suspect that I am in a conceptual error, but I don't find it (and it isn't the hierarchichal relations among the Location entities). Furthermore, it's difficult to explain the work context who takes us to this association schema (Location tables are shared among different projects and we are creating this as a part of an "applications template" that will contain "Location facilities" for the programmers).
So I will take your suggestions as a point to rethink my approach. But I still think that Doctrine should, at the very least, ignore the "incomplete values" in a composite foreign key (or create a warn message) instead of returning some object.
Thank you very much for your time!
El viernes, 9 de noviembre de 2012 09:54:01 UTC+1, Jàπ (Jasper N. Brouwer) escribió:
> I've never seen an association like this, using 3 @joinColumns. I don't > think this is supported (but I could be wrong).
> Why don't you try the following setup:
> Person <- ManyToOne -> City > Person <- ManyToOne -> District
> Country <- OneToMany -> City
> City <- OneToMany -> Person > City <- ManyToOne -> Country > City <- OneToMany -> District
> District <- ManyToOne -> Person > District <- ManyToOne -> City
> So a District belongs to a City, and a City belongs to a Country. > A person has either a District or a City (always one of these, but never > both). You can enforce this in the entity code.
> In Person you can make the following convenience functions:
> getCity() : If Person has related City, return that city. Else return > the City of the related District. > getCountry() : If Person has related City, return Country of that City. > Else return the Country of the City of the related District.
I understand that if those "location entities" are a shared part of many projects, it can be hard/time-consuming to refactor this.
But I'm still not sure Doctrine can do what you want it to do.
Do you have the Doctrine console running? If so, could you do a:
$ doctrine orm:validate-schema
This way we can make sure Doctrine understands what you want ;)
-- Jasper N. Brouwer
On 09-11-2012, at 14:33, Angel B.A. <ank...@gmail.com> wrote:
> The associations in the real code posted in previous message works perfectly when I persists the related objects, only fails recovering the objects from the DB because Doctrine treats a composite foreign key as valid even if the values are incomplete, so it takes any object thats satisfies this "partial" foreign key. I think this should trigger an error or ignore the incomplete values, at the very least.
> However, from the beginning I suspect that I am in a conceptual error, but I don't find it (and it isn't the hierarchichal relations among the Location entities). Furthermore, it's difficult to explain the work context who takes us to this association schema (Location tables are shared among different projects and we are creating this as a part of an "applications template" that will contain "Location facilities" for the programmers).
> So I will take your suggestions as a point to rethink my approach. But I still think that Doctrine should, at the very least, ignore the "incomplete values" in a composite foreign key (or create a warn message) instead of returning some object.
(Excuse me, but I couldn't return to this theme for a lot of days)
This is not the point, the mappings are OK (perhaps the concept I use it isn't :-)
Previous to write in this group I dived deep in the doctrine code and I found that there is no check to the field's number of a composite foreign key when it builds the related object. It iterates all the fields and if ALL are null, then returns no object. But if ONE is not null it starts to build the SQL query, with no check about the number of conditions to build (that would be exactly the same number as fields number in the composite key, not less).
At the end, the question is:
Is correct to reutilize a field in two composite foreign keys?
If it is, then doctrine should return a null object if at least one field of the composite foreign key is null.
(For reference, the doctrine code is in createEntity function in doctrine\lib\Doctrine\ORM\UnitOfWork.php )