2014-09-04 3 views
-1

범주와 섹션간에 OneToMany 관계가 있습니다. 내가 뭘 하려는지 섹션에 대한 편집을 구현하는 것입니다. 그래서 내 편집 작업은 지금까지 다음과 같습니다레코드 배열을 통해 검색

/** 
    * 
    * 
    * @Route("category/{category_id}/section/{id}/edit", name="section_edit") 
    * @Method("GET") 
    * @Template() 
    */ 



public function editAction($category_id, $id) 

    { 
     $em = $this->getDoctrine()->getManager(); 

    $category = $em->getRepository('MyOwnBundle:Category')->find($category_id); 

    if (!$category) { 
     throw $this->createNotFoundException('Unable to find Category entity.'); 
    } 

    ////..... 

}

모든 매우 간단하지만, 난 정말 $ 범주에 속하는 부분을 검색하는 방법을 모르겠어요. 편집을 수행하려면 id == $ id 인이 특정 레코드가 필요합니다. 분명히 모든 섹션을 통해 단순히 볼 수 없다. 왜냐하면 내가 정말로 $ 카테고리에 속하는지 알 수 없기 때문이다. 내가 이것을 할 수있게 해주는 간단한 방법이나,이 간단한 루프를 통해 $category->getSections()을 하나씩 처리해야합니까?

답변

0

솔루션은 매우 간단합니다.

ParamConverter을 사용하여 필요한 개체를로드 할 수 있습니다. 발견되지 않으면 실패합니다.

자세한 내용은 here을 참조하십시오. ParamConverter 주석 (use)을 가져 오는 것을 잊지 마십시오!

/** 
* @Route("category/{category_id}/section/{id}/edit", name="section_edit") 
* @ParamConverter("category", class="AcmeBundle:Category", options={"id": "category_id"}) 
* @Method("GET") 
* @Template() 
*/ 
public function editAction(Category $category, Section $section){ 
    // both category and section are fully loaded, no need to go to doctrine or check if they are null 

    if (!$section->getCategories()->contains($category)){ 
     throw $this->createNotFoundException('Invalid request.'); 
    } 

    ... 
    the rest of your logic 
    ... 
}