On Thu, Feb 11, 2010 at 5:03 PM, Саша Стаменковић <[email protected]>wrote:
> Can we see the source?
>
> Regards,
> Saša Stamenković
>
>
>
Sure...
The next thing however that I plan to do is likewise add support for
multiple columns in the 'exclude' option.
<?php
class Model_Validate_Db_NoRecordExists extends Zend_Validate_Db_Abstract {
protected $_fields;
function __construct($options) {
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
if (array_key_exists('fields',$options)) {
$this->_fields = $options['fields'];
// to prevent parent from throwing missing option exception:
$options['field'] = $this->_fields;
}
parent::__construct($options);
}
function isValid($value,$context=null) {
$this->_setValue($value);
/** FROM Zend_Validate_Db_Abstract: ///////////
* Check for an adapter being defined. if not, fetch the default
adapter.
*/
if ($this->_adapter === null) {
$this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
if (null === $this->_adapter) {
throw new Zend_Validate_Exception('No database adapter
present');
}
}
if (! $this->_fields) { // plural !
$result = $this->_query($value);
if ($result) {
$this->_error(self::ERROR_RECORD_FOUND);
return false;
} else {
return true;
}
}
/**
* Build select object
*/
$select = new Zend_Db_Select($this->_adapter);
$select->from($this->_table, $this->_fields, $this->_schema);
// sole difference from parent implementation: add multiple WHERE
clauses
// note: we are assuming form field name == column name
foreach ($this->_fields as $field) {
$select->where("$field = ?",$context[$field]);
}
if ($this->_exclude !== null) {
if (is_array($this->_exclude)) {
$select->where($this->_adapter->quoteIdentifier($this->_exclude['field']).'
!= ?', $this->_exclude['value']);
} else {
$select->where($this->_exclude);
}
}
$select->limit(1);
$result = $this->_adapter->fetchRow($select, array(),
Zend_Db::FETCH_ASSOC);
if ($result) {
$this->_error(self::ERROR_RECORD_FOUND);
return false;
} else {
return true;
}
}
}
--
Support real health care reform:
http://phimg.org/
--
David Mintz
http://davidmintz.org/