4

我在 Zend Framework 应用程序中使用 Doctrine 2,并且需要类似于 Zend_Validate_Db_RecordExists 和 Zend_Validate_Db_NoRecordExists 的功能。

例如,当用户输入一个新项目时,我需要验证重复的条目不存在。通过在我的表单上添加 Db_NoRecordExists 验证器,使用 Zend_Db 很容易做到这一点。

我尝试实现此处提出的自定义验证器解决方案,但我无法弄清楚他们如何与 Doctrine 通信以检索实体(我怀疑这种方法在 Doctrine 1.x 之后可能不再适用)。

Doctrine 手册的FAQ部分建议从客户端代码调用 contains() ,但这仅涵盖集合,如果可能的话,我想在我的表单模型中一致地处理我的所有表单验证。

任何人都可以建议一种将这些 Zend 验证器与配置为数据库连接/资源的 Doctrine 2 DBAL 一起使用的方法吗?

4

2 回答 2

3

这很简单,真的。

我有一些与 Doctrine ORM 对话的 Zend_Validate 类型的验证器,所以我有一个抽象类,它们源自它们。

这是抽象类:

<?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;
  }
}

这是我的 NoEntityExists 验证器:

<?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;

  }
}

在 Zend_Form 的上下文中使用(它有一个类似于上面抽象类的 em() 方法):

/**
   * 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);
}
于 2011-10-31T19:40:46.233 回答
2

查看我项目中的 RecordExists.php 和 NoRecordExists.php 类:-

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

我希望这些对你有用。

谢谢

安德鲁

于 2011-10-31T08:50:22.540 回答