在symfony中扩展sfDoctrineRecord [英] Extending sfDoctrineRecord in symfony

查看:111
本文介绍了在symfony中扩展sfDoctrineRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的symfony项目的一些实例类中添加了一些功能,我希望我的所有实例类都有。如果我没有关于编辑核心symfony安装的任何质疑,我只需将我的方法直接添加到 sfDoctrineRecord 类中。但是我不想这样做,当然,因为我的更改将会在升级时出现故障,而且我的更改也不会适用于其他项目。



如果我想要在symfony的所有实例类中添加一些功能,那么正确的方式是什么?



(PS当我说实例类时,我的意思是像 lib / model / doctrine / Customer.class.php

解决方案

步骤:


  1. 创建myDoctrineRecord

     抽象类myDoctrineRecord extends sfDoctrineRecord 
    {
    public function commonRecordMethod(){}
    }

    我将这个文件放在lib / record中,但是你可以把它放在自动装载机看到的任何地方。


  2. Set Symfony在你的ProjectConfiguration的configureDoctrine回调中使用这个类:

      public function configureDoctrine(Doctrine_Manager $ manager)
    {
    sfConfig :: set('doctrine_model_builder_options',array('baseClassName'=> myDoctrineRecord));
    }


就是这样! Symfony不是很棒吗:)


I've added some functionality to some of my instance classes in my symfony project that I want ALL of my instance classes to have. If I didn't have any qualms about editing the core symfony installation, I would just add my methods directly to the sfDoctrineRecord class. But I don't want to do that, of course, because my changes would break on upgrade, plus my changes wouldn't port well to other projects.

If I want to add certain functionality to all my instance classes in symfony, what's the "right" way to do that?

(P.S. When I say "instance class", I mean something like lib/model/doctrine/Customer.class.php.)

解决方案

Steps:

  1. Create myDoctrineRecord

    abstract class myDoctrineRecord extends sfDoctrineRecord
    {
      public function commonRecordMethod() { }
    }
    

    I place this file in lib/record, but you can put it anywhere that the autoloader will see it.

  2. Set Symfony to use this class in the configureDoctrine callback of your ProjectConfiguration:

     public function configureDoctrine(Doctrine_Manager $manager)
     {
       sfConfig::set('doctrine_model_builder_options', array('baseClassName' => 'myDoctrineRecord'));
     }
    

That's it! Isn't Symfony great? :)

这篇关于在symfony中扩展sfDoctrineRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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