我正在使用Symfony2
Doctrine 2
。
我的申请中有5个实体。
BookExtra(单向,在Book实体中,应该获取BookExtra数据)
添加新的Book
后,如果Author(s)
和Keyword(s)
存在,则应将受尊重的Author ID
和Keyword ID
分配给{{1}如果它不存在,则会创建新的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
生成schema
时,它永远不会生成cli
。
Relationships/Associations
实体的适当Relationships/Associations
应该是什么
我搜索了这个问题并且正确Book & Author
我发现了这个:
Saving onetoone relation entities
doctrine2: in a one-to-many bidirectional relationship, how to save from the inverse side?
但它对我没有帮助。
有人可以提供此类Relationships/Associations
和Relationships/Associations
操作的示例吗?
答案 0 :(得分: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;
对于books-> authors
/**
* @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
修改强>
假设你的所有实体都有定位器和吸气剂。
// 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