1
0
mirror of synced 2024-12-14 15:16:04 +03:00

More association mapping yml+xml code blocks

This commit is contained in:
Benjamin Eberlei 2011-06-12 23:43:29 +02:00
parent c187862e4f
commit 41600667c2

View File

@ -915,35 +915,70 @@ Real many-to-many associations are less common. The following
example shows a unidirectional association between User and Group
entities:
.. code-block:: php
.. configuration-block::
<?php
/** @Entity */
class User
{
// ...
.. code-block:: php
/**
* @ManyToMany(targetEntity="Group")
* @JoinTable(name="users_groups",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
private $groups;
<?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;
public function __construct() {
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
// ...
public function __construct() {
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
}
}
/** @Entity */
class Group
{
// ...
}
/** @Entity */
class Group
{
// ...
}
.. code-block:: xml
<doctrine-mapping>
<entity name="User">
<many-to-many field="groups" target-entity="Group">
<join-table name="users_groups">
<join-columns>
<join-column="user_id" referenced-column-name="id" />
</join-columns>
<inverse-join-columns>
<join-column="group_id" referenced-column-name="id" />
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
</doctrine-mapping>
.. code-block:: yaml
User:
type: entity
manyToMany:
groups:
targetEntity: Group
joinTable:
name: users_groups
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
group_id:
referencedColumnName: id
Generated MySQL Schema:
@ -980,45 +1015,89 @@ Many-To-Many, Bidirectional
Here is a similar many-to-many relationship as above except this
one is bidirectional.
.. code-block:: php
.. configuration-block::
<?php
/** @Entity */
class User
{
// ...
.. code-block:: php
/**
* @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;
<?php
/** @Entity */
class User
{
// ...
public function __construct() {
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
/**
* @ManyToMany(targetEntity="Group", inversedBy="users")
* @JoinTable(name="users_groups")
*/
private $groups;
public function __construct() {
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
// ...
}
/** @Entity */
class Group
{
// ...
/**
* @ManyToMany(targetEntity="User", mappedBy="groups")
*/
private $users;
/** @Entity */
class Group
{
// ...
/**
* @ManyToMany(targetEntity="User", mappedBy="groups")
*/
private $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
// ...
}
// ...
}
.. code-block::
<doctrine-mapping>
<entity name="User">
<many-to-many field="groups" inversed-by="users">
<join-table name="users_groups">
<join-columns>
<join-column="user_id" referenced-column-name="id" />
</join-columns>
<inverse-join-columns>
<join-column="group_id" referenced-column-name="id" />
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
<entity name="Group">
<many-to-many field="users" mapped-by="groups" />
</entity>
</doctrine-mapping>
.. code-block::
User:
type: entity
manyToMany:
groups:
targetEntity: Group
inversedBy: users
joinTable:
name: users_groups
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
group_id:
referencedColumnName: id
Group:
type: entity
manyToMany:
users:
targetEntity: User
mappedBy: groups
The MySQL schema is exactly the same as for the Many-To-Many
uni-directional case above.