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.