gristoi Posted July 1, 2012 Share Posted July 1, 2012 Hi Guys, I have been having a play around with symfony 2 and doctrine, and everything was going fine until i hit a bit of a brick wall. As a test I am building a recipe blog and part of the design is causing me a headache. Basically i have made a three table relation : Recipe =>RecipeIngredients<=Ingredients. the recipe entity: <?php namespace Recipe\BlogBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * @ORM\Entity * @ORM\Table(name="recipe") * @ORM\HasLifecycleCallbacks() */ class Recipe{ /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string") */ protected $recipeName; /** * @ORM\Column(type="text") */ protected $recipeDescription; /** * @ORM\ManyToOne(targetEntity="RecipeCategory", inversedBy="recipe") * @ORM\JoinColumn(name="recipe_id", referencedColumnName="id") */ protected $recipeCategory; /** * @ORM\OneToMany(targetEntity="Comment", mappedBy="recipe") */ protected $comments; /** * @ORM\Column(type="datetime") */ protected $created; /** * @ORM\OneToMany(targetEntity="RecipeSteps", mappedBy="recipe") */ protected $recipeSteps; /** * @ORM\OneToMany(targetEntity="RecipeIngredients", mappedBy="ingredients", cascade={"all"}) */ protected $ingredients; /** * @ORM\Column(type="string") */ protected $image; public function __construct() { $this->comments = new \Doctrine\Common\Collections\ArrayCollection(); $this->recipeSteps = new \Doctrine\Common\Collections\ArrayCollection(); $this->ingredients = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set _recipeName * * @param string $recipeName */ public function setRecipeName($recipeName) { $this->_recipeName = $recipeName; } /** * Get _recipeName * * @return string */ public function getRecipeName() { return $this->_recipeName; } /** * Set recipeDescription * * @param text $recipeDescription */ public function setRecipeDescription($recipeDescription) { $this->recipeDescription = $recipeDescription; } /** * Get recipeDescription * * @return text */ public function getRecipeDescription() { return $this->recipeDescription; } /** * Set created * * @param datetime $created */ public function setCreated($created) { $this->created = $created; } /** * Get created * * @return datetime */ public function getCreated() { return $this->created; } /** * Set image * * @param string $image */ public function setImage($image) { $this->image = $image; } /** * Get image * * @return string */ public function getImage() { return $this->image; } /** * Set recipeCategory * * @param Recipe\BlogBundle\Entity\RecipeCategory $recipeCategory */ public function setRecipeCategory(\Recipe\BlogBundle\Entity\RecipeCategory $recipeCategory) { $this->recipeCategory = $recipeCategory; } /** * Get recipeCategory * * @return Recipe\BlogBundle\Entity\RecipeCategory */ public function getRecipeCategory() { return $this->recipeCategory; } /** * Add comments * * @param Recipe\BlogBundle\Entity\Comment $comments */ public function addComment(\Recipe\BlogBundle\Entity\Comment $comments) { $this->comments[] = $comments; } /** * Get comments * * @return Doctrine\Common\Collections\Collection */ public function getComments() { return $this->comments; } /** * Add recipeSteps * * @param Recipe\BlogBundle\Entity\RecipeSteps $recipeSteps */ public function addRecipeSteps(\Recipe\BlogBundle\Entity\RecipeSteps $recipeSteps) { $this->recipeSteps[] = $recipeSteps; } /** * Get recipeSteps * * @return Doctrine\Common\Collections\Collection */ public function getRecipeSteps() { return $this->recipeSteps; } /** * Add ingredients * * @param Recipe\BlogBundle\Entity\RecipeIngredients $ingredients */ public function addRecipeIngredients(\Recipe\BlogBundle\Entity\RecipeIngredients $ingredients) { $this->ingredients[] = $ingredients; } /** * Get ingredients * * @return Doctrine\Common\Collections\Collection */ public function getIngredients() { return $this->ingredients; } the ingredient entity: <?php namespace Recipe\BlogBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="ingredients") */ class Ingredients{ /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string") */ protected $ingredientName; /** * @ORM\OneToMany(targetEntity="RecipeIngredients", mappedBy="recipe", cascade={"all"}) */ protected $recipe; public function __construct() { $this->recipe = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set ingredientName * * @param string $ingredientName */ public function setIngredientName($ingredientName) { $this->ingredientName = $ingredientName; } /** * Get ingredientName * * @return string */ public function getIngredientName() { return $this->ingredientName; } /** * Add recipe * * @param Recipe\BlogBundle\Entity\RecipeIngredients $recipe */ public function addRecipeIngredients(\Recipe\BlogBundle\Entity\RecipeIngredients $recipe) { $this->recipe[] = $recipe; } /** * Get recipe * * @return Doctrine\Common\Collections\Collection */ public function getRecipe() { return $this->recipe; } } and the association table: <?php namespace Recipe\BlogBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="recipe_ingredients") */ class RecipeIngredients{ /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string") */ protected $volume; /** * @ORM\ManyToOne(targetEntity="Ingredients", inversedBy="recipe", cascade={"all"}) */ protected $ingredients; /** * @ORM\ManyToOne(targetEntity="Recipe", inversedBy="ingredients", cascade={"all"}) */ protected $recipe; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set volume * * @param string $volume */ public function setVolume($volume) { $this->volume = $volume; } /** * Get volume * * @return string */ public function getVolume() { return $this->volume; } /** * Set ingredients * * @param Recipe\BlogBundle\Entity\Ingredients $ingredients */ public function setIngredients(\Recipe\BlogBundle\Entity\Ingredients $ingredients) { $this->ingredients = $ingredients; } /** * Get ingredients * * @return Recipe\BlogBundle\Entity\Ingredients */ public function getIngredients() { return $this->ingredients; } /** * Set recipe * * @param Recipe\BlogBundle\Entity\Recipe $recipe */ public function setRecipe(\Recipe\BlogBundle\Entity\Recipe $recipe) { $this->recipe = $recipe; } /** * Get recipe * * @return Recipe\BlogBundle\Entity\Recipe */ public function getRecipe() { return $this->recipe; } } So the issue i have been having is that i want to build a form to add a recipe, with the ability to add multiple ingredients at once and for each ingredient insert it into the ingredients table, and the volume for that ingredient into the recipeIngredient table. I am just struggling on how to build the form, and what' types' to use to do this in one go. I have been scratching my head for days on this . I hope this makes sense. Please help Quote Link to comment https://forums.phpfreaks.com/topic/265073-symfony-2-many-to-many-issue-with-form-builder/ Share on other sites More sharing options...
gristoi Posted July 2, 2012 Author Share Posted July 2, 2012 In addition to my original post, i have tried using an embedded recipeIngredient type in my form, but as expected ingredient_name is not part of recipeingredient so i get an error when submitting my form. What i really need is to have this as a collection in the form. So that i can have x amount of ingredients per recipe. Just lost as volume is stored in the join table and the ingredient name is in the ingredients table, which are 2 totally different entities volume | ingredient ------------------------ 1 tsp | salt 500 g | flour ....... anyone? Quote Link to comment https://forums.phpfreaks.com/topic/265073-symfony-2-many-to-many-issue-with-form-builder/#findComment-1358523 Share on other sites More sharing options...
gristoi Posted July 3, 2012 Author Share Posted July 3, 2012 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/265073-symfony-2-many-to-many-issue-with-form-builder/#findComment-1358752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.