Example #1
0
 /**
  *
  * @param string $objectName
  * @param booleab $forceConfig, optional
  */
 public function __construct($objectName, $forceConfig = true)
 {
     $this->_objectName = $objectName;
     $this->_objectConfig = Db_Object_Config::getInstance($objectName, $forceConfig);
     $this->_model = Model::factory($objectName);
     $this->_db = $this->_model->getDbConnection();
     $this->_dbPrefix = $this->_model->getDbPrefix();
 }
Example #2
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;
 }