Rev 1 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Validator;
use Laminas\Validator\Db\RecordExists;
use Laminas\Db\Sql\Select;
use Laminas\Db\Sql\TableIdentifier;
class RecordExistsMultiFields extends RecordExists
{
public function __construct($options = null)
{
parent::__construct($options);
}
public function isValid($value)
{
/*
* Check for an adapter being defined. If not, throw an exception.
*/
if (null === $this->adapter) {
throw new \Exception('No database adapter present');
}
$select = new Select();
$tableIdentifier = new TableIdentifier($this->getTable(), $this->getSchema());
$select->from($this->getTable())->columns(array($this->getField()));
$select->where->equalTo($this->getField(), null);
$custom_fields = $this->getOption('custom_fields');
if(is_array($custom_fields)) {
foreach($custom_fields as $key_custom => $value_custom)
{
if(!is_null($value_custom)) {
$select->where->equalTo($key_custom, $value_custom);
}
}
}
if ($this->exclude !== null)
{
if (is_array($this->exclude))
{
$select->where->notEqualTo(
$this->exclude['field'],
$this->exclude['value']
);
}
else
{
$select->where($this->exclude);
}
}
$this->setSelect($select);
// echo $select->getSqlString($this->adapter->platform); exit;
$valid = true;
$this->setValue($value);
$result = $this->query($value);
if (!$result)
{
$valid = false;
$this->error(self::ERROR_NO_RECORD_FOUND);
}
return $valid;
}
}