2013-03-08 1 views
0

간단한 질문이 있습니다.많은 게시물과 카테고리에 많은 테이블이 두 개 있습니다. 이 그대로 형태로 나타납니다. PostType 양식 CategoryType의 컬렉션이지만 여기에서 문제가 시작됩니다. . Symfony2 간단한 다 대다 이슈

는 난 그냥 원하는 결과를 얻을하지 않습니다 데이터를 유지하기 위해 요리 책 수집 양식의 지시에 따라 .. 이 여기에 코드입니다 :

class Post 
{ 
/** 
* 
* @ORM\ManyToMany(targetEntity="Categories", inversedBy="posts", cascade={"persist", "remove"}) 
* @ORM\JoinTable(name="AnCat", 
* joinColumns={ 
*  @ORM\JoinColumn(name="post_id", referencedColumnName="id") 
* }, 
* inverseJoinColumns={ 
*  @ORM\JoinColumn(name="categories_id", referencedColumnName="id") 
* } 
*) 
**/ 
protected $categories; 

public function __construct() 
{ 
    $this->categories = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

public function addCategory($categories) 
{ 
    foreach ($categories as $category) { 
     $category->addPosts($this); 
    } 
    $this->categories[] = $categories; 
} 

class Categories 
{ 
/** 
* 
* @ORM\ManyToMany(targetEntity="Post", mappedBy="categories") 
*/ 
protected $posts; 

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

/** 
* 
* @param Post $post 
* @return Categories 
*/ 
public function addPosts(Post $posts) 
{ 
    // I tried it but I get the same result! 
    /*if (!$this->posts->contains($posts)) { 
     $this->posts->add($posts); 
    }*/ 

    $posts->addCategory($this); 
    $this->posts[] = $posts; 
} 

class PostType extends AbstractType 
{ 

->add('Categories', 'collection', array('type' => new CategoriesType(), 
       'allow_add' => true, 
       'allow_delete' => true, 
       'prototype' => true, 
       'prototype_name' => '__categ__', 
       'by_reference' => false 
      )) 

class CategoriesType extends AbstractType 
{ 
    ->add('category', 'entity', array(
      'attr' => array('class' => 'cat'), 
      'class' => 'MyBusinessBundle:Categories', 
      'property' => 'category', 
      'label' => 'Categories' 
     )) 

문제는 새로운 분야 카테고리를 삽입 카테고리 대신 간단한 관계를 만드는 대신 내가 틀렸다 곳 내가 .. 당신의 postType에서

+0

What와 what? – Hast

+0

어떤 의미입니까? 더 구체적으로 기재 할 수 있습니까? 정보가 누락 된 것 같지 않습니다 ... – Lughino

답변

1

을 이해하지 못하고, 게시물 작성 양식에서 엔티티 유형

class PostType extends AbstractType 
    { 
    $builder->add('Categories', 'entity', 
     array('label' => 'Categories', 
      'required' => false, 
      'expanded' => true, 
      'class' => 'xxx\xxxBundle\Entity\Categories', 
      'property' => 'title', 
      'multiple' => true, 
     )); 

로 변경 수집 유형은 카테고리와 체크 박스를해야합니다. 다중 선택 필드가 필요하면 false로 확장 변경하십시오.

+0

고마워요,하지만 주된 문제는 컬렉션이 필요하다는 것입니다.이 예제에서는 카테고리가 하나가 아닌 하위 카테고리로 연결되고 CategoriesType에 필드가 있으므로 카테고리 필드 엔티티 하위 카테고리. – Lughino

+0

Perhap의 데이터 모드는 매우 "깨끗한"것이 아닙니다 ... categorie와 oneToMay 관계가있는 "join table"엔티티 호출 (postCategory), 게시물, subcat, type과 다른 oneToMay 관계를 생성해야합니다. 그 후 모든 필드가있는 콜렉션을 작성할 수 있습니다. – Clotaire