簡體   English   中英

Doctrine2的多對多關聯和唯一一對多關系的雙向和插入操作

[英]Doctrine2 Association for ManyToMany Bidirectional and Insert operation for Unique OneToMany Relationships

我正在將Symfony2Doctrine 2 我的申請中有5個實體。

  1. 關鍵字(雙向,應獲取所有具有特定關鍵字的書)
  2. 作者(雙向,應獲取所有具有特定作者的書)
  3. 類別(雙向,應獲取屬於特定類別的所有書籍)
  4. BookExtra(在書本實體中,單向,應獲取BookExtra數據)

    • 每本書可以有很多關鍵字
    • 每本書可以有很多作者
    • 每本書可以歸為一類
    • 每本書只有一個BookExtra記錄。
    • 關鍵字表和作者表應包含唯一值

當一個新的Book被添加,如果Author(s)Keyword(s)存在,尊重Author IDKeyword ID應該分配給的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;

}

KeywordCategory實體類似於作者實體

問題是,當我使用cli生成schema時,它永遠不會生成Relationships/Associations

Book & Author實體應該建立什么樣的正確Relationships/Associations

我搜索了此問題以及適當的Relationships/Associations

我找到了這個:

Symfony2應用程序/控制台未為實體關系/關聯生成屬性或架構更新

保存一對一關系實體

doctrine2:在一對多的雙向關系中,如何從反面保存?

但這並沒有幫助我。

有人可以舉例說明這種類型的Relationships/Associations並進行insert操作嗎?

對於作者->書籍:

/**
 * @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;

對於書籍->作者

/**
 * @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

編輯

假設您的所有實體都會有setter和getter。

// 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM