/** * {@inheritDoc} * @see IExportFormat::format() */ public function format(CActiveRecord $record, array $data, array $templates = array()) { if (empty($data)) { throw new CDbException(Yii::t('yii', 'Can not generate multiple insert command with empty data set.')); } $templates = array_merge(array('main' => "INSERT INTO {{tableName}} ({{columnInsertNames}}) VALUES \n{{rowInsertValues}};\n", 'columnInsertValue' => '{{value}}', 'columnInsertValueGlue' => ', ', 'rowInsertValue' => '({{columnInsertValues}})', 'rowInsertValueGlue' => ",\n", 'columnInsertNameGlue' => ', '), $templates); $table = $record->tableSchema; if ($table === null) { throw new CDbException(Yii::t('yii', 'Table "{table}" does not exist.', array('{table}' => $record->tableName()))); } $tableName = $table->rawName; $columns = array(); foreach ($data as $rowData) { foreach ($rowData as $columnName => $columnValue) { if (!in_array($columnName, $columns, true)) { if ($table->getColumn($columnName) !== null) { $columns[] = $columnName; } } } } $columnInsertNames = array(); foreach ($columns as $name) { $columnInsertNames[$name] = $record->getDbConnection()->quoteColumnName($name); } $columnInsertNamesSqlPart = implode($templates['columnInsertNameGlue'], $columnInsertNames); $rowInsertValues = array(); foreach ($data as $rowData) { $columnInsertValues = array(); foreach ($columns as $columnName) { /* @var $column CDbColumnSchema */ $column = $table->getColumn($columnName); $columnValue = array_key_exists($columnName, $rowData) ? $rowData[$columnName] : new CDbException('NULL'); if ($columnValue instanceof CDbExpression) { $columnInsertValue = $columnValue->expression; // in reverse order to prevent precocious replacements on param values foreach (array_reverse($columnValue->params) as $columnValueParamName => $columnValueParam) { $secureColumnParamValue = $this->secureOutput($record->getDbConnection(), $columnValueParam); $columnInsertValue = strtr(':' . $columnValueParamName, $secureColumnParamValue, $columnInsertValue); } } else { $columnInsertValue = $column->typecast($columnValue); if ($columnInsertValue === '' && $column->allowNull) { $columnInsertValue = null; } $columnInsertValue = $this->secureOutput($record->getDbConnection(), $columnInsertValue); } $columnInsertValues[] = strtr($templates['columnInsertValue'], array('{{column}}' => $columnInsertNames[$columnName], '{{value}}' => $columnInsertValue)); } $rowInsertValues[] = strtr($templates['rowInsertValue'], array('{{tableName}}' => $tableName, '{{columnInsertNames}}' => $columnInsertNamesSqlPart, '{{columnInsertValues}}' => implode($templates['columnInsertValueGlue'], $columnInsertValues))); } $sql = strtr($templates['main'], array('{{tableName}}' => $tableName, '{{columnInsertNames}}' => $columnInsertNamesSqlPart, '{{rowInsertValues}}' => implode($templates['rowInsertValueGlue'], $rowInsertValues))); return $sql; }
/** * Builds a new COrderedIterator object. This object will iterate over the * given $model records found in table. The found records will be filtered * accordingly to the params in given $criteria object. If no criteria is * provided, no filtering will be done. This method clones the criteria for * its inner purposes, so the given criteria can be used for other things * elsewhere. * * This iterator takes over the control of the $criteria->offset and the * $criteria->order values. If such values are provided, they will be * ignored for the iterations. If the $criteria->limit is provided, it will * be used as a one-row fetch limit each time it will be needed until the * end of the set is reached. If this is not provided, this object will try * to find a value that is not too big and not too small to have good * performances without exploding the memory. * * @param CActiveRecord $model * @param CDbCriteria $criteria */ public function __construct(CActiveRecord $model, CDbCriteria $criteria = null) { $this->_model = $model; if ($criteria === null) { $this->_criteria = new CDbCriteria(); } else { $this->_criteria = clone $criteria; } if (empty($this->_criteria->limit) || $this->_criteria->limit <= 0) { $this->_criteria->limit = 500; } // TODO dynamical evaluation $this->_criteria->offset = null; $this->_current_offset = 0; /* @var $schema CDbSchema */ $schema = $this->_model->getDbConnection()->getSchema(); /* @var $md CActiveRecordMetaData */ $md = $this->_model->getMetaData(); $pk = $md->tableSchema->primaryKey; if ($pk === null) { throw new CException(Yii::t('orderediterator', "You cannot use this iterator over {mname} models, since they have no primary keys.", array('{mname}' => get_class($this->_model)))); } if (is_array($pk)) { $orders = array(); foreach ($pk as $upk) { $orders[] = $schema->quoteColumnName($upk) . ' ASC'; } $this->_criteria->order = implode(', ', $orders); } else { $this->_criteria->order = $schema->quoteColumnName($pk) . ' ASC'; } }
/** * Builds a new CTimedOrderedIterator object. This object will iterate over * the given $model records found in table. The found records will be * filtered accordingly to the params in given $criteria object. The found * records will also be filtered by the given $dateValue that given * $dateField is supposed to have. If no criteria is provided, only the date * related field will be filtered. This method clones the criteria for its * inner purposes, so the given criteria can be used for other things * elsewhere. * * This iterator takes over the control of the $criteria->offset and the * $criteria->order values. If such values are provided, they will be * ignored for the iterations. If the $criteria->limit is provided, it will * be used as a one-row fetch limit each time it will be needed until the * end of the set is reached. If this is not provided, this object will try * to find a value that is not too big and not too small to have good * performances without exploding the memory. * * @param CActiveRecord $model * @param string $dateField * @param string|DateTime $dateValue * @param CDbCriteria $criteria */ public function __construct(CActiveRecord $model, $dateField, $dateValue, CDbCriteria $criteria = null) { $this->_model = $model; if ($criteria === null) { $this->_criteria = new CDbCriteria(); } else { $this->_criteria = clone $criteria; } if (empty($this->_criteria->limit) || $this->_criteria->limit <= 0) { $this->_criteria->limit = 500; } // TODO dynamical evaluation if (!is_string($dateField)) { throw new CException(Yii::t('timedorderediterator', "The date field should be a string, {type} given.", array('{type}' => gettype($dateField) === 'object' ? get_class($dateField) : gettype($dateField)))); } $this->_date_field = $dateField; if (is_string($dateValue)) { $this->_date_value = $dateValue; } else { if ($dateValue instanceof \DateTime) { $this->_date_value = $dateValue->format('Y-m-d'); } else { throw new CException(Yii::t('timedorderediterator', "The date value field should be a string or an instance of \\DateTime, {type} given.", array('{type}' => gettype($dateField) === 'object' ? get_class($dateField) : gettype($dateField)))); } } $this->_criteria->offset = null; $this->_current_offset = 0; /* @var $schema CDbSchema */ $schema = $this->_model->getDbConnection()->getSchema(); /* @var $md CActiveRecordMetaData */ $md = $this->_model->getMetaData(); $pk = $md->tableSchema->primaryKey; if ($pk === null) { throw new CException(Yii::t('timedorderediterator', "You cannot use this iterator over {mname} models, since they have no primary keys.", array('{mname}' => get_class($this->_model)))); } if (is_array($pk)) { $orders = array(); foreach ($pk as $upk) { $orders[] = $schema->quoteColumnName($upk) . ' ASC'; } $this->_criteria->order = implode(', ', $orders); } else { $this->_criteria->order = $schema->quoteColumnName($pk) . ' ASC'; } }
/** * Constructor. * @param CActiveRecord $model the model instance */ public function __construct($model) { $this->_model = $model; $tableName = $model->tableName(); if (($table = $model->getDbConnection()->getSchema()->getTable($tableName)) === null) { throw new CDbException(Yii::t('yii', 'The table "{table}" for active record class "{class}" cannot be found in the database.', array('{class}' => get_class($model), '{table}' => $tableName))); } if ($table->primaryKey === null) { $table->primaryKey = $model->primaryKey(); if (is_string($table->primaryKey) && isset($table->columns[$table->primaryKey])) { $table->columns[$table->primaryKey]->isPrimaryKey = true; } elseif (is_array($table->primaryKey)) { foreach ($table->primaryKey as $name) { if (isset($table->columns[$name])) { $table->columns[$name]->isPrimaryKey = true; } } } } $this->tableSchema = $table; $this->columns = $table->columns; foreach ($table->columns as $name => $column) { if (!$column->isPrimaryKey && $column->defaultValue !== null) { $this->attributeDefaults[$name] = $column->defaultValue; } } foreach ($model->relations() as $name => $config) { $this->addRelation($name, $config); } }