dqv84329 2016-07-17 15:43
浏览 27
已采纳

文章和引用symfony之间的多对多关系

I have a manytomany relation between two entities (product and quote) so that one or more products could be in one or more quotes. For example:

assuming that a customer chooses two products from a specific enterprise and he would like to receive a quote that summarize all the chosen products. Then he decides to choose another product of another enterprise to get at the end another quote. So here we have two quotes of different enterprises and of same user that each quote has its own products. you will say that it is a onetomany relation between the quote and the product because as I mentioned above that one quote can have many products so in the database you will have a quote_id column in the product table.

However, if another customer chooses the same products, a new quote will be created but when the query will insert these products in the quote by filling the quote_id column of the product table, it finds out that these articles are already have a quote_id of another quote.

That's why it is a manytomany relation so that many articles could be in many quotes.

This part is realized and I can match many products to many quotes without any problems.

Here is the entity of quote:

    /**
     * @ORM\ManyToMany(targetEntity="ArticleBundle\Entity\Article", mappedBy="quotes")
     */
    private $articles;

    public function __construct() {
        $this->articles = new \Doctrine\Common\Collections\ArrayCollection();
        $this->createdAt = new \DateTime();
    }


    /**
     * Set articles
     *
     * @param \ArticleBundle\Entity\Article $articles
     * @return Quote
     */
    public function setArticles(\ArticleBundle\Entity\Article $articles = null) {
        $this->articles = $articles;

        return $this;
    }

    /**
     * Get articles
     *
     * @return \ArticleBundle\Entity\Article 
     */
    public function getArticles() {
        return $this->articles;
    }

    /**
     * Add articles
     *
     * @param \QuoteBundle\Entity\Quote$articles
     * @return Devis
     */
    public function addArticle(\ArticleBundle\Entity\Article $article) {       
        $article->addQuote($this); // synchronously updating inverse side
        $this->articles[] = $article;

        return $this;
    }

    /**
     * Remove articles
     *
     * @param \QuoteBundle\Entity\Quote $articles
     */
    public function removeArticle(\ArticleBundle\Entity\Article $article) {
        $this->articles->removeElement($article);
    }

}

the entity article:

/**
 * @ORM\ManyToMany(targetEntity="QuoteBundle\Entity\Quote", inversedBy="articles")
 * @ORM\JoinTable(name="quotes_articles")
 */
private $quotes;


/**
 * Add devises
 *
 * @param \DevisBundle\Entity\Quote $quote
 * @return Article
 */
public function addQuote(\QuoteBundle\Entity\Quote $quote) {
    $this->quotes[] = $quote;

    return $this;
}

/**
 * Remove quotes
 *
 * @param \QuoteBundle\Entity\Quote $quote
 */
public function removeQuote(\QuoteBundle\Entity\Quote $quote) {
    $this->quotes->removeElement($quote);
}

/**
 * Get quotes
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getQuotes() {
    return $this->quotes;
}

}

The part that I find it difficult to reach is that when a customer chooses only one product , I would like to let him specify the quantity of the chosen product.

so when I updated the doctrine schema I got these tables :

  • quote table

  • product table

  • quotes_products table

can anyone tell me in which table I must add the quantity column and how can I write the annotation to let it added automatically in the database . I think it will be added to the quotes_products table.

Thank you for helping me.

  • 写回答

1条回答 默认 最新

  • duanchangnie7996 2016-07-17 16:31
    关注

    when adding a quantity column to the join table, you are making of your relationship a non pure manytomany. So, there will be no problem in separating the manyToMany relationships to two oneToMany relationships, and create an Entity Class for the join with additional field quantity.

    The idea in pseudo code:

    Class Quote
    {
    
    private $products;
    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="quote")
     */
    }
    

    Product:

    Class Product
    {
    
    private $quotes;
    /**
     * @ORM\OneToMany(targetEntity="Quote", mappedBy="product")
     */
    }
    

    ProdcutQuote:

    Class ProductQuote
    {
    /**
     * @ORM\ManyToOne(targetEntity="Quote")
     * @ORM\JoinColumn(name="quote_id", referencedColumnName="id")
     */
    private $quote;
    
    /**
     * @ORM\ManyToOne(targetEntity="Product")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    private $product; 
    
    
    private $quantity;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示