Zend_Validate_Db_RecordExists与Doctrine 2?

时间:2011-10-31 08:45:57

标签: zend-framework zend-form doctrine-orm zend-db zend-validate

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

例如,当用户输入新项目时,我需要验证重复项目是否已存在。使用Zend_Db很容易通过在我的表单上添加Db_NoRecordExists验证器来实现。

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

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

有人建议使用这些Zend验证器并将Doctrine 2 DBAL配置为数据库连接/资源吗?

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:2)

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

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

我希望这些对您有用。

由于

安德鲁