[英]Doctrine2 Association for ManyToMany Bidirectional and Insert operation for Unique OneToMany Relationships
I am using Symfony2
with Doctrine 2
. 我正在将
Symfony2
与Doctrine 2
。 I have 5 entities in my application. 我的申请中有5个实体。
BookExtra (Unidirectional, In Book Entity, should fetch BookExtra data) BookExtra(在书本实体中,单向,应获取BookExtra数据)
When a new Book
is added, if the Author(s)
and Keyword(s)
exists, the respected Author ID
and Keyword ID
should be assigned to the Book
, and if it doesn't exist, the new records
will be created and the respected ID
should be assigned to the Book
. 当一个新的
Book
被添加,如果Author(s)
和Keyword(s)
存在,尊重Author ID
和Keyword ID
应该分配给的Book
,如果它不存在,新的records
将被创建和应将受尊敬的ID
分配给Book
。
I have the following Entity Classes
: 我有以下
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
Entities are similar to Author Entity Keyword
和Category
实体类似于作者实体
The Problem is that, when i generate schema
with cli
, it never generates Relationships/Associations
. 问题是,当我使用
cli
生成schema
时,它永远不会生成Relationships/Associations
。
And what should be the proper Relationships/Associations
for Book & Author
Entity Book & Author
实体应该建立什么样的正确Relationships/Associations
I searched for this problem and proper Relationships/Associations
我搜索了此问题以及适当的
Relationships/Associations
I found this: 我找到了这个:
Symfony2 app/console not generating properties or schema updates for Entity Relationships/Associations Symfony2应用程序/控制台未为实体关系/关联生成属性或架构更新
Saving onetoone relation entities 保存一对一关系实体
doctrine2: in a one-to-many bidirectional relationship, how to save from the inverse side? doctrine2:在一对多的双向关系中,如何从反面保存?
But it didn't help me. 但这并没有帮助我。
Can somebody give an example of this type of Relationships/Associations
and insert
operations? 有人可以举例说明这种类型的
Relationships/Associations
并进行insert
操作吗?
For author->books: 对于作者->书籍:
/**
* @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;
For books->authors 对于书籍->作者
/**
* @ManyToMany(targetEntity="Book", mappedBy="author")
*/
protected $books;
See http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional for documentation of many-to-many 有关多对多文档,请参见http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional
See http://docs.doctrine-project.org/en/latest/reference/association-mapping.html for full documentation of associations 有关关联的完整文档,请参见http://docs.doctrine-project.org/en/latest/reference/association-mapping.html 。
EDIT 编辑
Is assume all your entities will have setters and getters. 假设您的所有实体都会有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);
The addBook and addAuthor will look like this: 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);
}
And add a constructor to both the author and book entities: 并向作者和书籍实体添加构造函数:
public function __construct() {
$this->books = new ArrayCollection();
}
public function __construct() {
$this->authors = new ArrayCollection();
}
Have a look at the documentation to see more examples: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html 看一下文档以查看更多示例: http : //docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.