symfony2:如何编写多对多关系的方法?

symfony2:如何编写多对多关系的方法?,symfony,doctrine-orm,Symfony,Doctrine Orm,我在下面创建了这两个模型,作者和书通过多对多关系关联。然后我生成了crud方法 问题是:当我试图创建或保存作者时,它不会保存书籍 namespace Prueba\FrontendBundle\Entity; use \Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as Gedmo; /** * @ORM\Entity()

我在下面创建了这两个模型,作者和书通过多对多关系关联。然后我生成了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;
    }
}

你把这些书与作者联系起来了吗?因为要将其关联起来,您需要执行以下操作:

$Author->addBook($Book);
你在addBook中说:

die("1"); //Here is not entering after creating or editing a book! Why?
之后,当您完成了作者和书籍之间的关联后,如果您编辑书籍,则作者将不会执行任何操作。关系完成后,更改一本书的某些内容,但id不会影响作者


。我正在将一个空成员BIBTEX与一个成员关联。如果表单发布了数据,我将使用persist和flush保存它。但首先,我将它添加到了成员中。

发布书籍代码以及保存实体的代码。