/** * Initializes the command object. * This method is invoked after a command object is created and initialized with configurations. * You may override this method to further customize the command before it executes. * @since 1.1.6 */ public function init() { if (null === Yii::app()->db) { throw new CException('An active "db" connection is required to run this command.'); } $this->dbConnection = Yii::app()->db; $this->dbSchema = $this->dbConnection->getSchema(); $this->tableNames = array_diff($this->dbSchema->tableNames, (array) $this->ignoreTableNames, (array) $this->systemTableNames); }
public function testIssue1407_5() { // :parameter3 is not used $tableSchema = $this->db->getSchema()->getTable('users'); $builder = $this->db->getSchema()->getCommandBuilder(); $criteria = new CDbCriteria(); $criteria->select = array('t.*', ':parameter1 AS test'); $criteria->params[':parameter1'] = 'testingValue'; $criteria->order = 'IF (t.username=:parameter2,t.username,t.email) DESC'; $criteria->params[':parameter2'] = 'user2'; $criteria->params[':parameter3'] = 'parameter3Value'; $criteria->addCondition('t.email LIKE :parameter4'); $criteria->params[':parameter4'] = 'email%'; $criteria->addInCondition('t.id', array(1, 2, 3)); $this->setExpectedException('CDbException'); $builder->createCountCommand($tableSchema, $criteria)->queryScalar(); }
public function testMultipleInsert() { $builder = $this->db->getSchema()->getCommandBuilder(); $tableName = 'types'; $data = array(array('int_col' => 1, 'char_col' => 'char_col_1', 'char_col2' => 'char_col_2_1', 'float_col' => 1.1, 'bool_col' => true), array('int_col' => 2, 'char_col' => 'char_col_2', 'float_col' => 2.2, 'bool_col' => false)); $command = $builder->createMultipleInsertCommand($tableName, $data); $command->execute(); $rows = $builder->dbConnection->createCommand('SELECT * FROM ' . $builder->dbConnection->quoteTableName($tableName))->queryAll(); $this->assertEquals(count($data), count($rows), 'Records count miss matches!'); foreach ($rows as $rowIndex => $row) { foreach ($row as $columnName => $value) { $columnIndex = array_search($columnName, $data[$rowIndex], true); if ($columnIndex == false) { continue; } $expectedValue = $data[$rowIndex][$columnIndex]; $this->assertTrue($expectedValue == $value, "Value for column '{$columnName}' incorrect!"); } } }