Extending sfDoctrineRecord in symfony

五迷三道 提交于 2019-12-05 20:23:25

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? :)

I suppose the proper way would probably be to add a Doctrine_Template to the models in question, however you would need to define it as a behavior for every model in your schema.yml

class MyMethodsTemplate extends Doctrine_Template
{
   public function customMethod1(){
      $model = $this->getInvoker();
      //do stuff with model
   }

   public function customMethod2(){
      $model = $this->getInvoker();
      //do stuff with model
   }
}

And then in your schema.yml:

ModelName:
  actAs:
   MyMethodTemplate: ~
  # the rest of your definition

After you rebuild you should be able to call:

$model = new ModelName();
$model->customMethod1();
$model->customMethod2(); 

Of course Doctrine templates and listeners are much more powerful than that. You should take a look at the documentation for decent overview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!