2

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:

https://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
Community
  • 1
  • 1
dennis
  • 286
  • 3
  • 9
  • 1
    FYI: it's not `categorie`, it's `category`. I know programming languages inflectors are not that smart, but please... :) – Tomasz Kowalczyk Sep 24 '12 at 12:10
  • Thank you! if the typing error is once there, it is difficult to remove it... but i correct it in my procject :-) – dennis Sep 24 '12 at 12:16

1 Answers1

5

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.

mbinette
  • 5,094
  • 3
  • 24
  • 32
  • Thank you, but if im generate the entitys automatically with doctrine, i get only 2 entitys not the third one. You say, i have to create a new one like: _ProductCupMainProductCategories_ map them with ManyToOne <--Third Entity--> OneToMany ? – dennis Sep 24 '12 at 12:29
  • @dennis: What mbinette told you is correct. You have to create manually your relational entity and tell doctrine what relationship live between them – DonCallisto Sep 24 '12 at 12:33
  • Please have a look on **EDIT 1** can you give me additional information ? – dennis Sep 24 '12 at 14:39
  • This is exactly it! If you end up having a lot of those kind of relationship entities, I would suggest you use a prefix for the entity classes (so they are easily identifiable... but the "Reference" suffix is good too!). PS: I would also define a method in Category to get the ProductCupMain objects directly (if you don't want to go through cat->reference->product everytime. Something like getProducts() which would build a collection with the products (in order!!!) and return it directly. No magic there, you'll have to loop through the references! ;-) – mbinette Sep 24 '12 at 16:23
  • Thank you very much!! Now it works! I correct my code in few minutes! – dennis Sep 24 '12 at 18:37