如何在 Symfony2 项目中配置扩展 PersistentObject 的 Doctrine2 实体?

2023-08-18php开发问题
24

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

问题描述

我希望能够使用这里描述的 PersistentObject http://www.doctrine-project.org/blog/a-doctrine-orm-odm-base-class.html 在 Symfony2 项目开发期间,避免创建和删除 getter 和在数据库和实体设计不断变化的同时,setter 无休止地进行.

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 框架中使用来自 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() 方法提供了神奇的 getter 和 setter.

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 VendorYourBundleYourEntity;

use DoctrineCommonPersistencePersistentObject; 
use DoctrineCommonPersistenceObjectManager;
use DoctrineORMMapping 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);
   }

   /** 
    * @ORMId  
    * @ORMColumn(type="integer")
    * @ORMGeneratedValue(strategy="AUTO")
    */
   protected $id;

   /**
    * @ORMColumn(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.
    }

    // ...

您可以使用 ...

 $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 实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17