如何在Symfony2项目中配置扩展PersistentObject的Doctrine2实体? [英] How do I configure a Doctrine2 entity which extends PersistentObject within Symfony2 project?

查看:104
本文介绍了如何在Symfony2项目中配置扩展PersistentObject的Doctrine2实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这里描述的PersistentObject http://www.doctrine-project.org/blog/a-doctrine-orm-odm-base-class.html ,以避免创建和删除getter和在数据库和实体设计的过程中,无限制的设置者。

I would like to be able to use the PersistentObject described here http://www.doctrine-project.org/blog/a-doctrine-orm-odm-base-class.html during development of a Symfony2 project, to avoid creating and deleting getters and setters endlessly whilst the database and entity design are in flux.

在Symfony2项目中,在简要文档(代码引用下面)中提到的配置ObjectManager之前, )?是否应该在控制器中,如果是这样,它会是什么样子?

Where in a Symfony2 project does one 'configure' the ObjectManager, as suggested in the brief documentation (code quote below)? Should it be in the controller, and if so, what would it look like?

 $entityManager = EntityManager::create(...);
 PersistentObject::setObjectManager($entityManager);

我找不到任何工作示例(尽管我在stackoverflow中找到了Zend2框架的这个并行示例: 在Zend Framework中使用Doctrine中的PersistentObject

I cannot find any working examples (although I did find this parallels example for the Zend2 framework on stackoverflow: Using PersistentObject from Doctrine in Zend Framework

感谢您的时间!

推荐答案

PersistentObject 是一个您不需要手动持久化的对象,使用php的 __ call()方法

The PersistentObject is an object which you don't manually have to persist. It thereby provides magic getters and setters using php's __call() method.

您只需扩展对象即可在您的实体类中并将其用于控制​​器中,而无需生成getter和setter。

You simply extend the Object in your entity class and use it inside your controller. without the need to generate getters and setters.

示例实体

<?php
namespace Vendor\YourBundle\YourEntity;

use Doctrine\Common\Persistence\PersistentObject; 
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Mapping as ORM;

class YourEntity extends PersistentObject
{

   // i added the constructor in order to save the setObjectManager() 
   // call in the the controller

   public function __construct(ObjectManager $om)
   {
       $this->setObjectManager($om);
   }

   /** 
    * @ORM\Id  
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
   protected $id;

   /**
    * @ORM\Column(type="string", length=100)
    */
   protected $name;

   // ... more properties

}

示例控制器操作

class YourController extends Controller
{
    public function yourAction($name)
    {
        $em = $this->get('doctrine')->getManager('default');

        $entity = new YourEntity($em);   // __construct calls setObjectManager($em)

        $entity->setName($name);         // magic setter is used here

        // ... no need for $em->persist($entity);

        $em->flush();                    // $entity is being persisted.
    }

    // ...

你得到在一个symfony控制器中使用以下之一的学说实体经理:

You get the doctrine entity manager inside a symfony controller using one of ...

 $em = $this->get('doctrine')->getManager();          // gets default manager
 $em = $this->get('doctrine')->getManager('default'); // same as above
 $em = $this->getDoctrine()->getManager();            // using alias 

这篇关于如何在Symfony2项目中配置扩展PersistentObject的Doctrine2实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆