Exemple #1
0
 /**
  * Sets the named attribute value. It can also set BELONGS_TO and HAS_ONE
  * relations if you pass a ActiveRecord
  *
  * You may also use $this->AttributeName to set the attribute value.
  *
  * @param string $name the attribute name
  * @param mixed $value the attribute value.
  * @return boolean whether the attribute exists and the assignment is conducted successfully
  * @see hasAttribute
  */
 public function setAttribute($name, $value, $format = false)
 {
     if ($this->loadingFromDatabase) {
         //skip fancy features when loading from the database.
         $this->_attributes[$name] = $value;
         return true;
     }
     if ($format) {
         $value = $this->formatInput($name, $value);
     }
     if (isset($this->columns[$name])) {
         if (GO::config()->debug) {
             if ($this->columns[$name]['gotype'] != 'file' && is_object($value) || is_array($value)) {
                 throw new \Exception($this->className() . "::setAttribute : Invalid attribute value for " . $name . ". Type was: " . gettype($value));
             }
         }
         $relationFieldName = $this->_getAclFk();
         if ($name === $relationFieldName) {
             $aclWasOverwritten = $this->isAclOverwritten();
         }
         //normalize CRLF to prevent issues with exporting to vcard etc.
         if (isset($this->columns[$name]['gotype']) && ($this->columns[$name]['gotype'] == 'textfield' || $this->columns[$name]['gotype'] == 'textarea')) {
             $value = \GO\Base\Util\String::normalizeCrlf($value, "\n");
         }
         if ((!isset($this->_attributes[$name]) || (string) $this->_attributes[$name] !== (string) $value) && !$this->isModified($name)) {
             $this->_modifiedAttributes[$name] = isset($this->_attributes[$name]) ? $this->_attributes[$name] : false;
             //				GO::debug("Setting modified attribute $name to ".$this->_modifiedAttributes[$name]);
             //				GO::debugCalledFrom(5);
         }
         $this->_attributes[$name] = $value;
         // Set the ACL_ID if the relation acl FK changed and ACL is overwritten
         if ($name === $relationFieldName && !$aclWasOverwritten && $this->aclOverwrite() && $this->isModified($name)) {
             if (!empty($this->{$name})) {
                 $modelWithAcl = $this->findRelatedAclModel();
                 if ($modelWithAcl) {
                     $this->{$this->aclOverwrite()} = $modelWithAcl->findAclId();
                 }
             }
         }
     } else {
         if ($r = $this->getRelation($name)) {
             if ($r['type'] == self::BELONGS_TO || $r['type'] == self::HAS_ONE) {
                 if ($value instanceof ActiveRecord) {
                     $cacheKey = $this->_getRelatedCacheKey($r);
                     $this->_relatedCache[$cacheKey] = $value;
                 } else {
                     throw new \Exception("Value for relation '" . $name . "' must be a ActiveRecord '" . gettype($value) . "' was given");
                 }
             } else {
                 throw new \Exception("Can't set one to many relation!");
             }
         } else {
             parent::__set($name, $value);
         }
     }
     return true;
 }