有關的書籍和作者小例子: DB結構:學說許多一對多的關係,onFlush事件
實體(他們使用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();
我希望學說產生這些SQLS:
"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作者和#創建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();
一切都OK。所以我需要將書添加到作者和作者預訂?是否有可能創建書籍和作者,然後書籍作者和刷新(無需添加作者書)?
如果我使用學說onFLush事件偵聽器,我可以使用
getScheduledEntityUpdates
(應該更新實體的名單),getScheduledEntityInsertions
(應該更新實體的列表)和getScheduledEntityDeletions
(應刪除實體列表)的方法unitOfWork對象。但這只是實體。我怎樣才能獲得有關鏈接表的信息?我需要獲取SQL來鏈接教師onFlush事件的表。
謝謝。這工作。但其他問題依然存在。如何在onFlush事件中獲取關於linkig表的信息(因爲這不是實體)?更清楚的是:在onFlush事件中,所有實體都被序列化並保存到NoSQL數據庫中。其他PHP腳本使unserialze併合併到實體管理器和刷新。並沒有生成鏈接表的SQL。 – tuchk4