2012-05-14 2 views
0

나는 many to many 관계를 통해 아래 두 모델 (저자와 책)을 만들었습니다. 그런 다음 나는 진부한 방법을 만들어 냈습니다.symfony2 : many to many relationship을위한 메소드를 작성하는 방법?

문제 : 새로운 또는 편집 양식을 사용하여 작성자를 만들거나 저장하려고 시도하면 해당 책이 저장되지 않습니다.

namespace Prueba\FrontendBundle\Entity; 

use \Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 
use Gedmo\Mapping\Annotation as Gedmo; 

/** 
* @ORM\Entity() 
* @ORM\Table(name="author") 
*/ 
class Author 
{ 
    /** @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer") **/ 
    protected $id; 

    /** 
    * @ORM\Column(type="string") 
    */ 
    protected $name; 

    /** 
    * @ORM\ManyToMany(targetEntity="Book", inversedBy="authors") 
    * @ORM\JoinTable(name="author_book") 
    */ 
    protected $books; 

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

    public function getId() 
    { 
     return $this->id; 
    } 

    public function getName() 
    { 
     return $this->name; 
    } 

    public function setName($name) 
    { 
     $this->name = $name; 
    } 


    public function getBooks() 
    { 
     return $this->books; 
    } 

    public function addBook($book) 
    { 
     $this->books[] = $book; 
    } 

    public function setBooks($books) 
    { 
     $this->books = $books; 
    } 
    public function __toString() 
    { 
     return $this->name; 
    } 
} 

-------------- 

namespace Prueba\FrontendBundle\Entity; 

use \Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 
use Gedmo\Mapping\Annotation as Gedmo; 

/** 
* @ORM\Entity() 
* @ORM\Table(name="book") 
*/ 
class Book 
{ 
    /** @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer") **/ 
    protected $id; 

    /** 
    * @ORM\Column(type="string") 
    */ 
    protected $title; 

    /** 
    * @ORM\ManyToMany(targetEntity="Author", inversedBy="books") 
    * @ORM\JoinTable(name="author_book") 
    */ 
    protected $authors; 

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

    public function getId() 
    { 
     return $this->id; 
    } 

    public function getTitle() 
    { 
     return $this->title; 
    } 

    public function setTitle($title) 
    { 
     //die("fadsfadsff"); 
     $this->title = $title; 
    } 

    public function getAuthors() 
    { 
     //die(); 
     return $this->authors; 
    } 

    public function addAuthor($authors) 
    { 
     die();//Here is not entering after editing a book!!!!! why???? 
     $this->authors = $authors; 
    } 

    public function setAuthors($authors) 
    { 
     die();//Here is not entering after editing a book!!!!! why???? 
     $this->authors = $authors; 
    } 

    public function __toString() 
    { 
     return $this->title; 
    } 
} 

답변

2

문제의 원인인지는 모르지만 두 엔터티 모두에 inversedBy을 지정했는지 확인하십시오.

그것은해야한다 : 저자 책을 소유

class Author 
{ 
    /** 
    * @ORM\ManyToMany(targetEntity="Book", inversedBy="authors") 
    */ 
    protected $books; 
} 

때문입니다.

그리고

class Book 
{ 
    /** 
    * @ORM\ManyToMany(targetEntity="Author", mappedBy="books") 
    */ 
    protected $authors; 
} 

이 작성자에게 속해 있기 때문에

.

+0

그냥이의 사실이다 명확하게 작동합니다 이 특별한 문제는 있지만 소유 측면은 관례에 불과합니다. 엔티티를 사용하는 방식에 따라 소유자가 무엇인지 결정할 수 있습니다. http://readthedocs.org/docs/doctrine-orm/en/2.0.x/reference/association-mapping.html – gremo

+0

감사합니다. 네, 맞습니다. 'inverseBy'와 'mappedBy'의 위치가 중요하지 않다고 생각했지만 잘못되었습니다. – ziiweb

+0

어쨌든, 궁금한 점이 있습니다 : 코딩 흐름이 addAuthor() 또는 setAuthors()에 입력되지 않는 이유는 무엇입니까? ? – ziiweb

0

계단식 지속성은 자동으로하지 않고, 당신은 내가 ManyToMany 협회와 함께 자신을 계단식 사용하지 않는 한 모든 cascade rules

/** 
* @ORM\ManyToMany(targetEntity="Book", inversedBy="authors", cascade={"persist"}) 
* @ORM\JoinTable(name="author_book") 
*/ 
protected $books; 

를 지정하지 않은,하지만