Doctrine2 Association for ManyToMany针对唯一OneToMany关系的双向和插入操作

时间:2013-10-18 14:25:22

标签: entity-framework symfony doctrine-orm relational-database

我正在使用Symfony2 Doctrine 2。 我的申请中有5个实体。

  1. 关键字(双向,应该提取所有具有特定关键字的图书)
  2. 作者(双向,应该取得所有具有特定作者的书籍)
  3. 类别(双向,应取出属于特定类别的所有图书)
  4. BookExtra(单向,在Book实体中,应该获取BookExtra数据)

    • 每本书都有很多关键词
    • 每本书可以有很多作者
    • 每本书都可以归入单一类别
    • 每本书都有一本BookExtra记录。
    • 关键字和作者表应包含唯一值
  5. 添加新的Book后,如果Author(s)Keyword(s)存在,则应将受尊重的Author IDKeyword ID分配给{{1}如果它不存在,则会创建新的Book,并且应将受尊重的records分配给ID

    我有以下Book

    Entity Classes

    Book.php
    
    namespace Acme\StoreBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * Book
     *
     * @ORM\Table(name="book")
     * @ORM\Entity
     */
    class Book {
    
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    
    /**
     * @var string
     *
     * @ORM\Column(name="isbn", type="string", length=16)
     */
    protected $isbn;
    
    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    protected $title;
    
    /*
     * @ORM\OneToOne(targetEntity="BookExtra")
     * @ORM\JoinColumn(name="extra_id", referencedColumnName="id")
     *
     *   *********         OR        *********
     *   *********What should go HERE*********
     *
     */
    protected $bookextra;
    
    /*
     * @ORM\ManyToMany(targetEntity="Author", inversedBy="books")
     * @ORM\JoinTable(name="authors_books")
     */
    protected $author;
    
    /*
     * @ORM\OneToOne(targetEntity="Category")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category;
    
    }
    

    BookExtra.php
    
    namespace Acme\StoreBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * BookExtra
     *
     * @ORM\Table(name="book_extra")
     * @ORM\Entity
     */
    class Detail {
    
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    
    /**
     * @var string
     *
     * @ORM\Column(name="data", type="string", length=255)
     */
    protected $data;
    
    }
    

    Author.php namespace Bookhut\BookBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Author * * @ORM\Table(name="author") * @ORM\Entity */ class Author { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var string * * @ORM\Column(name="name", type="string", length=100) */ protected $name; // **********What should go HERE********* protected $books; } & Keyword实体类似于作者实体

    问题在于,当我使用Category生成schema时,它永远不会生成cli

    Relationships/Associations实体的适当Relationships/Associations应该是什么

    我搜索了这个问题并且正确Book & Author

    我发现了这个:

    Symfony2 app/console not generating properties or schema updates for Entity Relationships/Associations

    Saving onetoone relation entities

    doctrine2: in a one-to-many bidirectional relationship, how to save from the inverse side?

    但它对我没有帮助。

    有人可以提供此类Relationships/AssociationsRelationships/Associations操作的示例吗?

1 个答案:

答案 0 :(得分:0)

对于作者 - >书籍:

/**
 * @ManyToMany(targetEntity="Author", inversedBy="books")
 * @JoinTable(name="authors_books",
 *     joinColumns={@JoinColumn(name="book_id", referencedColumnName="id")},
 *     inverseJoinColumns={@JoinColumn(name="author_id", referencedColumnName="id")}
 * )
 */
protected $author;

对于books-> authors

/**
 * @ManyToMany(targetEntity="Book", mappedBy="author")
 */
protected $books;

有关多对多

的文档,请参阅http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional

有关关联的完整文档,请参阅http://docs.doctrine-project.org/en/latest/reference/association-mapping.html

修改

假设你的所有实体都有定位器和吸气剂。

// Create new Author object
$Author = new Author();
// Use setters to set all attributes for this author

// Create new book
$Book = new Book();
// Use setters to set all attributes for book

// Attach book to author
$Author->addBook($Book);
$Book->addAuthor($Author);

addBook和addAuthor将如下所示:

// Inside author entity
public function addBook(Book $Book) {
    $this->books->add($Book);
}

// Inside book entity
public function addAuthor($Author) {
    $this->authors->add($Author);
}

并为作者和书籍实体添加构造函数:

public function __construct() {
    $this->books = new ArrayCollection();
}

public function __construct() {
    $this->authors = new ArrayCollection();
}

查看文档以查看更多示例: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html