2012-05-14 1 views
0

나는 많은 관계를 통해이 두 모델 (저자와 책)을 만들었습니다. 그런 다음 나는 진부한 방법을 만들어 냈습니다.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) 
    {die("1");//Here is not entering after creating or editing a book!!!!! why???? 
     $this->books[] = $book; 
    } 

    public function setBooks($books) 
    {die("2");//Here is not entering after creating or editing a book!!!!! why???? 
     $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="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 setBooks($books) 
    { 
     $this->books = $books; 
    } 

    public function __toString() 
    { 
     return $this->name; 
    } 
} 
+0

책 코드와 엔티티를 유지하는 코드를 게시하십시오. – gremo

답변

0

Author과 관련이 있습니까? 그것을 관계에 있기 때문에이 작업을 수행해야합니다

$Author->addBook($Book); 

그리고 당신은 addBook에서 그 말 :

die("1"); //Here is not entering after creating or editing a book! Why? 

당신은 저자와 책 사이의 연결을했을 때, 후에는 Book을 편집하는 경우를, Author은 아무 것도하지 않습니다. 관계가 완료되면 Book (id 제외)의 변경 사항이 작성자에게 영향을 미치지 않습니다.

Here is an example. 나는 비어있는 MemberBibtex를 회원과 연관 짓고있다. 양식이 데이터와 함께 게시되면 persist와 flush로 저장합니다. 하지만 먼저 회원에게 추가했습니다.