それで、私はしばらくDoctrineを使って遊んでいて、いくつかの基本的なプロジェクトでそれを持っていますが、戻って何ができるかを詳しく調べることにしました.
私は現在、選択したフレームワークとして symfony 2 に切り替えることを決定し、doctrine 2 で何ができるかをより深く調べています。
私が理解しようとしてきたことの 1 つは、ドクトリン内の多対多の関係です。私はレシピシステムの構築を開始しており、レシピと材料の関係に取り組んでおり、レシピ、レシピ材料、材料の 3 つのエンティティが得られました。直接の多対多の関係を使用できない理由は、各成分の結合テーブル (単位と数量) に 2 つの追加の列を格納したいからです。
現時点で私が抱えている問題は、エンティティが正常に存続することですが、結合テーブルのレシピ ID が挿入されないことです。私は考えることができるすべてを試し、答えを探してすべてのスレッドとウェブサイトを調べました. 私が見逃しているのは完全に明白なものだと確信しています。助けてください、以下は私がこれまでに持っているコードです:
<?php
namespace Recipe\RecipeBundle\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\OneToMany(targetEntity="RecipeIngredient", mappedBy="recipe", cascade= {"persist"})
*/
protected $ingredients;
/**
* @ORM\Column(type="string")
* @var string $title
*
*/
protected $title;
/**
* Constructor
*/
public function __construct()
{
$this->ingredients = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add ingredients
*
* @param \Recipe\RecipeBundle\Entity\RecipeIngredient $ingredients
* @return Recipe
*/
public function addIngredient(\Recipe\RecipeBundle\Entity\RecipeIngredient $ingredients)
{
$ingredients->setRecipe($this);
$this->ingredients[] = $ingredients;
return $this;
}
/**
* Remove ingredients
*
* @param \Recipe\RecipeBundle\Entity\RecipeIngredient $ingredients
*/
public function removeIngredient(\Recipe\RecipeBundle\Entity\RecipeIngredient $ingredients)
{
$this->ingredients->removeElement($ingredients);
}
/**
* Get ingredients
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getIngredients()
{
return $this->ingredients;
}
/**
* Set title
*
* @param string $title
* @return Recipe
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
とレシピ食材
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Recipe", inversedBy="ingredients")
* */
protected $recipe;
/**
* @ORM\ManyToOne(targetEntity="Ingredient", inversedBy="ingredients" , cascade={"persist"})
* */
protected $ingredient;
/**
* @ORM\Column(type="string")
* @var string $quantity
*
*/
protected $quantity;
/**
* @ORM\Column(type="string")
* @var string $unit
*
*/
protected $unit;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set quantity
*
* @param string $quantity
* @return RecipeIngredient
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* @return string
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Set unit
*
* @param string $unit
* @return RecipeIngredient
*/
public function setUnit($unit)
{
$this->unit = $unit;
return $this;
}
/**
* Get unit
*
* @return string
*/
public function getUnit()
{
return $this->unit;
}
/**
* Set recipe
*
* @param \Recipe\RecipeBundle\Entity\Recipe $recipe
* @return RecipeIngredient
*/
public function setRecipe(\Recipe\RecipeBundle\Entity\Recipe $recipe = null)
{
$this->recipe = $recipe;
return $this;
}
/**
* Get recipe
*
* @return \Recipe\RecipeBundle\Entity\Recipe
*/
public function getRecipe()
{
return $this->recipe;
}
/**
* Set ingredient
*
* @param \Recipe\RecipeBundle\Entity\Ingredient $ingredient
* @return RecipeIngredient
*/
public function setIngredient(\Recipe\RecipeBundle\Entity\Ingredient $ingredient = null)
{
$this->ingredient = $ingredient;
return $this;
}
/**
* Get ingredient
*
* @return \Recipe\RecipeBundle\Entity\Ingredient
*/
public function getIngredient()
{
return $this->ingredient;
}
}