public function validate($value, $primary, array $row, Entity\Field $field) { $entity = $field->getEntity(); $primaryNames = $entity->getPrimaryArray(); $query = new Entity\Query($entity); $query->setSelect($primaryNames); $query->setFilter(array('=' . $field->getName() => $value)); $query->setLimit(2); $result = $query->exec(); while ($existing = $result->fetch()) { // check primary foreach ($existing as $k => $v) { if (!isset($primary[$k]) || $primary[$k] != $existing[$k]) { return $this->getErrorMessage($value, $field); } } } return true; }
public static function getInstanceByQuery(Query $query, &$entity_name = null) { if ($entity_name === null) { $entity_name = 'Tmp' . randString(); } elseif (!preg_match('/^[a-z0-9_]+$/i', $entity_name)) { throw new Main\ArgumentException(sprintf('Invalid entity name `%s`.', $entity_name)); } $query_string = '(' . $query->getQuery() . ')'; $query_chains = $query->getChains(); $replaced_aliases = array_flip($query->getReplacedAliases()); // generate fieldsMap $fieldsMap = array('TMP_ID' => array('data_type' => 'integer', 'primary' => true)); foreach ($query->getSelect() as $k => $v) { if (is_array($v)) { // expression $fieldsMap[$k] = array('data_type' => $v['data_type']); } else { $fieldDefinition = is_numeric($k) ? $v : $k; $fieldsMap[$fieldDefinition] = array('data_type' => $query_chains[$fieldDefinition]->getLastElement()->getValue()->getDataType()); } if (isset($replaced_aliases[$k])) { $fieldsMap[$k]['column_name'] = $replaced_aliases[$k]; } } // generate class content $eval = 'class ' . $entity_name . 'Table extends ' . __NAMESPACE__ . '\\DataManager {' . PHP_EOL; $eval .= 'public static function getMap() {' . PHP_EOL; $eval .= 'return ' . var_export($fieldsMap, true) . ';' . PHP_EOL; $eval .= '}'; $eval .= 'public static function getTableName() {' . PHP_EOL; $eval .= 'return ' . var_export($query_string, true) . ';' . PHP_EOL; $eval .= '}'; $eval .= 'public static function getFilePath() {' . PHP_EOL; $eval .= 'return null;' . PHP_EOL; $eval .= '}'; $eval .= '}'; eval($eval); return self::getInstance($entity_name); }