Esempio n. 1
0
 /**
  * Gets an attribute’s value.
  *
  * @param string $name         The attribute’s name.
  * @param bool   $flattenValue
  *
  * @return mixed
  */
 public function getAttribute($name, $flattenValue = false)
 {
     if (isset($this->_attributes[$name])) {
         if ($flattenValue) {
             return ModelHelper::packageAttributeValue($this->_attributes[$name]);
         } else {
             return $this->_attributes[$name];
         }
     }
 }
Esempio n. 2
0
 /**
  * Prepares the model's attribute values to be saved to the database.
  *
  * @return null
  */
 public function prepAttributesForSave()
 {
     $attributes = $this->getAttributeConfigs();
     $attributes['dateUpdated'] = array('type' => AttributeType::DateTime, 'column' => ColumnType::DateTime, 'required' => true);
     $attributes['dateCreated'] = array('type' => AttributeType::DateTime, 'column' => ColumnType::DateTime, 'required' => true);
     foreach ($attributes as $name => $config) {
         $value = $this->getAttribute($name);
         if ($config['type'] == AttributeType::DateTime) {
             // Leaving this in because we want to allow plugin devs to save a timestamp or DateTime object.
             if (DateTimeHelper::isValidTimeStamp($value)) {
                 $value = new DateTime('@' . $value);
             }
         }
         $this->setAttribute($name, ModelHelper::packageAttributeValue($value, true));
     }
     // Populate dateCreated and uid if this is a new record
     if ($this->isNewRecord()) {
         $this->dateCreated = DateTimeHelper::currentTimeForDb();
         $this->uid = StringHelper::UUID();
     }
     // Update the dateUpdated
     $this->dateUpdated = DateTimeHelper::currentTimeForDb();
 }
Esempio n. 3
0
 /**
  * Saves a content model to the database.
  *
  * @param ContentModel $content
  *
  * @return bool
  */
 private function _saveContentRow(ContentModel $content)
 {
     $values = array('id' => $content->id, 'elementId' => $content->elementId, 'locale' => $content->locale);
     $excludeColumns = array_keys($values);
     $excludeColumns = array_merge($excludeColumns, array_keys(DbHelper::getAuditColumnConfig()));
     $fullContentTableName = craft()->db->addTablePrefix($this->contentTable);
     $contentTableSchema = craft()->db->schema->getTable($fullContentTableName);
     foreach ($contentTableSchema->columns as $columnSchema) {
         if ($columnSchema->allowNull && !in_array($columnSchema->name, $excludeColumns)) {
             $values[$columnSchema->name] = null;
         }
     }
     // If the element type has titles, than it's required and will be set. Otherwise, no need to include it (it
     // might not even be a real column if this isn't the 'content' table).
     if ($content->title) {
         $values['title'] = $content->title;
     }
     foreach (craft()->fields->getFieldsWithContent() as $field) {
         $handle = $field->handle;
         $value = $content->{$handle};
         $values[$this->fieldColumnPrefix . $field->handle] = ModelHelper::packageAttributeValue($value, true);
     }
     $isNewContent = !$content->id;
     if (!$isNewContent) {
         $affectedRows = craft()->db->createCommand()->update($this->contentTable, $values, array('id' => $content->id));
     } else {
         $affectedRows = craft()->db->createCommand()->insert($this->contentTable, $values);
     }
     if ($affectedRows) {
         if ($isNewContent) {
             // Set the new ID
             $content->id = craft()->db->getLastInsertID();
         }
         // Fire an 'onSaveContent' event
         $this->onSaveContent(new Event($this, array('content' => $content, 'isNewContent' => $isNewContent)));
         return true;
     } else {
         return false;
     }
 }
Esempio n. 4
0
 /**
  * Saves a content model to the database.
  *
  * @param ContentModel $content
  * @param bool         $validate Whether to call the model's validate() function first.
  * @return bool
  */
 public function saveContent(ContentModel $content, $validate = true)
 {
     if (!$validate || $content->validate()) {
         $values = array('id' => $content->id, 'elementId' => $content->elementId, 'locale' => $content->locale, 'title' => $content->title);
         $allFields = craft()->fields->getAllFields();
         foreach ($allFields as $field) {
             $fieldType = craft()->fields->populateFieldType($field);
             // Only include this value if the content table has a column for it
             if ($fieldType && $fieldType->defineContentAttribute()) {
                 $handle = $field->handle;
                 $value = $content->{$handle};
                 $values[$field->handle] = ModelHelper::packageAttributeValue($value, true);
             }
         }
         $isNewContent = !$content->id;
         if (!$isNewContent) {
             $affectedRows = craft()->db->createCommand()->update('content', $values, array('id' => $content->id));
         } else {
             $affectedRows = craft()->db->createCommand()->insert('content', $values);
             if ($affectedRows) {
                 // Set the new ID
                 $content->id = craft()->db->getLastInsertID();
             }
         }
         if ($affectedRows) {
             // Fire an 'onSaveContent' event
             $this->onSaveContent(new Event($this, array('content' => $content, 'isNewContent' => $isNewContent)));
             return true;
         }
     }
     return false;
 }