-1

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:

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

Items 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

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. .....................................................................................................................................................................

Dialkord
  • 111
  • 1
  • 3
  • 12
  • Please have a look to https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md – Weenesta - Mathieu Dormeval Feb 28 '18 at 09:57
  • First is good to rename Items to Item because this is a entity. After that you should create a property in Item that is related to Category and mapping it in yml (bidirectional may be the better choice). After that create property that is instance from ArrayCollection to store collection of Item in Category. (After few hours I will be at computer and can help you more) – l13 Feb 28 '18 at 09:58
  • @l13, Thanks, I'll try to do something myself, but I will not refuse your help. – Dialkord Feb 28 '18 at 10:11
  • @MathieuDormeval, thanks, but it's not exactly what I need – Dialkord Feb 28 '18 at 10:12

1 Answers1

1

Add in your Category yml:

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

Add in your Item yml:

   manyToOne:
    catregory:
        targetEntity: Namespace\TO\YOUR\Entity\Category
        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 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();
}
l13
  • 529
  • 3
  • 11
  • Have an error while mapping:`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` – Dialkord Feb 28 '18 at 15:49
  • Maybe I need to add `joinTable` property? – Dialkord Feb 28 '18 at 15:52
  • if your entity is withous 's' here should be -> AppBundle\Entity\Item: on you orm.yml – l13 Feb 28 '18 at 15:55
  • but how to make one product belong to a multitude of categories? – Dialkord Feb 28 '18 at 17:31