public function validate($db, &$row) { $sql = sprintf('select %s from %s', $this->foreignKeyMapping[0]->foreign, $this->foreignTable); $sep = ' where '; foreach ($this->foreignKeyMapping as $fkm) { $sql .= sprintf('%s%s = ?', $sep, $fkm->foreign); if ($sep != ' and ') { $sep = ' and '; } } $ps = new PreparedStatement($sql, 0, 1); foreach ($this->foreignKeyMapping as $fkm) { $vn = $fkm->local; $val = property_exists($row, $vn) ? $row->{$vn} : ''; // If we're set to allow nulls and any value is null, don't validate. if ($this->allowNULL && $val === null) { return ''; } switch ($fkm->type) { case 'int': $ps->setInt($val); break; case 'float': $ps->setFloat($val); break; case 'double': $ps->setDouble($val); break; case 'boolean': $ps->setBoolean($val); break; case 'string': $ps->setString($val); break; case 'binary': $ps->setBinary($val); break; default: throw new Exception(sprintf('Unexpected PreparedStatement data type: %s', $fkm->type)); } } if (!$db->fetchObject($db->executeQuery($ps), true)) { if ($this->errorMsg != '') { return $this->errorMsg; } return _t('ForeignKeyValidator.class.errorMsg.mustMatchAnExistingEntry'); } return ''; }
public function validate($db, &$row) { $sql = sprintf('select %s from %s', $this->fields[0]->field, $this->table); $sep = ' where '; foreach ($this->fields as $fld) { $qo = $fld->queryOperator; if ($qo == 'beginsWith' || $qo == 'contains' || $qo == 'endsWith') { $qo = 'like'; } $sql .= sprintf('%s%s %s ?', $sep, $fld->field, $qo); if ($sep != ' and ') { $sep = ' and '; } } $ps = new PreparedStatement($sql, 0, 1); foreach ($this->fields as $fld) { $vn = $fld->field; $val = property_exists($row, $vn) ? $row->{$vn} : ''; // If we're set to allow nulls and any value is null, don't validate. if ($this->allowNULL && $val === null) { return ''; } switch ($fld->type) { case 'int': $ps->setInt($val); break; case 'float': $ps->setFloat($val); break; case 'double': $ps->setDouble($val); break; case 'boolean': $ps->setBoolean($val); break; case 'string': switch ($fld->queryOperator) { case 'beginsWith': $ps->setString($val . '%'); break; case 'contains': $ps->setString('%' . $val . '%'); break; case 'endsWith': $ps->setString('%' . $val); break; default: $ps->setString($val); break; } break; case 'binary': $ps->setBinary($val); break; default: throw new Exception(sprintf('Unexpected PreparedStatement data type: %s', $fld->type)); } } if ($db->fetchObject($db->executeQuery($ps), true)) { if ($this->errorMsg != '') { return $this->errorMsg; } return _t('NoDuplicatesValidator.class.errorMsg.anEntryAlreadyExistsWithThisValue'); } return ''; }