【问题标题】:Relationships in Doctrine教义中的关系
【发布时间】:2018-08-08 03:33:51
【问题描述】:

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

一个产品可以属于多个类别,这怎么办?我认为这是多对多关系,但我无法正确建立关系。 ..................................................... ..................................................... ..................................................... ......

【问题讨论】:

  • 首先最好将 Items 重命名为 Item,因为这是一个实体。之后,您应该在 Item 中创建一个与 Category 相关的属性并将其映射到 yml 中(双向可能是更好的选择)。之后,创建作为 ArrayCollection 实例的属性,以存储 Category 中的 Item 集合。 (几个小时后,我会在电脑前为您提供更多帮助)
  • @l13,谢谢,我会尝试自己做一些事情,但我不会拒绝你的帮助。
  • @MathieuDormeval,谢谢,但这不是我所需要的

标签: symfony doctrine-orm yaml relationship any


【解决方案1】:

在您的类别 yml 中添加:

oneToMany:
    items:
        targetEntity: Namespace\TO\YOUR\Entity\Item
        mappedBy: category

在你的 Item 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();
}

【讨论】:

  • 映射时出错:Invalid mapping file 'AppBundle.Entity.Item.orm.yml' for class 'AppBundle\Entity\Item'. My Entity.Item.orm.yml:AppBundle\Entity\Items: type: entity manyToOne: category: targetEntity: AppBundle\Entity\Category inversedBy: items joinColumn: name: category_id referencedColumn: id
  • 也许我需要添加joinTable 属性?
  • 如果你的实体没有's',这里应该是 -> AppBundle\Entity\Item: on you orm.yml
  • 但是如何让一个产品属于多个类别呢?
猜你喜欢
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
相关资源
最近更新 更多