0

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

1 回答 1

0

您是否将书籍与Author? 因为要关联它,您需要这样做:

$Author->addBook($Book);

你是在 addBook 中说的:

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

之后,当您完成作者和书籍之间的关联后,如果您编辑BookAuthor则不会执行任何操作。关系完成,更改 a 的某些内容Book(id 除外)不会影响作者。

这是一个例子。我将一个空的 MemberBibtex 关联到一个成员。如果表单与数据一起发布,我将使用持久化和刷新保存它。但首先,我将其添加到成员中。

于 2012-06-28T08:00:28.793 回答