Doctrine 2 ORM - Mapping a property with one of multiple target classes - doctrine-orm

In my ZF3 application I want to map an entity "User" having a property "plan" to another entity with could be one of the following classes "PlanA", "PlanB" or "PlanC" using Doctrine 2 ORM. A "User" can have one "Plan", a "Plan" can be associated with many "Users".
I looked into discriminator maps, but I am not sure that that's the right approach.
Any ideas/code sample for that?

Related

Determine if target entity is a foreign key in Doctrine filter

Doctrine passes a $targetEntity and $targetTableAlias to the filter constraint like so:
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{ ...
It appears that all entities pass through the filter when a query is being built. Is there any way to determine if $targetEntity is a foreign key or not? I'd like to write a filter that works on the "primary" entity (what Doctrine calls the "base" table) but not its foreign keys.
Your question is not very clear and I am not sure if this will answer your question, but in the Doctrine 2 documentation chapter 30. Filters you can read the following:
In the case of joined or single table inheritance, you always get passed the ClassMetadata of the inheritance root. This is necessary to avoid edge cases that would break the SQL when applying the filters.
So I would say the $targetEntity is always the root entity (so never a foreign key).
If this doesn't answer your question I would like to ask you to be a bit more specific on what exactly you want to do or what you want to find out.

doctrine2: reference the join-table in a many-to-many relation

I am using Symfony and Doctrine
This might be a simple question but i cannot seem to find the answer to it...
How can i reference the join-table of a many-to-many association in a querybuilder ?
I have 2 entities: Article and Tag with a many-to-many bidirectional relation
this creates the extra join-table :
article <-> article_tag <-> tag
I can reference the article table by selecting it from the Article Entity :
$this->_em->createQueryBuilder();
->select('a')
->from('Acme\DemoBundle\Entity\Article','a')
And i can reference the tag table by selecting it from the Tag Entity :
$this->_em->createQueryBuilder();
->select('t')
->from('Acme\DemoBundle\Entity\Tag','t')
Now how can i select things from the join-table ? There is no entity for it...
You cant interact with this table. Doctrine will handle it, and like Roger Guasch said on the comments, you have to create accessors to the corresponding entities.
If you need to interact directly with this entity, you must define it by yourself, threating the m-n table as an entity. Then you can make reference to it.
Hope it helps!

ManyToMany self-referenced with extra column in Doctrine

I have problem which is easy to solve with pure SQL but I need to solve it with Doctrine in YAML.
I have well known db tables users and friends. Where users table has primary key user_id and friends has friend_id and friend_with_id which uses user_id from table user. My problem starts when I need to add there one more column mood. With SQL I add to table friends onemore column named mood and it's done with doctrine I can't find any solution.
For better understanding I add db scheme:
I dont see any self-reference relationship here. Both friend_id and friend_with_id points to users.user_id. What I see is, two one-to-many relationships from the user side OR two many-to-one relationships from the friends side.
So, there will be two ArrayCollection objects (ManyToOne) in the Friend entity corresponding to friend_id and friends_with_id. Similarly two ArrayCollections (OneToMany) on the User entity, namely, myFriends and friendsWith.

Doctrine2: Uni-directional #OneToMany with foreign key?

I have a "Product" entity with many "Video" entities, and I only need a unidirectional #OneToMany with foreign key (one product, many videos). My Product-side "key" is not primary or unique, which is why I need it to be unidirectional (eg, "select * from videos where product_family = 2143")
I'm using Doctrine 2.1
Is there yet a way to do uni-directional #OneToMany with only a foreign-key in Doctrine 2.1? If not, soon?
UPDATE: I found a relevant quote from Roman Borschel on May 2010:
"this would need quite some special-case handling in many places. In the light that there are 2 reasonably good alternatives (mapping through a jointable or simply making the association bidirectional) we do not consider this something that really needs to be done."
Has this opinion by the Doctrine2 team changed?
OneToMany by design has the related ID on the "Many" side of the relationship. So to make the child table relate to the parent without an additional field in a join table is not possible.

One-to-many relation ship between 3 entities in Doctrine 2

I have three entities: User, Office and PhoneNumber. The user has many phone numbers, and the office has many phone numbers too.
The problem is how to represent these entities relations in Doctrine 2.
At first I tried to use bi-directional one-to-many associations
(User -> has many -> PhoneNumbers) (Office -> has many ->
PhoneNumbers), the PhoneNumber has two mapping fields, one for User
and anotherone for Office. This solution doesn't work since one of
the mapping foreign keys couldn't be null.
My second approach was to use two entities and one superclass for PhoneNumber. The PhoneNumber superclass has defined all common fields except the mapping field. Entities UserPhoneNumber and
OfficePhoneNumber extended the PhoneNumber entity and specified the
different mapping field and different table. (one table for OfficePhoneNumbers, anotherone for UserPhoneNumbers)
This solution actually works, but it is quite ugly to have 3
classes to represent one simple entity.
My third approach is to use uni-directional one-to-many mapping. This will eliminate the need of mapping field for the PhoneNumber entity. The problem is that when I use cascade remove for the many-to-many field, it violates the integrity constraint when deleting records.
When I omit the cascade remove option, after removing User or Office, the PhoneNumber remains in the Database (but the record in mapping table is removed).
What is the best way to handle this type of association?
Thanks
I finally solve the problem connected with (probably the nicest) solution 1). The problem was in misunderstanding of mappedBy attribute which should specify the entity field, not the database field.