2013-03-27 5 views
1

Symfony 2.1에서 엔티티 필드 유형을 사용할 때 양방향 관계 (일대 다 관계)에 문제가 있습니다. 하위 (Product)를 생성하고 상위 (Category)를 선택할 때, 모든 것이 늠름한 것이고, 예상대로 생성되고 지속됩니다. 문제는 부모 (카테고리)를 생성하고 Symfony2의 폼 시스템에서 생성 된 여러 자식 (제품) 체크 박스를 선택할 때 발생합니다. 은 유지되지 않습니다.Symfony 2.1 엔티티 필드 유형 다중 확장 안 함

그러나 나는 내 예를 들어 프로젝트의 repo 복제 쉬울 수 있습니다, 아래에있는 내 예를 들어 클래스를 놓을 게요 - https://github.com/domudall/multiple-expanded-entity-choice-issue

카테고리를

class Category 
{ 
    protected $id; 
    protected $name; 

    /** 
    * @ORM\OneToMany(targetEntity="Product", mappedBy="category") 
    */ 
    protected $products; 

    public function __construct() 
    { 
     $this->products = new ArrayCollection(); 
    } 

    public function addProduct($product) 
    { 
     if (!$this->products->contains($product)) { 
      $this->products->add($product); 
      $product->setCategory($this); 
     } 

     return $this; 
    } 

    public function setProducts($products) 
    { 
     foreach ($products as $product) { 
      $this->addProduct($product); 
     } 

     return $this; 
    } 

    public function removeProducts() 
    { 
     if ($this->products->contains($product)) { 
      $this->products->remove($product); 
     } 

     return $this; 
    } 
} 

제품

class Product 
{ 
    protected $id; 
    protected $name; 

    /** 
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="products") 
    */ 
    protected $category; 

    public function setCategory(Category $category) 
    { 
     $this->category = $category; 
     $category->addProduct($this); 

     return $this; 
    } 

    public function getCategory() 
    { 
     return $this->category; 
    } 
} 

DefaultController

class DefaultController extends Controller 
{ 
    public function categoryCreateAction(Request $request) 
    { 
     $category = new Category(); 

     $form = $this 
      ->createFormBuilder($category) 
      ->add('name', 'text', array()) 
      ->add('products', 'entity', array(
       'class' => 'AcmeShopBundle:Product', 
       'property' => 'name', 
       'required' => false, 
       'expanded' => true, 
       'multiple' => true, 
      )) 
      ->getForm(); 

     if ($request->getMethod() == 'POST') { 
      $this->save($category, $form, $request); 
     } 
    } 

    public function productCreateAction(Request $request) 
    { 
     $product = new Product(); 

     $form = $this 
      ->createFormBuilder($product) 
      ->add('name', 'text') 
      ->add('category', 'entity', array(
       'class' => 'AcmeShopBundle:Category', 
       'property' => 'name', 
       'required' => false, 
      )) 
      ->getForm(); 

     if ($request->getMethod() == 'POST') { 
      $this->save($product, $form, $request); 
     } 
    } 

    protected function save($entity, $form, $request) 
    { 
     $form->bind($request); 

     if ($form->isValid()) { 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($entity); 
      $em->flush(); 
     } 
    } 
} 

내가 주시면 감사하겠습니다 잘못된거야 곳으로 모든 포인터.

참고 차라리 단위 테스트에 대한 개체의 논리를 것, 교리를 cascade 옵션을 사용하지 않으

는 DB에서 분리합니다.

+1

은'mappedBy' 측 불변이다. Product 객체를 수동으로 유지해야합니다. – arghav

답변

3

캐스케이드하지 않으려면이 엔티티를 수동으로 유지해야합니다.

나는 수집의 map() 방법 (아래)를 사용하지만 당신은 컬렉션의 toArray() 방법을 통해 foreach 또는 array_map와 함께 할 수 있습니다.

컨트롤러는 다음 코드를보십시오

protected function saveCategory($category, $form, $request) 
{ 
    $form->bind($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager();   
     $entity->getProducts()->map(
      function($product) use ($em) { 
       $em->persist($product); 
      } 
     ); 

     $em->persist($category); 
     $em->flush(); 
    } 
} 
+1

코드 스 니펫을 수정하십시오. – Trix