简体   繁体   English

Doctrine:ManyToMany与属性或职位的关系

[英]Doctrine: ManyToMany relation with attributes or positions

I have those Entitys: 我有这些实体:

First: ProductCupMain -> like a Product 第一:ProductCupMain - >像产品一样

<?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 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. (ManyToMany关系)我的问题是一个职位。 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 http://i.stack.imgur.com/Z0gzw.jpg

I need a method like: getPositionInCategory($categoryId) where i can get the right position. 我需要一个方法,如:getPositionInCategory($ categoryId),我可以得到正确的位置。 Can you help me ? 你能帮助我吗 ?

Further, i had found a similar problem here , but i cant get the right solution for me. 此外,我在这里发现了类似的问题,但我无法为我找到合适的解决方案。

EDIT 1 编辑1

Solution from @mbinette: I've created a third entity for the reference. 来自@mbinette的解决方案:我已经为参考创建了第三个实体。 Is it right ? 这样对吗 ?

//ProductCategoryReference // 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

There is no way to add more attributes to a Dotrine/Doctrine2 entity relation. 无法向Dotrine / Doctrine2实体关系添加更多属性。 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. 您可以做的就是您在图表中完成的工作 - 创建3个不同的实体,这些实体由ManyToOne / OneToMany关系链接。 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. 如果是这样,有一些未解决的门票( 门票1门票2 ),所以也许我们将来会看到它。 For now, I think you'll have to stick with the RelationshipEntity (3 entities). 现在,我认为你必须坚持使用RelationshipEntity(3个实体)。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM