Nachdem ich mich lange gewundert hatte, warum die Validierung eines eigentlich fehlerhaften Objekts
$niveau = Doctrine::getTable('Niveaus') ->findOneByProfilIdAndKompetenzId( $profil_id, $id ); $niveau->setNiveau(20); // Es sind nur Werte im Bereich 1-5 erlaubt $niveau->isValid(); // gibt dennoch true zurück |
nie fehlgeschlagen ist, bin ich jetzt hinter das Ganze gestiegen:
In der Konfiguration des symfony-Plugins sfDoctrinePlugin (das standardmäßig in symfony enthalten ist) steht ganz einfach still und heimlich:
// lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/config/sfDoctrinePluginConfiguration.class.php $manager = Doctrine_Manager::getInstance(); ... $manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_NONE); |
Dies hat zur Folge, dass die Objektvalidierung eines Doctrine_Records nicht aktiv ist und diese einfach "übergangen" wird.
$niveau->isValid(); |
gibt in diesem Fall einfach "true" zurück. Siehe:
// lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Record.php /** * tests validity of the record using the current data. * * @param boolean $deep run the validation process on the relations * @param boolean $hooks invoke save hooks before start * @return boolean whether or not this record is valid */ public function isValid($deep = false, $hooks = true) { if ( ! $this->_table->getAttribute(Doctrine_Core::ATTR_VALIDATE)) { return true; } ... |
Abhilfe:
Ich habe für mein Projekt die Validierung global aktiviert, so dass ich sie in jeder Applikation des Projekts verwenden kann.
// config/ProjectConfiguration.class.php // configure doctrine engine public function configureDoctrine(Doctrine_Manager $manager) { // enable validation for doctrine records $manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL); } |
Fertig!