dsgixl5195 2010-12-21 09:26
浏览 71
已采纳

使用Doctrine_Template覆盖Doctrine_Record验证方法

in my Symfony project I would use a new strategy for manage the data form.

I don't want use the Symfony Form object, but I want use the model to build them.

I don't want to redeclare the Base Doctrine_Record class, so I wrote a new Doctrine_Template: ExtendedModel.

In the ExtendeModel template I've new objects and methods, but I need to override the validate() function of Doctrine_Record.

I tried with

class ExtendedModel extends Doctrine_Template {

[...]

public $validatorSchema;

public function setValidatorSchema(sfValidatorSchema $validatorSchema) {
    $this->validatorSchema = $validatorSchema;
}

public function getValidatorSchema() {
    return $this->validatorSchema;
}

public function validate() {
    $this->getInvoker()->setup();

    $errorStack = $this->getInvoker()->getErrorStack();
    if ($this->getValidatorSchema()) {
        try {
            $this->getValidatorSchema()->addOption('allow_extra_fields', true);
            $this->getValidatorSchema()->clean($this->getInvoker()->toArray(false));
        } catch (sfValidatorErrorSchema $errorSchema) {
            $errorStack = $this->getInvoker()->getErrorStack();
            foreach ($errorSchema->getErrors() as $key => $error) {
                /* @var $error sfValidatorError  */
                $errorStack->add($key, $error->getMessage());
            }
        }
    }
    $this->getInvoker()->validate();
}

}

but Doctrine use the original validate() function.

I want to override some Doctrine_Record functions with a new methods declared into my Doctrine_Template.

Could you suggest me a solution for this problem?

Tnx!

  • 写回答

1条回答 默认 最新

  • drtoaamk20278 2010-12-21 14:37
    关注

    Templates do not override Doctrine_Record methods, they are only fallbacks invoked via the PHP magic __call method when a native method isn't found.

    To do this, you need to have your own class in the Doctrine_Record inheritance chain. Fortunately, this is pretty easy:

    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'));
    }
    

    This is copied/pasted from my previous answer to a similar question. You'll have to rebuild the model as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题