0

以下の 2 つのモデル (Author と Book) を多対多の関係で関連付けて作成しました。次に、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)
    {
        $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;
    }
}
4

2 に答える 2

2

それが問題の原因かどうかはわかりませんがinversedBy、両方のエンティティで指定しました。

そのはず:

class Author
{
    /**
     * @ORM\ManyToMany(targetEntity="Book", inversedBy="authors")
     */
    protected $books;
}

著者が本を所有しているからです。

class Book
{
    /**
     * @ORM\ManyToMany(targetEntity="Author", mappedBy="books")
     */
    protected $authors;
}

著者のものだからです。

于 2012-05-14T19:15:55.557 に答える
0

カスケード永続性は自動ではなく、カスケード ルールを指定していません

/**
 * @ORM\ManyToMany(targetEntity="Book", inversedBy="authors", cascade={"persist"})
 * @ORM\JoinTable(name="author_book")
 */
protected $books;

私自身、ManyToMany アソシエーションでカスケードを使用したことはありませんが、動作するはずです

于 2012-05-14T21:10:44.643 に答える