Doctrine: ManyToMany relation with attributes or positions

Refresh

February 2019

Views

4.1k time

1

I have those Entitys:

First: ProductCupMain -> like a Product

<?php

namespace xxx\Security\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * xxx\Security\Entity\ProductCupMain
 *
 * @ORM\Table(name="product_cup_main")
 * @ORM\Entity
 */
class ProductCupMain
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=45, nullable=true)
     */
    private $name;

    /**
     * @var string $image
     *
     * @ORM\Column(name="image", type="string", length=128, nullable=true)
     */
    private $image;

    /**
     * @var string $pdf
     *
     * @ORM\Column(name="pdf", type="string", length=128, nullable=true)
     */
    private $pdf;

    /**
     * @var ProductCategories
     *
     * @ORM\ManyToMany(targetEntity="ProductCategories", inversedBy="productCupMain")
     * @ORM\JoinTable(name="product_cup_main_has_product_categories",
     *   joinColumns={
     *     @ORM\JoinColumn(name="product_cup_main_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="product_categories_id", referencedColumnName="id")
     *   }
     * )
     */
    private $productCategories;

Secound: ProductCategories -> like Categories

<?php

namespace xxx\Security\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * xxx\Security\Entity\ProductCategories
 *
 * @ORM\Table(name="product_categories")
 * @ORM\Entity
 */
class ProductCategories
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=45, nullable=false)
     */
    private $name;

    /**
     * @var string $image
     *
     * @ORM\Column(name="image", type="string", length=100, nullable=true)
     */
    private $image;

    /**
     * @var string $shortname
     *
     * @ORM\Column(name="shortname", type="string", length=164, nullable=false)
     */
    private $shortname;

    /**
     * @var integer $position
     *
     * @ORM\Column(name="position", type="integer", nullable=false)
     */
    private $position;

    /**
     * @var ProductCupMain
     *
     * @ORM\ManyToMany(targetEntity="ProductCupMain", mappedBy="productCategories")
     */
    private $productCupMain;

So, i want to save Many products in Many categories. (ManyToMany relation) My problem is a position. The product have different positions in the categories.

I want to save the position on the relation, something like this:

http://i.stack.imgur.com/Z0gzw.jpg

I need a method like: getPositionInCategory($categoryId) where i can get the right position. Can you help me ?

Further, i had found a similar problem here, but i cant get the right solution for me.

EDIT 1

Solution from @mbinette: I've created a third entity for the reference. Is it right ?

//ProductCategoryReference

<?php

namespace xxx\Security\Entity;

use Doctrine\ORM\Mapping as ORM;

    /**
     * xxx\Security\Entity\ProductCategoryReference
     *
     * @ORM\Table(name="product_cup_main_has_product_categories")
     * @ORM\Entity
     */
    class ProductCategoryReference
    {

        /**
         * @ORM\ManyToOne(targetEntity="ProductCupMain", inversedBy="categoriesHavetheProduct") 
         */
        private $prductCupMain;

        /**
         * @ORM\ManyToOne(targetEntity="ProductCategories", inversedBy="productsInCategory")
         */
        private $productsCategorie; 

        /**
         * @ORM\Column(name="postionInCategorie", type="integer", length=164, nullable=false)
         */
        private $postionInCategorie;


        public function getProductCupMain()
        {
            return $this->prductCupMain;
        }

        public function setProductCupMain($prductCupMain)
        {
            $this->prductCupMain = $prductCupMain;
        }

        public function getProductsCategorie()
        {
            return $this->productsCategorie;
        }

        public function setProductsCategorie($productsCategorie)
        {
            $this->productsCategorie = $productsCategorie;
        }

        public function getPostionInCategorie()
        {
            return $this->productsCategorie;
        }

        public function setPostionInCategorie($postionInCategorie)
        {
            $this->postionInCategorie = $postionInCategorie;
        }

    }

But what is with the product entity and the category entity ?

Product Entity:

/**
 * @var CategoryHavetheProduct
 * 
 * @OneToMany(targetEntity="ProductCategoryReference", mappedBy="productCupMain")
 * 
**/
   private $categoriesHavetheProduct;

   //Please show me the getter and setter methods

Categorie Entity:

/**
 * @var ProductsInCategory
 * 
 * @OneToMany(targetEntity="ProductCategoryReference", mappedBy="productsCategorie")
 * 
 **/
private $productsInCategory;

//Please show me the getter and setter methods

1 answers

4

There is no way to add more attributes to a Dotrine/Doctrine2 entity relation. If you need to save more data related to a relation (other than the entities involved), then it's a little bit more than a simple relation, isn't it? ;-)

What you can do is exactly what you've done in your diagram - create 3 different entities, which are linked by ManyToOne/OneToMany relations. Then you can customize that entity and add the information/responsabilities you need.

EDIT

By position, you do mean a position set by you, right? Or you mean you want to keep the order in which they were added to your collection? If so, there are some unresolved tickets about it (ticket 1 and ticket 2), so maybe we'll see that in the future. For now, I think you'll have to stick with the RelationshipEntity (3 entities).

Hope this helps.