public function prepare($builder) { // override private `buildJoinWith()` if (!empty($this->joinWith)) { $this->buildJoinWith(); $this->joinWith = null; // clean it up to avoid issue https://github.com/yiisoft/yii2/issues/2687 } if (empty($this->from)) { $this->from = [$this->finder->tableName()]; } return parent::prepare($builder); }
/** * Create temporary auth token in database and cache. Delete old authentication * records for this client with current ip address. * @return type */ private function createApiDeviceRecord() { if ($this->model == null) { return; } $pkAttribute = $this->model->tableSchema->primaryKey; // generate data for new temp auth token $newRecord = array('ip_address' => $this->module->getIpAddress(), 'token' => self::generateRandomToken(), 'connected_type' => $this->model->tableName(), 'connected_id' => $this->model->{$pkAttribute}, 'update_time' => date('Y-m-d H:i:s')); // delete auth record for the current entity if (Yii::app()->cache) { $oldTokensCommand = Yii::app()->db->createCommand("SELECT * FROM {$this->module->authTableName} WHERE connected_type=:type AND connected_id=:id AND ip_address=:ip"); $oldTokensCommand->bindValues(array(':type' => $newRecord['connected_type'], ':id' => $newRecord['connected_id'], ':ip' => $newRecord['ip_address'])); $tokens = $oldTokensCommand->queryAll(); foreach ($tokens as $token) { Yii::app()->cache->delete("api-auth-token-" . $token['token']); } } // delete auth records from database $deleteCommand = Yii::app()->db->createCommand("DELETE FROM {$this->module->authTableName} WHERE connected_type=:type AND connected_id=:id AND ip_address=:ip"); $deleteCommand->bindValues(array(':type' => $newRecord['connected_type'], ':id' => $newRecord['connected_id'], ':ip' => $newRecord['ip_address'])); $deleteCommand->execute(); // insert new one $insertCommand = Yii::app()->db->createCommand("INSERT INTO {$this->module->authTableName}(token, ip_address, update_time, connected_type, connected_id) VALUES(:token, :ip, :date, :type, :id)"); $insertCommand->bindValues(array(':token' => $newRecord['token'], ':date' => $newRecord['update_time'], ':type' => $newRecord['connected_type'], ':id' => $newRecord['connected_id'], ':ip' => $newRecord['ip_address'])); if ($insertCommand->execute()) { // update this model with new token $this->setIsAuthenticated($newRecord); if (isset(Yii::app()->cache)) { Yii::app()->cache->set("api-auth-token-" . $this->token, $newRecord, $this->module->authCacheDuration); } } }
/** * Returns table name * @see CActiveRecord::tableName() * @return string */ public function tableName() { if (!$this->_tableName) { $this->_tableName = parent::tableName(); } return $this->_tableName; }
/** * {@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; }
/** * Function for child classes to implement to return the table name associated with it */ protected function _tableName() { // call the original method for our table name stuff return parent::tableName(); }
/** * Compares current active record with another one. * The comparison is made by comparing table name and the primary key values of the two active records. * @param CActiveRecord $record record to compare to * @return boolean whether the two active records refer to the same row in the database table. */ public function equals($record) { return $this->tableName() === $record->tableName() && $this->getPrimaryKey() === $record->getPrimaryKey(); }
/** * 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); } }
/** * Renvoie un tableau contenant toutes les valeurs de la colonne $column * @param CActiveRecord $model * @param string $column * @return array */ public static function getColumn(CActiveRecord $model, $column) { $table = $model->tableName(); $command = Yii::app()->db->createCommand("SELECT {$column} FROM {$table}"); return $command->queryColumn(); }
public function tableName() { return isset($this->tableName) ? $this->tableName : parent::tableName(); }
public function tableName() { return $this->wrappedUser->tableName(); }