나는 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;
}
}
그냥이의 사실이다 명확하게 작동합니다 이 특별한 문제는 있지만 소유 측면은 관례에 불과합니다. 엔티티를 사용하는 방식에 따라 소유자가 무엇인지 결정할 수 있습니다. http://readthedocs.org/docs/doctrine-orm/en/2.0.x/reference/association-mapping.html – gremo
감사합니다. 네, 맞습니다. 'inverseBy'와 'mappedBy'의 위치가 중요하지 않다고 생각했지만 잘못되었습니다. – ziiweb
어쨌든, 궁금한 점이 있습니다 : 코딩 흐름이 addAuthor() 또는 setAuthors()에 입력되지 않는 이유는 무엇입니까? ? – ziiweb