簡體   English   中英

Doctrine:ManyToMany與屬性或職位的關系

[英]Doctrine: ManyToMany relation with attributes or positions

我有這些實體:

第一: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

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

所以,我想在許多類別中保存許多產品。 (ManyToMany關系)我的問題是一個職位。 該產品在類別中有不同的位置。

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

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

我需要一個方法,如:getPositionInCategory($ categoryId),我可以得到正確的位置。 你能幫助我嗎 ?

此外,我在這里發現了類似的問題,但我無法為我找到合適的解決方案。

編輯1

來自@mbinette的解決方案:我已經為參考創建了第三個實體。 這樣對嗎 ?

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

    }

但是產品實體和類別實體是什么?

產品實體:

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

   //Please show me the getter and setter methods

分類實體:

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

//Please show me the getter and setter methods

無法向Dotrine / Doctrine2實體關系添加更多屬性。 如果你需要保存更多與關系相關的數據(除了涉及的實體),那么它只是一個簡單的關系,不是嗎? ;-)

您可以做的就是您在圖表中完成的工作 - 創建3個不同的實體,這些實體由ManyToOne / OneToMany關系鏈接。 然后,您可以自定義該實體並添加所需的信息/責任。

編輯

按位置,你的意思是你設定的位置,對吧? 或者您的意思是您想要保留它們添加到您的收藏中的順序? 如果是這樣,有一些未解決的門票( 門票1門票2 ),所以也許我們將來會看到它。 現在,我認為你必須堅持使用RelationshipEntity(3個實體)。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM