2013-10-18 153 views
0

我使用Symfony2與​​。 我在我的應用程序中有5個實體。Doctrine2 AssociationTo ManyToMany雙向和插入操作的唯一OneToMany關係

  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; 

} 

Keyword & Category實體類似於筆者實體

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

什麼應該是正確的Relationships/AssociationsBook & Author實體

我搜索了這個問題,並適當Relationships/Associations

我發現這一點:

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/Associationsinsert操作嗎?

回答

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; 

對於書籍 - >作者

/** 
* @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和getters。

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

+0

謝謝,你能否提供一個例子在作者表中添加一個唯一記錄,以便它只包含唯一記錄?如果具有相同作者的另一本書將被分配給他們尊敬的身份證件(我已經描述過) – HelloWeb

相關問題