Example #1
0
 /**
  * Set the object field val
  * @param string $name
  * @param mixed $value
  * @return bool
  * @throws Exception
  */
 public function set($name, $value)
 {
     if ($this->_acl) {
         $this->_checkCanEdit();
     }
     $propConf = $this->_config->getFieldConfig($name);
     $validator = $this->getConfig()->getValidator($name);
     if ($validator && !call_user_func_array(array($validator, 'validate'), array($value))) {
         throw new Exception('Invalid value for field ' . $name);
     }
     /*
      * Validate value by fields type in config
      */
     if ($this->_config->isMultiLink($name)) {
         if (is_array($value) && !empty($value[0])) {
             if (!$this->_validateLink($name, $value)) {
                 throw new Exception('Invalid property value');
             }
             $value = $this->_collectLinksData($name, $value);
         } else {
             $value = '';
         }
     } elseif ($this->_config->isDictionaryLink($name)) {
         if ($this->_config->isRequired($name) && !strlen($value)) {
             throw new Exception('Field ' . $name . ' cannot be empty');
         }
         if (strlen($value)) {
             $fieldConfig = $this->_config->getFieldConfig($name);
             $dictionary = Dictionary::getInstance($fieldConfig['link_config']['object']);
             if (!$dictionary->isValidKey($value)) {
                 throw new Exception('Invalid dictionary value [' . $name . ']');
             }
         }
     } elseif ($this->_config->isLink($name)) {
         if (is_object($value)) {
             if ($value instanceof Db_Object) {
                 if ($this->_config->isObjectLink($name)) {
                     if (!$value->isInstanceOf($this->getLinkedObject($name))) {
                         throw new Exception('Invalid value type for field ' . $name . ' expects ' . $this->getLinkedObject($name) . ', ' . $value->getName() . ' passed');
                     }
                 }
                 $value = $value->getId();
             } else {
                 $value = $value->__toString();
             }
         }
         if (is_array($value)) {
             throw new Exception('Invalid value for field ' . $name);
         }
         if ($this->_config->isRequired($name) && !strlen($value)) {
             throw new Exception('Field ' . $name . ' cannot be empty');
         }
         $value = intval($value);
         if ($value != 0 && !$this->_validateLink($name, $value)) {
             throw new Exception('Invalid value for field ' . $name);
         }
         if ($value == 0) {
             $value = null;
         }
     } elseif ($this->_config->isBoolean($name)) {
         $value = intval((bool) $value);
     } elseif (is_null($value) && $this->_config->isNull($name)) {
         $value = null;
     } else {
         $value = Db_Object_Property::filter($propConf, $value);
     }
     if (isset($propConf['db_len']) && $propConf['db_len']) {
         if (mb_strlen($value, 'UTF-8') > $propConf['db_len']) {
             throw new Exception('The field value exceeds the allowable length [' . $name . ']');
         }
         if ($propConf['db_type'] == 'bit' && (strlen($value) > $propConf['db_len'] || strlen($value) < $propConf['db_len'])) {
             throw new Exception('Invalid length for bit value [' . $name . ']');
         }
     }
     if (isset($this->_data[$name]) && $this->_data[$name] === $value) {
         if (isset($this->_updates[$name])) {
             unset($this->_updates[$name]);
         }
         return true;
     }
     $this->_updates[$name] = $value;
     return true;
 }
Example #2
0
 /**
  * Get property SQL query
  *
  * @param array $data
  * @return string
  */
 protected function _proppertySql($name, $data)
 {
     $property = new Db_Object_Property($name);
     $property->setData($data);
     return $property->__toSql();
 }