Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 views
in Technique[技术] by (71.8m points)

symfony - Relationships in Doctrine

I have 2 Entities: Categories and products. How can I build a relationship between these Entities? Category Entity:

    class Category
    {
        private $id;
        private $name;
        private $parent;
        public function getChildren()
        {
            return $this->children;
        }
    //setters and getters
    }

Items Entity:

 class Items
    {

        private $id;
        private $name;
        private $price;
        private $color;
        private $size;
        private $weight;
    //getters and setters
    }

Category yml:

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

Items yml:

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

One product can belong to several categories, how can this be done? I think that is the ManyToMany relationship, but I can't build relations correctly. .....................................................................................................................................................................

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add in your Category yml:

oneToMany:
    items:
        targetEntity: NamespaceTOYOUREntityItem
        mappedBy: category

Add in your Item yml:

   manyToOne:
    catregory:
        targetEntity: NamespaceTOYOUREntityCategory
        inversedBy: items
        joinColumn:
            name: category_id
            referencedColumn: id

Add in your Item Entity:

    /**
 * @var Catregory
 */
protected $catregory;


public function setCategory(Catregory $catregory) {
    $this->catregory = $catregory;
}

public function getCatregory() {
    return $this->catregory;
}       

Add in your Category Entity:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...