1
0
mirror of synced 2024-12-15 15:46:02 +03:00
doctrine2/manual/en/association-mapping.txt

464 lines
17 KiB
Plaintext

This chapter explains how associations between entities are mapped with Doctrine. We start out with an explanation of the concept of owning and inverse sides which is important to understand when working with bidirectional associations. Please read these explanations carefully.
++ Owning Side and Inverse Side
When mapping bidirectional associations it is important to understand the concept of the owning and inverse sides. The following general rules apply:
* Relationships may be bidirectional or unidirectional.
* A bidirectional relationship has both an owning side and an inverse side.
* A unidirectional relationship only has an owning side.
* The owning side of a relationship determines the updates to the relationship in the database.
The following rules apply to *bidirectional* associations:
* The inverse side of a bidirectional relationship must refer to its owning side by use of the mappedBy attribute of the OneToOne, OneToMany, or ManyToMany mapping declaration. The mappedBy attribute designates the field in the entity that is the owner of the relationship.
* The owning side of a bidirectional relationship must refer to its inverse side by use of the inversedBy attribute of the OneToOne, ManyToOne, or ManyToMany mapping declaration. The inversedBy attribute designates the field in the entity that is the inverse side of the relationship.
* The many side of OneToMany/ManyToOne bidirectional relationships *must* be the owning side, hence the mappedBy element can not be specified on the ManyToOne side.
* For OneToOne bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key (@JoinColumn(s)).
* For ManyToMany bidirectional relationships either side may be the owning side (the side that defines the @JoinTable and/or does not make use of the mappedBy attribute, thus using a default join table).
Especially important is the following:
**The owning side of a relationship determines the updates to the relationship in the database**.
To fully understand this, remember how bidirectional associations are maintained
in the object world. There are 2 references on each side of the association
and these 2 references both represent the same association but can change
independently of one another. Of course, in a correct application the semantics
of the bidirectional association are properly maintained by the application
developer (that's his responsibility). Doctrine needs to know which of
these two in-memory references is the one that should be persisted and which
not. This is what the owning/inverse concept is mainly used for.
**Changes made only to the inverse side of an association are ignored. Make sure to update both sides of a bidirectional association (or at least the owning side, from Doctrine's point of view)**
The owning side of a bidirectional association is the side Doctrine "looks at" when determining
the state of the association, and consequently whether there is anything to do to update the association
in the database.
> **NOTE**
> "Owning side" and "inverse side" are technical concepts of the ORM technology, not concepts
> of your domain model. What you consider as the owning side in your domain model can be different
> from what the owning side is for Doctrine. These are unrelated.
++ Collections
In all the examples of many-valued associations in this manual we will make use of a `Collection` interface and a corresponding default implementation `ArrayCollection` that are defined in the `Doctrine\Common\Collections` namespace. Why do we need that? Doesn't that couple my domain model to Doctrine? Unfortunately, PHP arrays, while being great for many things, do not make up for good collections of business objects, especially not in the context of an ORM. The reason is that plain PHP arrays can not be transparently extended / instrumented in PHP code, which is necessary for a lot of advanced ORM features. The classes / interfaces that come closest to an OO collection are ArrayAccess and ArrayObject but until instances of these types can be used in all places where a plain array can be used (something that may happen in PHP6) their useability is fairly limited. You "can" type-hint on `ArrayAccess` instead of `Collection`, since the Collection interface extends `ArrayAccess`, but this will severely limit you in the way you can work with the collection, because the `ArrayAccess` API is (intentionally) very primitive and more importantly because you can not pass this collection to all the useful PHP array functions, which makes it very hard to work with.
> **CAUTION**
> The Collection interface and ArrayCollection class, like everything else in the
> Doctrine\Common namespace, are neither part of the ORM, nor the DBAL, it is a plain PHP
> class that has no outside dependencies apart from dependencies on PHP itself (and the
> SPL). Therefore using this class in your domain classes and elsewhere does not introduce
> a coupling to the persistence layer. The Collection class, like everything else in the
> Common namespace, is not part of the persistence layer. You could even copy that class
> over to your project if you want to remove Doctrine from your project and all your
> domain classes will work the same as before.
++ Mapping Defaults
The @JoinColumn and @JoinTable definitions are usually optional and have sensible default values. The defaults for a join column in a one-to-one/many-to-one association is as follows:
name: "<fieldname>_id"
referencedColumnName: "id"
As an example, consider this mapping:
[php]
/** @OneToOne(targetEntity="Shipping") */
private $shipping;
This is essentially the same as the following, more verbose, mapping:
[php]
/**
* @OneToOne(targetEntity="Shipping")
* @JoinColumn(name="shipping_id", referencedColumnName="id")
*/
private $shipping;
The @JoinTable definition used for many-to-many mappings has similar defaults. As an example, consider this mapping:
[php]
class User {
//...
/** @ManyToMany(targetEntity="Group") */
private $groups;
//...
}
This is essentially the same as the following, more verbose, mapping:
[php]
class User {
//...
/**
* @ManyToMany(targetEntity="Group")
* @JoinTable(name="User_Group",
* joinColumns={@JoinColumn(name="User_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="Group_id", referencedColumnName="id")}
* )
*/
private $groups;
//...
}
In that case, the name of the join table defaults to a combination of the simple, unqualified class names of the participating classes, separated by an underscore character. The names of the join columns default to the simple, unqualified class name of the targeted class followed by "_id". The referencedColumnName always defaults to "id", just as in one-to-one or many-to-one mappings.
If you accept these defaults, you can reduce the mapping code to a minimum.
++ One-To-One, Unidirectional
A unidirectional one-to-one association is very common. Here is an example of a `Product` that has one `Shipping` object associated to it. The `Shipping` side does not reference back to the `Product` so it is unidirectional.
[php]
/** @Entity */
class Product
{
// ...
/**
* @OneToOne(targetEntity="Shipping")
* @JoinColumn(name="shipping_id", referencedColumnName="id")
*/
private $shipping;
// ...
}
/** @Entity */
class Shipping
{
// ...
}
Note that the @JoinColumn is not really necessary in this example, as the defaults would be the same.
++ One-To-One, Bidirectional
Here is a one-to-one relationship between a `Customer` and a `Cart`. The `Cart`
has a reference back to the `Customer` so it is bidirectional.
[php]
/** @Entity */
class Customer
{
// ...
/**
* @OneToOne(targetEntity="Cart", mappedBy="customer")
*/
private $cart;
// ...
}
/** @Entity */
class Cart
{
// ...
/**
* @OneToOne(targetEntity="Customer", inversedBy="cart")
* @JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
// ...
}
Note that the @JoinColumn is not really necessary in this example, as the defaults would be the same.
++ One-To-One, Self-referencing
You can easily have self referencing one-to-one relationships like below.
[php]
/** @Entity */
class Customer
{
// ...
/**
* @OneToOne(targetEntity="Customer")
* @JoinColumn(name="mentor_id", referencedColumnName="id")
*/
private $mentor;
// ...
}
Note that the @JoinColumn is not really necessary in this example, as the defaults would be the same.
++ One-To-Many, Unidirectional with Join Table
A unidirectional one-to-many association can be mapped through a join table. From Doctrine's point of view, it is simply mapped as a unidirectional many-to-many whereby a unique constraint on one of the join columns enforces the one-to-many cardinality.
The following example sets up such a unidirectional one-to-many association:
[php]
/** @Entity */
class User
{
// ...
/**
* @ManyToMany(targetEntity="Phonenumber")
* @JoinTable(name="users_phonenumbers",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="phonenumber_id", referencedColumnName="id", unique=true)}
* )
*/
private $phonenumbers;
// ...
}
/** @Entity */
class Phonenumber
{
// ...
}
> **NOTE**
> One-To-Many uni-directional relations with join-table only work using the @ManyToMany annotation and a unique-constraint.
++ Many-To-One, Unidirectional
You can easily implement a many-to-one unidirectional association with the following:
[php]
/** @Entity */
class User
{
// ...
/**
* @ManyToOne(targetEntity="Address")
* @JoinColumn(name="address_id", referencedColumnName="id")
*/
private $address
}
/** @Entity */
class Address
{
// ...
}
> **TIP**
> The above `@JoinColumn` is optional as it would default to `address_id` and `id`
> anyways. You can omit it and let it use the defaults.
++ One-To-Many, Bidirectional
Bidirectional one-to-many associations are very common. The following code shows an example with a Product and a Feature class:
[php]
/** @Entity */
class Product
{
// ...
/**
* @OneToMany(targetEntity="Feature", mappedBy="product")
*/
private $features;
// ...
}
/** @Entity */
class Feature
{
// ...
/**
* @ManyToOne(targetEntity="Product", inversedBy="features")
* @JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
// ...
}
Note that the @JoinColumn is not really necessary in this example, as the defaults would be the same.
++ One-To-Many, Self-referencing
You can also setup a one-to-many association that is self-referencing. In this example we
setup a hierarchy of `Category` objects by creating a self referencing relationship.
This effectively models a hierarchy of categories and from the database perspective is known as an adjacency list approach.
[php]
/** @Entity */
class Category
{
// ...
/**
* @OneToMany(targetEntity="Category", mappedBy="parent")
*/
private $children;
/**
* @ManyToOne(targetEntity="Category", inversedBy="children")
* @JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
// ...
}
Note that the @JoinColumn is not really necessary in this example, as the defaults would be the same.
++ Many-To-Many, Unidirectional
Real many-to-many associations are less common. The following example shows a unidirectional association between User and Group entities:
[php]
/** @Entity */
class User
{
// ...
/**
* @ManyToMany(targetEntity="Group")
* @JoinTable(name="users_groups",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
private $groups;
// ...
}
/** @Entity */
class Group
{
// ...
}
> **NOTE**
> Why are many-to-many associations less common? Because frequently you want to associate
> additional attributes with an association, in which case you introduce an association
> class. Consequently, the direct many-to-many association disappears and is replaced
> by one-to-many/many-to-one associations between the 3 participating classes.
++ Many-To-Many, Bidirectional
Here is a similar many-to-many relationship as above except this one is bidirectional.
[php]
/** @Entity */
class User
{
// ...
/**
* @ManyToMany(targetEntity="Group", inversedBy="users")
* @JoinTable(name="users_groups",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
private $groups;
// ...
}
/** @Entity */
class Group
{
// ...
/**
* @ManyToMany(targetEntity="User", mappedBy="groups")
*/
private $users;
// ...
}
++ Many-To-Many, Self-referencing
You can even have a self-referencing many-to-many association. A common scenario is where a `User` has friends and the target entity of that relationship is a `User` so it is self referencing. In this example it is bidirectional so `User` has a field named `$friendsWithMe` and `$myFriends`.
[php]
/** @Entity */
class User
{
// ...
/**
* @ManyToMany(targetEntity="User", mappedBy="myFriends")
*/
private $friendsWithMe;
/**
* @ManyToMany(targetEntity="User", inversedBy="friendsWithMe")
* @JoinTable(name="friends",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="friend_user_id", referencedColumnName="id")}
* )
*/
private $myFriends;
// ...
}
++ Ordering To-Many Collections
In many use-cases you will want to sort collections when they are retrieved from the database.
In userland you do this as long as you haven't initially saved an entity with its associations
into the database. To retrieve a sorted collection from the database you can use the
`@OrderBy` annotation with an collection that specifies an DQL snippet that is appended
to all queries with this collection.
Additional to any `@OneToMany` or `@ManyToMany` annotation you can specify the `@OrderBy`
in the following way:
[php]
/** @Entity */
class User
{
// ...
/**
* @ManyToMany(targetEntity="Group")
* @OrderBy({"name" = "ASC"})
*/
private $groups;
}
The DQL Snippet in OrderBy is only allowed to consist of unqualified,
unquoted field names and of an optional ASC/DESC positional statement.
Multiple Fields are separated by a comma (,). The referenced field
names have to exist on the `targetEntity` class of the `@ManyToMany` or
`@OneToMany` annotation.
The semantics of this feature can be described as follows.
* `@OrderBy` acts as an implicit ORDER BY clause for the given fields, that is appended
to all the explicitly given ORDER BY items.
* All collections of the ordered type are always retrieved in an ordered fashion.
* To keep the database impact low, these implicit ORDER BY items are only added
to an DQL Query if the collection is fetch joined in the DQL query.
Given our previously defined example, the following would not add ORDER BY, since g is not fetch joined:
[sql]
SELECT u FROM User u JOIN u.groups g WHERE SIZE(g) > 10
However the following:
[sql]
SELECT u FROM User u JOIN u.groups g WHERE u.id = 10
...would internally be rewritten to:
[sql]
SELECT u FROM User u JOIN u.groups g WHERE u.id = 10 ORDER BY g.name ASC
You can't reverse the order, an explicit:
[sql]
SELECT u FROM User u JOIN u.groups g WHERE u.id = 10 ORDER BY g.name DESC
...is internally rewritten to:
[sql]
SELECT u FROM User u JOIN u.groups g WHERE u.id = 10 ORDER BY g.name DESC, g.name ASC