关于书籍和作者的小例子:数据库结构:
实体(它们是使用 symfony2 控制台命令从数据库生成的):
作者:
/**
* Author
*
* @ORM\Table(name="authors")
* @ORM\Entity
*/
class Author
{
private $_isCachable = true;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="App\CoreBundle\Entity\Books", mappedBy="author", cascade={"persist","remove","detach","merge","refresh"})
*/
private $book;
/**
* Constructor
*/
public function __construct()
{
$this->book = new \Doctrine\Common\Collections\ArrayCollection();
}
}
书:
<?php
namespace App\CoreBundle\Entity;
use App\CoreBundle\RedisLayer\Marks\Insert;
use App\CoreBundle\RedisLayer\Marks\Update;
use Doctrine\ORM\Mapping as ORM;
/**
* Book
*
* @ORM\Table(name="books")
* @ORM\Entity
*/
class Book
{
private $_isCachable = true;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="ttile", type="string", length=255, nullable=false)
*/
private $ttile;
/**
* @var string
*
* @ORM\Column(name="isbn", type="string", length=255, nullable=false)
*/
private $isbn;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="App\CoreBundle\Entity\Authors", inversedBy="book", cascade={"persist","remove","detach","merge","refresh"})
* @ORM\JoinTable(name="author_books",
* joinColumns={
* @ORM\JoinColumn(name="book_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
* }
* )
*/
private $author;
/**
* Constructor
*/
public function __construct()
{
$this->author = new \Doctrine\Common\Collections\ArrayCollection();
}
}
所以当我运行这段代码时:
$book = new \App\CoreBundle\Entity\Book();
$book->setTtile('book title: '.uniqid());
$book->setIsbn('book isbn: '.uniqid());
$author = new \App\CoreBundle\Entity\Author();
$author->setName('author title: '.uniqid());
$author->addBook($book);
$entityManager->persist($author);
$entityManager->flush();
我希望学说能够生成这些 SQL:
"START TRANSACTION"]
# CREATE BOOK
INSERT INTO books (ttile, isbn) VALUES (?, ?)
array(2) {
[1] =>
string(25) "book title: 524ea3cf34d5f"
[2] =>
string(24) "book isbn: 524ea3cf34da3"
}
array(2) {
[1] =>
string(6) "string"
[2] =>
string(6) "string"
}
# CREATE AUTHOR
INSERT INTO authors (name) VALUES (?)
array(1) {
[1] =>
string(27) "author title: 524ea3cf34de9"
}
array(1) {
[1] =>
string(6) "string"
}
# CREATE LINKS
INSERT INTO author_books (book_id, author_id) VALUES (?, ?)
array(2) {
[0] =>
int(34)
[1] =>
int(23)
}
"COMMIT"
但我只得到#CERATE AUTHOR 和#CREATE BOOK 的SQL。未生成用于链接表的 SQL。
问题是什么?
如果我运行此代码:
$book = new \App\CoreBundle\Entity\Book(); $book->setTtile('book title: '.uniqid()); $book->setIsbn('book isbn: '.uniqid()); $author = new \App\CoreBundle\Entity\Author(); $author->setName('author title: '.uniqid()); $author->addBook($book); $book->addAuthor($author); // added $entityManager->persist($author); $entityManager->flush();
一切正常。所以我需要将书添加到作者和作者到书?是否可以创建书籍和作者,然后创建书籍并刷新(不将作者添加到书籍)?
如果我使用 onFLush 事件监听器,我可以使用 unitOfWork对象
getScheduledEntityUpdates
的(应该更新的实体列表)、getScheduledEntityInsertions
(应该更新的实体列表)和getScheduledEntityDeletions
(应该删除的实体列表)方法。但这只是实体。如何获取有关链接表的信息?我需要在学说 onFlush 事件中获取用于链接表的 SQL。