/** * Adds `id`, `dateCreated`, `date_update`, and `uid` columns to $columns, packages up the column definitions into * strings, and then passes it back to CDbCommand->createTable(). * * @param string $table * @param array $columns * @param null $options * @param bool $addIdColumn * @param bool $addAuditColumns * * @return int */ public function createTable($table, $columns, $options = null, $addIdColumn = true, $addAuditColumns = true) { $table = $this->getConnection()->addTablePrefix($table); $columns = array_merge($addIdColumn ? array('id' => ColumnType::PK) : array(), $columns, $addAuditColumns ? DbHelper::getAuditColumnConfig() : array()); foreach ($columns as $col => $settings) { $columns[$col] = DbHelper::generateColumnDefinition($settings); } // Create the table return parent::createTable($table, $columns, $options); }
/** * 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; } }