学说中的关系

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学说中的关系相关的知识,希望对你有一定的参考价值。

我有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
    }

阳明海运类:

AppBundleEntityCategory:
  type: entity
  oneToMany:
        children:
            targetEntity: AppBundleEntityCategory
            mappedBy: parent
            orderBy:
                name: ASC
  manyToOne:
        parent:
            targetEntity: AppBundleEntityCategory
            inversedBy: children
            joinColumn:
                name: parentId
                referencedColumn: id
  table: category
  repositoryClass: AppBundleRepositoryCategoryRepository
  id:
      id:
          column: id
          type: integer
          id: true
          generator:
          generator:
              strategy: AUTO
  fields:
      name:
          type: string
          lenght: 255

物品回来:

AppBundleEntityItems:
    type: entity
    table: items
    repositoryClass: AppBundleRepositoryItemsRepository
    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: NamespaceTOYOUREntityItem
        mappedBy: category

添加你的项目yml:

   manyToOne:
    catregory:
        targetEntity: NamespaceTOYOUREntityCategory
        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 DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection; 

.................

/**
 * @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();
}

以上是关于学说中的关系的主要内容,如果未能解决你的问题,请参考以下文章

学说中的关系

关系数据库中的作者、文章、杂志关系 [学说]

Symfony学说中的许多关系

跨数据库关系学说2和PHP

如何通过学说实体之间的 ID 建立一对一的关系

ORM(学说)和SQL之间的代码比较?