原文地址:http://docs.doctrine-project.org/en/latest/reference/association-mapping.html
注:本文只针对本人使用到的关系类型进行翻译,未使用到的以后有空再补上。
5. Association Mapping
This chapter explains mapping associations between objects.
这一章节介绍雷竞技苹果下载地址之间的关系映射。
Instead of working with foreign keys in your code, you will always work with references to objects instead and Doctrine will convert those references to foreign keys internally.
你将使用雷竞技苹果下载地址关联(references)和Doctrine替代你原来代码中数据库外键的工作。
A reference to a single object is represented by a foreign key.
一个关联到单个雷竞技苹果下载地址对应一个数据库的外键。
A collection of objects is represented by many foreign keys pointing to the object holding the collection
一个雷竞技苹果下载地址的集合代表许多外键指向这个雷竞技苹果下载地址所在的集合。
This chapter is split into three different sections.
本章分为三个不同的部分:
A list of all the possible association mapping use-cases is given.
所有可用的雷竞技苹果下载地址映射关系列表
Mapping Defaults are explained that simplify the use-case examples.
默认映射介绍简单的使用实例。
Collections are introduced that contain entities in associations.
集合介绍链接实体映射。
To gain a full understanding of associations you should also read about owning and inverse sides of associations
如果需要完全了解雷竞技苹果下载地址映射,你可以阅读owning and inverse sides of associations。
目录
5.1. Many-To-One, Unidirectional 多对一,单向(用户 对 地址)
5.2. One-To-One, Unidirectional 一对一,单向()
5.3. One-To-One, Bidirectional 一对一,双向(顾客 对 购物车)
5.4. One-To-One, Self-referencing 一对一,自指向
5.5. One-To-Many, Bidirectional 一对多,双向(产品 对 特点)
5.6. One-To-Many, Unidirectional with Join Table 一对多,使用表的单向
5.7. One-To-Many, Self-referencing 一对多,自指向(层级分类、文章栏目分类)
5.8. Many-To-Many, Unidirectional 多对多,单向
5.9. Many-To-Many, Bidirectional 多对多,双向
5.9.1. Owning and Inverse Side on a ManyToMany association
5.10. Many-To-Many, Self-referencing 多对多,自指向
5.11. Mapping Defaults 默认映射
5.12. Collections 集合
5.13. Initializing Collections 初始化集合
PS:
一对多和多对一底层数据库结构完全一样,都是在[多]方创建外键;
一对多只提供[一]方对[多]方的访问,[多]方没有[一]方的引用.如果有,就变成双向关联了;
只需在[一]方配置,[多]方不需要;
5.1. Many-To-One, Unidirectional
5.1. 多对一,单向
A many-to-one association is the most common association between objects.
多对一映射是非常常见的雷竞技苹果下载地址关系。
<?php
/** @Entity **/
class User
{
// ...
/**
* @ManyToOne(targetEntity="Address")
* @JoinColumn(name="address_id", referencedColumnName="id")
**/
private $address;
}
/** @Entity **/
class Address
{
// ...
}
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.
上述代码中的 @JoinColumn 是设置默认映射使用User.address_id关联到Address.id,你可以删除它,以使用默认值。
Generated MySQL Schema:
生成的 MySQL 代码:
CREATE TABLE User (
id INT AUTO_INCREMENT NOT NULL,
address_id INT DEFAULT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE Address (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE User ADD FOREIGN KEY (address_id) REFERENCES Address(id);
5.7. One-To-Many, Self-referencing
5.7. 一对多,自指向
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.
你可以设置自指向的关系映射。在本例中,将创建一个使用自指向的层级的分类雷竞技苹果下载地址映射关系。可以在数据库的关系视图中看到他们的层级关系。
PS:这个关系的典型应用应该是文章的栏目分类、用户角色等树状结构。
<?php
/** @Entity **/
class Category
{
// ...
/**
* @OneToMany(targetEntity="Category", mappedBy="parent")
**/
private $children;
/**
* @ManyToOne(targetEntity="Category", inversedBy="children")
* @JoinColumn(name="parent_id", referencedColumnName="id")
**/
private $parent;
// ...
public function __construct() {
$this->children = new DoctrineCommonCollectionsArrayCollection();
}
}
Note that the @JoinColumn is not really necessary in this example, as the defaults would be the same.
例子中的 @JoinColumn 可不写,默认值也是这样。
Generated MySQL Schema:
生成的 MySQL 代码:
CREATE TABLE Category (
id INT AUTO_INCREMENT NOT NULL,
parent_id INT DEFAULT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE Category ADD FOREIGN KEY (parent_id) REFERENCES Category(id);