3

我的问题参考了Doctrine 多对多关系和 onFlush 事件

在此处输入图像描述

作者实体:http://pastebin.com/GBCaSA4Z书籍 实体:http ://pastebin.com/Xb2SEiaQ

我运行这段代码:

    $book = new \App\CoreBundle\Entity\Books();
    $book->setTtile('book title: '.uniqid());
    $book->setIsbn('book isbn: '.uniqid());

    $author = new \App\CoreBundle\Entity\Authors();
    $author->setName('author title: '.uniqid());

    $author->addBook($book);

    $entityManager->persist($author);


    $entityManager->flush();

这样就一切OK了。Doctrine 生成三个查询:create author、create book 和 create links at author_books。

但是我需要在学说onFlush事件中分离和序列化所有实体并将它们保存在 NoSQL 数据库中。然后其他脚本将反序列化所有这些实体并将它们保存到数据库中。并且通过这种方式,Doctrine 只生成了两个 SQL 查询:create book 和 create author。我应该怎么做学说也为链接表生成 SQL 查询?

这里是反序列化实体的部分脚本:

    foreach ($serializedEntities as $serializedEntity)
    {
        $detachedEtity = unserialize($serializedEntity);

        $entity = $entityManager->merge($detachedEtity);

        //$entityManager->persist($entity)

    }

    $entityManager->flush();

更新:我总是得到这样的错误:

    Notice: Undefined index: 000000002289c17b000000003937ebb0 in /app/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 2776
4

2 回答 2

0

只是一个帮助类的例子

// Helper book 类
类 Booking{

      public function __construct($container)
    {
        $this->container = $container;
        $this->club_interval = $container->get('X.interval');
        $this->security_context = $container->get('security.context');
                                    // your entity manager 
        $this->em = $container->get('doctrine.orm.entity_manager');
        $this->session = $container->get('session');
        $this->translator = $container->get('translator');
        $this->event_dispatcher = $container->get('event_dispatcher');
        $this->price = 0;
    }

    public function serialize()
    {
        $this->session->set('booking', serialize($this->booking));
    }

    public function unserialize()
    {
        $b = unserialize($this->session->get('booking'));

        $partner = null;
        foreach ($b->getUsers() as $u) {
            $partner = $this->em->getReference('X:User', $u->getId());;
        }

        $field = $this->em->getReference('X:Field', $b->getField()->getId());
        $user = $this->em->getReference('X:User', $b->getUser()->getId());
        $this->buildBooking($field, $b->getFirstDate(), $b->getEndDate(), $user, $partner, $b->getGuest());
    }

///////////////////////////////////////////////      
// here you can choose your database manager check __constructor      
////////////////////////////////////////////
        public function save(){

        $this->em->persist($this->booking);
        $this->em->flush();

        if ($this->booking->getStatus() >= \X\BookingBundle\Entity\Booking::CONFIRMED) {
            $event = new \X\BookingBundle\Event\FilterBookingEvent($this->booking);
            $this->event_dispatcher->dispatch(\X\BookingBundle\Event\Events::onBookingConfirm, $event);
        }

        $this->em->flush();

        return $this->booking;
    }

因此您可以直接从 Symfony2 控制器中触发此类方法,使用 save() 而不是 persist() 或使用事件调度程序,但更棘手

 if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
        throw new AccessDeniedException();
    }

    $b = $this->get('X_helper.booking');
    $b->unserialize();
    $b->setStatus(\X\BookingBundle\Entity\Booking::PENDING);
    /////////////////////////////////////
    // here use helper's method save instead of flush
    $b->save();

    return $this->redirect($this->generateUrl(' route '));
于 2013-10-09T13:59:10.043 回答
0

您可以使用继承的类定义每个实体的行为,例如:

class FieldRepository extends EntityRepository{
   define all specific methods 
}

所以每个实体都将按照类上的定义保存

您也可以使用助手概念,这将更快更容易地解决您的问题

OnFlush ---> 调度事件 'onFlush' --> 定义助手类的行为

于 2013-10-09T12:34:55.980 回答