[英]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.