2
votes

I have 2 Doctrine 2 Entities: - Country (id, name) - Mapping (id, objectType, internalId, externalId)

I need association between Country and Mapping using following condition: Country.id = Mapping.internalId AND Mapping.objectType = 'country'

Can I implement this relation using YAML/XML mapping?

2

2 Answers

1
votes

This is currently not supported in Doctrine 2. Foreign keys for associations must map to the primary key and you cannot implement filters on the associations yet. Filters are scheduled for implementation in 2.1.

1
votes

You can implement this by way of Inheritance Mapping; your Country entity would be a sub-class of the Mapping entity. You could then load up a Country like so:

$em->getRepository('entity\name\of\Country')->findBy(array('internalId' => 1));

Carefully investigate what you really need from your objects. I've found Inheritance Mapping works really well in certain situations. You'll need to think carefully about your domain to see if that's the case here.