/** * Validate unique fields, object field groups * Returns errors array or returns false, is used for ExtJS forms * @property boolean $new * @return mixed false / array */ public function validateUniqueValues() { $uniqGroups = array(); foreach ($this->_config->get('fields') as $k => $v) { if ($k === $this->_primaryKey) { continue; } if (!$this->_config->isUnique($k)) { continue; } $value = $this->get($k); if (is_array($value)) { $value = serialize($value); } if (is_array($v['unique'])) { foreach ($v['unique'] as $val) { if (!isset($uniqGroups[$val])) { $uniqGroups[$val] = array(); } $uniqGroups[$val][$k] = $value; } } else { $v['unique'] = strval($v['unique']); if (!isset($uniqGroups[$v['unique']])) { $uniqGroups[$v['unique']] = array(); } $uniqGroups[$v['unique']][$k] = $value; } } if (empty($uniqGroups)) { return false; } $db = $this->_model->getDbConnection(); foreach ($uniqGroups as $group) { $sql = $db->select()->from($this->_model->table(), array('count' => 'COUNT(*)')); if ($this->getId()) { $sql->where(' ' . $db->quoteIdentifier($this->_primaryKey) . ' != ?', $this->getId()); } foreach ($group as $k => $v) { if ($k === $this->_primaryKey) { continue; } $sql->where($db->quoteIdentifier($k) . ' =?', $v); } $count = $db->fetchOne($sql); if ($count > 0) { foreach ($group as $k => &$v) { $v = Lang::lang()->get('SB_UNIQUE'); } unset($v); return $group; } } return false; }
public function refreshTableInfo() { $conName = $this->_objectConfig->get('connection'); $this->_db = $this->_dbManager->getDbConnection($conName); if ($this->_objectConfig->hasDbPrefix()) { $this->_dbPrefix = $this->_dbManager->getDbConfig($conName)->get('prefix'); } else { $this->_dbPrefix = ''; } $this->_table = $this->_objectConfig->get('table'); }
/** * Prepare DB engine update SQL * * @return boolean Ambigous string> */ public function prepareEngineUpdate() { $config = $this->_objectConfig->__toArray(); $conf = $this->_db->fetchRow('SHOW TABLE STATUS WHERE `name` = "' . $this->_model->table() . '"'); if (!$conf || !isset($conf['Engine'])) { return false; } if (strtolower($conf['Engine']) === strtolower($this->_objectConfig->get('engine'))) { return false; } return $this->changeTableEngine($this->_objectConfig->get('engine'), true); }