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 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面