If you have watched Girls no (aaratto) at 123Movies.to, share your thought with us by commenting below.Q:
Doctrine 2 - Inferring association type from class name?
Is there any way to infer the association type (one-to-one, many-to-one, etc.) from the class name, without mapping the association explicitly?
I'm looking for something like:
class A
{
/**
* @OneToMany(associationType=AssociationType::ONE_TO_MANY)
*/
private $categories;
* @ManyToOne(associationType=AssociationType::MANY_TO_ONE)
private $category;
}
This would be very convenient when you have a class that represents an entity, but you want to explicitly specify the association types for each property, without having to map them explicitly.
A:
As per my comment above, you can use the annotations version of Doctrine 2's @JoinTable. The annotations version will allow you to set up the join table via annotations alone. It will use the association name as the table name.
Unfortunately, I'm not aware of any mapping tools that have the ability to auto-detect join table associations from the class name (at least not one that I have worked with). I do hope there is something out there though.
I found this topic very interesting, and decided to try and solve it myself. I ended up writing an annotation class called EntityInferredAssociation.
Using this class, you can define a getInferredAssociation() method that takes the name of the class and returns the association type.
You can use it like this:
class User {
* @EntityInferredAssociation(
* association="User.inferred",
* targetEntity="Foo",
* joinColumn="foo_id",
* joinTable="User_foo"
* )
private $
Related links:
Comments