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)
    {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ても何もしません。リレーションが完了し、Book(id を除く) 何かを変更しても作成者には影響しません。

ここに例があります。空の MemberBibtex をメンバーに関連付けています。フォームがデータとともに投稿された場​​合は、永続化とフラッシュで保存します。しかし、最初に、それをメンバーに追加しました。

于 2012-06-28T08:00:28.793 に答える