【发布时间】:2017-01-11 12:16:23
【问题描述】:
我有三个简单的实体:RecipeEntity、IngredientEntity 和 FoodEntity。
据我正确理解教义关联RecipeEntity 应该与IngredientEntity 具有双向 oneToMany 关系,因为一个配方包含许多成分。
一种成分只包含一种食物,因此我假设从成分到食物存在单向关联。
作为 ID,我使用第三方库使用 Uuid 而不是整数,这通常可以正常工作。
现在,我的 SQL 数据库充满了指向食物的成分的食谱。 当我调用食谱时,我可以检索成分。 在循环访问配料时,我可以将配方(双向关联)作为对象访问。
但是,当我想访问食物时,我没有得到我预期的 FoodEntity 对象,而只有食物的 id(它本身就是一个对象,因为使用的 uuid 库)。
为什么我没有得到FoodEntity 对象?
怎么了?
希望,我说清楚了! 感谢您的帮助。
干杯, LT。
这就是我所拥有的(为了更好的可读性而减少了):
/**
* Class RecipeEntity
*
* @ORM\Entity(repositoryClass="RecipeRepository")
* @ORM\Table(name="recipe")
*
*/
class RecipeEntity implements ArraySerializableInterface
{
/**
* @ORM\Column(name="id", type="uuid")
* @ORM\Id
* @ORM\GeneratedValue(strategy="UUID")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="IngredientEntity", mappedBy="recipe")
*/
private $ingredients;
public function __construct()
{
$this->ingredients = new ArrayCollection();
}
/**
* @return Collection
*/
public function getIngredients()
{
return $this->ingredients;
}
}
/**
* Class IngredientEntity
*
* @ORM\Entity
* @ORM\Table(name="ingredient", indexes={@ORM\Index(name="recipe_id", columns={"recipe_id"}), @ORM\Index(name="food_id", columns={"food_id"})})
*/
class IngredientEntity implements ArraySerializableInterface
{
/**
* @ORM\Column(name="id", type="uuid")
* @ORM\Id
* @ORM\GeneratedValue(strategy="UUID")
*/
private $id;
/**
* @ORM\Column(name="recipe_id", type="uuid")
* @ORM\ManyToOne(targetEntity="RecipeEntity", inversedBy="ingredients")
*/
private $recipe;
/**
* @ORM\Column(name="food_id", type="uuid")
* @ORM\OneToOne(targetEntity="FoodEntity")
*/
private $food;
}
/**
* Class FoodEntity
*
* @ORM\Table(name="food", indexes={@ORM\Index(name="source_id", columns={"source_id"})})
* @ORM\Entity(repositoryClass="LT\Model\Repository\FoodRepository")
*/
class FoodEntity implements ArraySerializableInterface
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="uuid")
* @ORM\GeneratedValue(strategy="UUID")
*/
private $id;
}
【问题讨论】:
标签: php orm doctrine-orm associations one-to-one