原则:与属性或位置的多对多关系

den*_*nis 2 php database orm doctrine symfony

我有这些实体:

第一: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;
Run Code Online (Sandbox Code Playgroud)

第二:ProductCategories -> 类似类别

<?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;
Run Code Online (Sandbox Code Playgroud)

所以,我想保存许多类别的许多产品。(多对多关系)我的问题是一个位置。该产品在类别中具有不同的位置。

我想保存关系上的位置,如下所示:

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

我需要一个像 getPositionInCategory($categoryId) 这样的方法,我可以在其中获得正确的位置。你能帮助我吗 ?

此外,我在这里发现了类似的问题,但我无法找到适合我的解决方案。

编辑1

@mbinette 的解决方案:我创建了第三个实体作为参考。这样对吗 ?

//产品类别参考

<?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;
        }

    }
Run Code Online (Sandbox Code Playgroud)

但是产品实体和类别实体是什么呢?

产品实体:

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

   //Please show me the getter and setter methods
Run Code Online (Sandbox Code Playgroud)

类别实体:

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

//Please show me the getter and setter methods
Run Code Online (Sandbox Code Playgroud)

mbi*_*tte 5

无法向 Dotrine/Doctrine2 实体关系添加更多属性。如果您需要保存更多与关系相关的数据(除了涉及的实体之外),那么它就不仅仅是一个简单的关系,不是吗?;-)

您可以做的正是您在图表中所做的 - 创建 3 个不同的实体,它们通过 ManyToOne/OneToMany 关系链接。然后您可以自定义该实体并添加您需要的信息/职责。

编辑

你说的位置,是指你自己设定的位置吧?或者您的意思是您想保留它们添加到您的收藏中的顺序?如果是这样,则有一些未解决的票证(票证 1票证 2),所以也许我们将来会看到这一点。现在,我认为您必须坚持使用RelationshipEntity(3 个实体)。

希望这可以帮助。