学说中的关系
Relationships in Doctrine
我有 2 个实体:
类别和产品。
如何在这些实体之间建立关系?
类别实体:
class Category
{
private $id;
private $name;
private $parent;
public function getChildren()
{
return $this->children;
}
//setters and getters
}
项目实体:
class Items
{
private $id;
private $name;
private $price;
private $color;
private $size;
private $weight;
//getters and setters
}
类别 yml:
AppBundle\Entity\Category:
type: entity
oneToMany:
children:
targetEntity: AppBundle\Entity\Category
mappedBy: parent
orderBy:
name: ASC
manyToOne:
parent:
targetEntity: AppBundle\Entity\Category
inversedBy: children
joinColumn:
name: parentId
referencedColumn: id
table: category
repositoryClass: AppBundle\Repository\CategoryRepository
id:
id:
column: id
type: integer
id: true
generator:
generator:
strategy: AUTO
fields:
name:
type: string
lenght: 255
项目 yml:
AppBundle\Entity\Items:
type: entity
table: items
repositoryClass: AppBundle\Repository\ItemsRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 255
price:
type: integer
color:
type: string
length: 255
nullable: true
size:
type: integer
weight:
type: integer
一个商品可以属于多个分类,怎么办?我认为那是 ManyToMany 关系,但我无法正确建立关系。
..................................................... ..................................................... ..................................................... .........
在您的类别 yml 中添加:
oneToMany:
items:
targetEntity: Namespace\TO\YOUR\Entity\Item
mappedBy: category
在您的项目 yml 中添加:
manyToOne:
catregory:
targetEntity: Namespace\TO\YOUR\Entity\Category
inversedBy: items
joinColumn:
name: category_id
referencedColumn: id
添加您的项目实体:
/**
* @var Catregory
*/
protected $catregory;
public function setCategory(Catregory $catregory) {
$this->catregory = $catregory;
}
public function getCatregory() {
return $this->catregory;
}
添加您的类别实体:
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
.................
/**
* @var Collection of Item
*/
protected $items;
public function __construct() {
$this->items = new ArrayCollection();
}
public function getItems() {
return $this->items;
}
public function setItems(Collection $items) {
$this->items = $items;
}
public function addItem(Item $item) {
if (!$this->Items->contains($item)) {
$item->setCategory($this);
$this->items->add($item);
}
}
public function removeItem(Item $item) {
if ($this->items->contains($item)) {
$this->items->remove($item);
}
}
public function clearItems() {
$this->items->clear();
}
我有 2 个实体: 类别和产品。 如何在这些实体之间建立关系? 类别实体:
class Category
{
private $id;
private $name;
private $parent;
public function getChildren()
{
return $this->children;
}
//setters and getters
}
项目实体:
class Items
{
private $id;
private $name;
private $price;
private $color;
private $size;
private $weight;
//getters and setters
}
类别 yml:
AppBundle\Entity\Category:
type: entity
oneToMany:
children:
targetEntity: AppBundle\Entity\Category
mappedBy: parent
orderBy:
name: ASC
manyToOne:
parent:
targetEntity: AppBundle\Entity\Category
inversedBy: children
joinColumn:
name: parentId
referencedColumn: id
table: category
repositoryClass: AppBundle\Repository\CategoryRepository
id:
id:
column: id
type: integer
id: true
generator:
generator:
strategy: AUTO
fields:
name:
type: string
lenght: 255
项目 yml:
AppBundle\Entity\Items:
type: entity
table: items
repositoryClass: AppBundle\Repository\ItemsRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 255
price:
type: integer
color:
type: string
length: 255
nullable: true
size:
type: integer
weight:
type: integer
一个商品可以属于多个分类,怎么办?我认为那是 ManyToMany 关系,但我无法正确建立关系。 ..................................................... ..................................................... ..................................................... .........
在您的类别 yml 中添加:
oneToMany:
items:
targetEntity: Namespace\TO\YOUR\Entity\Item
mappedBy: category
在您的项目 yml 中添加:
manyToOne:
catregory:
targetEntity: Namespace\TO\YOUR\Entity\Category
inversedBy: items
joinColumn:
name: category_id
referencedColumn: id
添加您的项目实体:
/**
* @var Catregory
*/
protected $catregory;
public function setCategory(Catregory $catregory) {
$this->catregory = $catregory;
}
public function getCatregory() {
return $this->catregory;
}
添加您的类别实体:
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
.................
/**
* @var Collection of Item
*/
protected $items;
public function __construct() {
$this->items = new ArrayCollection();
}
public function getItems() {
return $this->items;
}
public function setItems(Collection $items) {
$this->items = $items;
}
public function addItem(Item $item) {
if (!$this->Items->contains($item)) {
$item->setCategory($this);
$this->items->add($item);
}
}
public function removeItem(Item $item) {
if ($this->items->contains($item)) {
$this->items->remove($item);
}
}
public function clearItems() {
$this->items->clear();
}