我在下面创建了通过多对多关系关联的这两个模型(作者和书籍)。然后我生成了 crud 方法。
问题:当我尝试创建或保存作者时,它不会保存书籍。
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;
}
}