Exemplo n.º 1
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;
 }