[英]Doctrine2 Association for ManyToMany Bidirectional and Insert operation for Unique OneToMany Relationships
我正在將Symfony2
與Doctrine 2
。 我的申請中有5個實體。
BookExtra(在書本實體中,單向,應獲取BookExtra數據)
當一個新的Book
被添加,如果Author(s)
和Keyword(s)
存在,尊重Author ID
和Keyword 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
。
Book & Author
實體應該建立什么樣的正確Relationships/Associations
我搜索了此問題以及適當的Relationships/Associations
我找到了這個:
Symfony2應用程序/控制台未為實體關系/關聯生成屬性或架構更新
但這並沒有幫助我。
有人可以舉例說明這種類型的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 。
編輯
假設您的所有實體都會有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.