Zend_Validate_Db_RecordExists with Doctrine 2?

I use Doctrine 2 in a Zend Framework application and require functionality similar to Zend_Validate_Db_RecordExists and Zend_Validate_Db_NoRecordExists.

For example, when a user enters a new item, I need to verify that the duplicate entry does not exist yet. This is easy to do with Zend_Db by adding the Db_NoRecordExists validator to my forms.

I tried to implement the proposed authentication solution here , but I can’t understand how they communicate with Doctrine to extract entities (I suspect that this approach may no longer work the 1.x post-doctrine).

The FAQ section of the Doctrine manual suggests calling contains () from client code, but this only applies to collections, and if possible, I would like to process all my form validations sequentially from my form models.

Can someone suggest a way to use these Zend validators with DBAL Doctrine 2 configured as a database connection / resource?

+4
source share
2 answers

It is pretty simple, really.

I have several validators like Zend_Validate that communicate with Doctrine ORM, so I have an abstract class from which they come off.

Here's the abstract class:

<?php namespace TimDev\Validate\Doctrine; abstract class AbstractValidator extends \Zend_Validate_Abstract{ /** * @var Doctrine\ORM\EntityManager */ private $_em; public function __construct(\Doctrine\ORM\EntityManager $em){ $this->_em = $em; } public function em(){ return $this->_em; } } 

Here is my NoEntityExists validator:

 <?php namespace TimDev\Validate\Doctrine; class NoEntityExists extends AbstractValidator{ private $_ec = null; private $_property = null; private $_exclude = null; const ERROR_ENTITY_EXISTS = 1; protected $_messageTemplates = array( self::ERROR_ENTITY_EXISTS => 'Another record already contains %value%' ); public function __construct($opts){ $this->_ec = $opts['class']; $this->_property = $opts['property']; $this->_exclude = $opts['exclude']; parent::__construct($opts['entityManager']); } public function getQuery(){ $qb = $this->em()->createQueryBuilder(); $qb->select('o') ->from($this->_ec,'o') ->where('o.' . $this->_property .'=:value'); if ($this->_exclude !== null){ if (is_array($this->_exclude)){ foreach($this->_exclude as $k=>$ex){ $qb->andWhere('o.' . $ex['property'] .' != :value'.$k); $qb->setParameter('value'.$k,$ex['value'] ? $ex['value'] : ''); } } } $query = $qb->getQuery(); return $query; } public function isValid($value){ $valid = true; $this->_setValue($value); $query = $this->getQuery(); $query->setParameter("value", $value); $result = $query->execute(); if (count($result)){ $valid = false; $this->_error(self::ERROR_ENTITY_EXISTS); } return $valid; } } 

Used in the context of Zend_Form (which has an em () method similar to an abstract class):

 /** * Overrides superclass method to add just-in-time validation for NoEntityExists-type validators that * rely on knowing the id of the entity in question. * @param type $data * @return type */ public function isValid($data) { $unameUnique = new NoEntityExists( array('entityManager' => $this->em(), 'class' => 'PMS\Entity\User', 'property' => 'username', 'exclude' => array( array('property' => 'id', 'value' => $this->getValue('id')) ) ) ); $unameUnique->setMessage('Another user already has username "%value%"', NoEntityExists::ERROR_ENTITY_EXISTS); $this->getElement('username')->addValidator($unameUnique); return parent::isValid($data); } 
+3
source

Check out the RecordExists.php and NoRecordExists.php classes in my project: -

https://github.com/andyfenna/AJF-IT/tree/master/library/AJFIT/Validate

I hope they help you.

thanks

Andrew

+2
source

Source: https://habr.com/ru/post/888718/


All Articles