开发者

Override Doctrine_Record validate method with a Doctrine_Template

开发者 https://www.devze.com 2023-01-31 19:27 出处:网络
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.

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 n开发者_C百科ew methods declared into my Doctrine_Template.

Could you suggest me a solution for this problem?

Tnx!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号