Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * Get object by unique field
  *
  * @param string $fieldName
  * @param string $value
  * @param array $fields - optional
  * @throws Exception
  * @return array
  */
 public function getItemByUniqueField($fieldName, $value, $fields = '*')
 {
     if (!$this->_objectConfig->isUnique($fieldName)) {
         $eText = 'getItemByUniqueField field "' . $fieldName . '" [' . $this->_objectConfig->getName() . '] should be unique';
         $this->logError($eText);
         throw new Exception($eText);
     }
     $sql = $this->_dbSlave->select()->from($this->table(), $fields);
     $sql->where($this->_dbSlave->quoteIdentifier($fieldName) . ' = ?', $value);
     return $this->_dbSlave->fetchRow($sql);
 }