2017-01-11 5 views
1

RecipeEntity, IngredientEntityFoodEntity의 세 엔티티가 있습니다. 한 조리법이 많은 재료를 보유 doctrine oneToOne 단방향 : 매핑 된 객체 얻기

나는 교리 연결을 알고있는 것처럼

제대로 RecipeEntityIngredientEntity양방향 oneToMany 관계를 가져야한다. 하나의 성분이 정확히 하나의 음식을 보유하고 있으므로, 나는 단 향성의 성분을 식품으로 가정합니다. ID로 일반적으로 잘 작동하는 타사 라이브러리를 사용하는 정수 대신 Uuids가 있습니다.

이제 SQL 데이터베이스에 음식을 가리키는 재료를 가르키는 레시피가 있습니다. 레시피를 부를 때 재료를 검색 할 수 있습니다. 재료를 반복하면서 요리법 (양방향 연결)을 개체로 사용할 수 있습니다. 내가 음식을 액세스 할 때

그러나는, 나는 때문에 인 UUID 라이브러리의 객체 자체를 인 (내가 기대 한 것 같은 FoodEntity 개체를 얻을 수 있지만, 음식의 ID를하지 않습니다 익숙한).

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; 
} 

답변

1

당신이 만드는 실수는 (조리법의 경우) 및 @ManyToOne (식품의 경우) @OneToOne뿐만 아니라 모두 @Column을 추가 할 것입니다 . 속성은 관계/연관 또는 필드/열 중 하나이며 둘 다 사용할 수 없습니다.

엔티티 정의의 연결에서 @Column 주석을 제거해야합니다.

/** 
* 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\ManyToOne(targetEntity="RecipeEntity", inversedBy="ingredients") 
    */ 
    private $recipe; 

    /** 
    * @ORM\OneToOne(targetEntity="FoodEntity") 
    */ 
    private $food; 
} 
+0

감사의 말, 1000 번, 그 트릭을! – Lowtower

+0

정확한 지점 !!! –

+0

@Lowtower 당신은 오신 것을 환영합니다. – Wilt