Browse Source

...

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
da4b7afe59
  1. 3
      framework/base/Object.php
  2. 12
      framework/db/ar/ActiveMetaData.php
  3. 131
      framework/db/ar/ActiveQuery.php
  4. 33
      framework/db/ar/ActiveRecord.php
  5. 78
      framework/db/ar/ActiveRelation.php
  6. 176
      framework/db/ar/JoinElement.php
  7. 8
      framework/db/dao/Query.php
  8. 13
      framework/db/dao/TableSchema.php
  9. 6
      framework/db/dao/mysql/Driver.php
  10. 10
      framework/db/dao/sqlite/Driver.php
  11. 23
      framework/db/dao/sqlite/QueryBuilder.php
  12. 2
      tests/unit/MysqlTestCase.php
  13. 18
      tests/unit/framework/db/ar/ActiveRecordTest.php

3
framework/base/Object.php

@ -188,8 +188,7 @@ class Object
*/ */
public function hasProperty($name, $checkVar = true) public function hasProperty($name, $checkVar = true)
{ {
return $this->canGetProperty($name, false) || $this->canSetProperty($name, false) return $this->canGetProperty($name, false) || $this->canSetProperty($name, false) || $checkVar && property_exists($this, $name);
|| $checkVar && property_exists($this, $name);
} }
/** /**

12
framework/db/ar/ActiveMetaData.php

@ -3,6 +3,7 @@
namespace yii\db\ar; namespace yii\db\ar;
use yii\db\Exception; use yii\db\Exception;
use yii\db\dao\TableSchema;
/** /**
* ActiveMetaData represents the meta-data for an Active Record class. * ActiveMetaData represents the meta-data for an Active Record class.
@ -28,19 +29,16 @@ class ActiveMetaData
public function __construct($modelClass) public function __construct($modelClass)
{ {
$tableName = $modelClass::tableName(); $tableName = $modelClass::tableName();
$table = $modelClass::getDbConnection()->getDriver()->getTableSchema($tableName); $this->table = $modelClass::getDbConnection()->getDriver()->getTableSchema($tableName);
if ($table === null) { if ($this->table === null) {
throw new Exception("Unable to find table '$tableName' for ActiveRecord class '$modelClass'."); throw new Exception("Unable to find table '$tableName' for ActiveRecord class '$modelClass'.");
} }
if ($table->primaryKey === null) {
$primaryKey = $modelClass::primaryKey(); $primaryKey = $modelClass::primaryKey();
if ($primaryKey !== null) { if ($primaryKey !== null) {
$table->fixPrimaryKey($primaryKey); $this->table->fixPrimaryKey($primaryKey);
} else { } elseif ($this->table->primaryKey === null) {
throw new Exception("The table '$tableName' for ActiveRecord class '$modelClass' does not have a primary key."); throw new Exception("The table '$tableName' for ActiveRecord class '$modelClass' does not have a primary key.");
} }
}
$this->table = $table;
foreach ($modelClass::relations() as $name => $config) { foreach ($modelClass::relations() as $name => $config) {
$this->addRelation($name, $config); $this->addRelation($name, $config);

131
framework/db/ar/ActiveQuery.php

@ -12,6 +12,7 @@ namespace yii\db\ar;
use yii\base\VectorIterator; use yii\base\VectorIterator;
use yii\db\dao\Query; use yii\db\dao\Query;
use yii\db\Exception;
/** /**
* ActiveFinder.php is ... * ActiveFinder.php is ...
@ -44,32 +45,28 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
$this->query = new Query; $this->query = new Query;
} }
public function all($refresh = false) public function all()
{ {
if ($this->records === null || $refresh) { if ($this->records === null) {
$this->records = $this->findRecords(); $this->records = $this->findRecords();
} }
return $this->records; return $this->records;
} }
public function one($refresh = false, $limitOne = true) public function one($limitOne = true)
{ {
if ($this->records === null || $refresh) { if ($this->records === null) {
if ($limitOne) { if ($limitOne) {
$this->limit(1); $this->limit(1);
} }
$this->records = $this->findRecords(); $this->records = $this->findRecords();
} }
if (isset($this->records[0])) { return isset($this->records[0]) ? $this->records[0] : null;
return $this->records[0];
} else {
return null;
}
} }
public function exists() public function exists()
{ {
return $this->select(array('1'))->asArray(true)->one() !== null;
} }
public function asArray($value = true) public function asArray($value = true)
@ -81,6 +78,10 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
public function with() public function with()
{ {
$this->with = func_get_args(); $this->with = func_get_args();
if (isset($this->with[0]) && is_array($this->with[0])) {
// the parameter is given as an array
$this->with = $this->with[0];
}
return $this; return $this;
} }
@ -649,6 +650,11 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
return $this; return $this;
} }
public function getParams()
{
return $this->query->params;
}
/** /**
* Sets the parameters to be bound to the query. * Sets the parameters to be bound to the query.
* @param array list of query parameter values indexed by parameter placeholders. * @param array list of query parameter values indexed by parameter placeholders.
@ -677,21 +683,20 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
return $this; return $this;
} }
public function joinWith()
{
// todo: inner join with one or multiple relations
}
protected function findRecords() protected function findRecords()
{ {
/* if (!empty($this->with)) {
* public $with; // todo: handle findBySql() and limit cases
*/ $this->initRelationalQuery();
}
if ($this->sql === null) { if ($this->sql === null) {
if ($this->query->from === null) { $this->initFrom($this->query);
$modelClass = $this->modelClass;
$tableName = $modelClass::tableName();
if ($this->tableAlias !== null) {
$tableName .= ' ' . $this->tableAlias;
}
$this->query->from = array($tableName);
}
$command = $this->query->createCommand($this->getDbConnection()); $command = $this->query->createCommand($this->getDbConnection());
$this->sql = $command->getSql(); $this->sql = $command->getSql();
} else { } else {
@ -731,14 +736,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
protected function performCountQuery() protected function performCountQuery()
{ {
if ($this->sql === null) { if ($this->sql === null) {
if ($this->query->from === null) { $this->initFrom($this->query);
$modelClass = $this->modelClass;
$tableName = $modelClass::tableName();
if ($this->tableAlias !== null) {
$tableName .= ' ' . $this->tableAlias;
}
$this->query->from = array($tableName);
}
$this->query->select = 'COUNT(*)'; $this->query->select = 'COUNT(*)';
$command = $this->query->createCommand($this->getDbConnection()); $command = $this->query->createCommand($this->getDbConnection());
$this->sql = $command->getSql(); $this->sql = $command->getSql();
@ -748,4 +746,79 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
} }
return $command->queryScalar(); return $command->queryScalar();
} }
protected function initFrom($query)
{
if ($query->from === null) {
$modelClass = $this->modelClass;
$tableName = $modelClass::tableName();
if ($this->tableAlias !== null) {
$tableName .= ' ' . $this->tableAlias;
}
$query->from = array($tableName);
}
}
protected function initRelationalQuery()
{
$joinTree = new JoinElement(null, null);
$joinCount = 0;
$this->buildJoinTree($joinTree, $this->with, $joinCount);
$query = new Query;
foreach ($joinTree->children as $child) {
$child->buildQuery($query);
}
}
/**
* @param JoinElement $parent
* @param array|string $with
* @param integer $joinCount
* @param array $config
* @return null|JoinElement
* @throws \yii\db\Exception
*/
protected function buildJoinTree($parent, $with, &$joinCount, $config = array())
{
if (is_array($with)) {
foreach ($with as $name => $value) {
if (is_string($value)) {
$this->buildJoinTree($parent, $value, $joinCount);
} elseif (is_string($name) && is_array($value)) {
$this->buildJoinTree($parent, $name, $joinCount, $value);
}
}
return null;
}
if (($pos = strrpos($with, '.')) !== false) {
$parent = $this->buildJoinTree($parent, substr($with, 0, $pos), $joinCount);
$with = substr($with, $pos + 1);
}
if (isset($parent->children[$with])) {
$child = $parent->children[$with];
} else {
$modelClass = $parent->relation->modelClass;
$relations = $modelClass::getMetaData()->relations;
if (!isset($relations[$with])) {
throw new Exception("$modelClass has no relation named '$with'.");
}
$relation = clone $relations[$with];
if ($relation->tableAlias === null) {
$relation->tableAlias = 't' . ($joinCount++);
}
$child = new JoinElement($parent, $relation);
}
foreach ($config as $name => $value) {
$child->relation->$name = $value;
}
if (!empty($child->relation->with)) {
$this->buildJoinTree($child, $child->relation->with, $joinCount);
}
return $child;
}
} }

33
framework/db/ar/ActiveRecord.php

@ -68,10 +68,10 @@ abstract class ActiveRecord extends \yii\base\Model
} elseif ($q !== null) { } elseif ($q !== null) {
// query by primary key // query by primary key
$primaryKey = static::getMetaData()->table->primaryKey; $primaryKey = static::getMetaData()->table->primaryKey;
if (is_string($primaryKey)) { if (count($primaryKey) === 1) {
$query->where(array($primaryKey => $q)); $query->where(array($primaryKey[0] => $q));
} else { } else {
throw new Exception('Composite primary keys require multiple column values.'); throw new Exception('Multiple values are required to query by composite keys.');
} }
} }
return $query; return $query;
@ -1112,20 +1112,14 @@ abstract class ActiveRecord extends \yii\base\Model
public function getPrimaryKey() public function getPrimaryKey()
{ {
$table = static::getMetaData()->table; $table = static::getMetaData()->table;
if (is_string($table->primaryKey)) { if (count($table->primaryKey) === 1) {
return $this->{$table->primaryKey}; return $this->{$table->primaryKey[0]};
} } else {
elseif (is_array($table->primaryKey))
{
$values = array(); $values = array();
foreach ($table->primaryKey as $name) foreach ($table->primaryKey as $name) {
{
$values[$name] = $this->$name; $values[$name] = $this->$name;
} }
return $values; return $values;
} else
{
return null;
} }
} }
@ -1138,14 +1132,11 @@ abstract class ActiveRecord extends \yii\base\Model
public function setPrimaryKey($value) public function setPrimaryKey($value)
{ {
$this->_pk = $this->getPrimaryKey(); $this->_pk = $this->getPrimaryKey();
$table = $this->getMetaData()->tableSchema; $table = $this->getMetaData()->table;
if (is_string($table->primaryKey)) { if (count($table->primaryKey) === 1) {
$this->{$table->primaryKey} = $value; $this->{$table->primaryKey[0]} = $value;
} } else {
elseif (is_array($table->primaryKey)) foreach ($table->primaryKey as $name) {
{
foreach ($table->primaryKey as $name)
{
$this->$name = $value[$name]; $this->$name = $value[$name];
} }
} }

78
framework/db/ar/ActiveRelation.php

@ -2,42 +2,66 @@
namespace yii\db\ar; namespace yii\db\ar;
class ActiveRelation extends \yii\db\dao\BaseQuery class ActiveRelation extends \yii\base\Object
{ {
public $name; public $name;
public $modelClass; public $modelClass;
public $hasMany; public $hasMany;
public $joinType; public $joinType;
public $alias; public $tableAlias;
public $on; public $on;
public $via; public $via;
public $index;
public $with; public $with;
public $scopes; public $scopes;
public function mergeWith($relation) /**
{ * @var string|array the columns being selected. This refers to the SELECT clause in a SQL
parent::mergeWith($relation); * statement. It can be either a string (e.g. `'id, name'`) or an array (e.g. `array('id', 'name')`).
if ($relation->joinType !== null) { * If not set, if means all columns.
$this->joinType = $relation->joinType; * @see select()
} */
if ($relation->alias !== null) { public $select;
$this->alias = $relation->alias; /**
} * @var string|array query condition. This refers to the WHERE clause in a SQL statement.
if ($relation->on !== null) { * For example, `age > 31 AND team = 1`.
if (!empty($this->on)) { * @see where()
$this->on = "({$this->on}) AND ({$relation->on})"; */
} else { public $where;
$this->on = $relation->on; /**
} * @var integer maximum number of records to be returned. If not set or less than 0, it means no limit.
} */
if ($relation->via !== null) { public $limit;
$this->via = $relation->via; /**
} * @var integer zero-based offset from where the records are to be returned. If not set or
if ($relation->index !== null) { * less than 0, it means starting from the beginning.
$this->index = $relation->index; */
} public $offset;
// todo: with, scopes /**
} * @var string|array how to sort the query results. This refers to the ORDER BY clause in a SQL statement.
* It can be either a string (e.g. `'id ASC, name DESC'`) or an array (e.g. `array('id ASC', 'name DESC')`).
*/
public $orderBy;
/**
* @var string|array how to group the query results. This refers to the GROUP BY clause in a SQL statement.
* It can be either a string (e.g. `'company, department'`) or an array (e.g. `array('company', 'department')`).
*/
public $groupBy;
/**
* @var string|array how to join with other tables. This refers to the JOIN clause in a SQL statement.
* It can either a string (e.g. `'LEFT JOIN tbl_user ON tbl_user.id=author_id'`) or an array (e.g.
* `array('LEFT JOIN tbl_user ON tbl_user.id=author_id', 'LEFT JOIN tbl_team ON tbl_team.id=team_id')`).
* @see join()
*/
public $join;
/**
* @var string|array the condition to be applied in the GROUP BY clause.
* It can be either a string or an array. Please refer to [[where()]] on how to specify the condition.
*/
public $having;
/**
* @var array list of query parameter values indexed by parameter placeholders.
* For example, `array(':name'=>'Dan', ':age'=>31)`.
*/
public $params;
} }

176
framework/db/ar/JoinElement.php

@ -0,0 +1,176 @@
<?php
/**
* ActiveQuery class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\ar;
use yii\base\VectorIterator;
use yii\db\dao\Query;
use yii\db\Exception;
class JoinElement extends \yii\base\Object
{
/**
* @var ActiveRelation
*/
public $relation;
/**
* @var JoinElement
*/
public $parent;
/**
* @var JoinElement[]
*/
public $children = array();
public $columnAliases = array(); // alias => original name
public $pkAlias = array(); // original name => alias
public $records;
public $relatedRecords;
public function __construct($parent, $relation)
{
if ($parent !== null) {
$this->parent = $parent;
$this->relation = $relation;
$parent->children[$relation->name] = $this;
}
}
public function populateData($row)
{
$pk = array();
foreach ($this->pkAlias as $alias) {
if (isset($row[$alias])) {
$pk[] = $row[$alias];
} else {
return null;
}
}
$pk = count($pk) === 1 ? $pk[0] : serialize($pk);
// create active record
if (isset($this->records[$pk])) {
$record = $this->records[$pk];
} else {
$attributes = array();
foreach ($row as $alias => $value) {
if (isset($this->columnAliases[$alias])) {
$attributes[$this->columnAliases[$alias]] = $value;
}
}
$modelClass = $this->relation->modelClass;
$record = $modelClass::populateRecord($attributes);
foreach ($this->children as $child) {
if ($child->relation->select !== false) {
$record->initRelation($child->relation);
}
}
$this->records[$pk] = $record;
}
// populate child records
foreach ($this->children as $child) {
if ($child->relation->select === false) {
continue;
}
$childRecord = $child->populateData($row);
if ($childRecord === null) {
continue;
}
if ($child->relation->hasMany) {
$fpk = serialize($childRecord->getPrimaryKey());
if (isset($this->relatedRecords[$pk][$child->relation->name][$fpk])) {
continue;
}
$this->relatedRecords[$pk][$child->relation->name][$fpk] = true;
}
$record->addRelatedRecord($child->relation, $childRecord);
}
return $record;
}
public function buildQuery($query)
{
$tokens = array(
'@.' => $this->relation->tableAlias,
'?.' => $this->parent->relation->tableAlias,
);
foreach ($this->buildSelect() as $column) {
$query->select[] = strtr($column, $tokens);
}
if ($this->relation->where !== null) {
$query->where[] = strtr($this->relation->where, $tokens);
}
if ($this->relation->having !== null) {
$query->having[] = strtr($this->relation->having, $tokens);
}
/*
* joinType;
on;
via;
orderby
groupby
join
params
*/
foreach ($this->children as $child) {
$child->buildQuery($query);
}
}
public function buildSelect()
{
$modelClass = $this->relation->modelClass;
$tableSchema = $modelClass::getMetaData()->table;
$select = $this->relation->select;
$columns = array();
$columnCount = 0;
if (empty($select) || $select === '*') {
foreach ($tableSchema->columns as $column) {
$alias = $this->tableAlias . '_' . ($columnCount++);
$columns[] = "{$column->name} AS $alias";
$this->columnAliases[$alias] = $column->name;
if ($column->isPrimaryKey) {
$this->pkAlias[$column->name] = $alias;
}
}
} else {
if (is_string($select)) {
$select = explode(',', $select);
}
foreach ($tableSchema->primaryKey as $column) {
$alias = $this->tableAlias . '_' . ($columnCount++);
$columns[] = "$column AS $alias";
$this->pkAlias[$column] = $alias;
}
foreach ($select as $column) {
$column = trim($column);
if (preg_match('/^(.*?)\s+AS\s+(\w+)$/im', $column, $matches)) {
// if the column is already aliased
$this->columnAliases[$matches[2]] = $matches[2];
$columns[] = $column;
} elseif (!isset($this->pkAlias[$column])) {
$alias = $this->tableAlias . '_' . ($columnCount++);
$columns[] = "$column AS $alias";
$this->columnAliases[$alias] = $column;
}
}
}
return $columns;
}
}

8
framework/db/dao/Query.php

@ -377,7 +377,7 @@ class Query extends \yii\base\Object
/** /**
* Sets the SELECT part of the query. * Sets the SELECT part of the query.
* @param mixed $columns the columns to be selected. Defaults to '*', meaning all columns. * @param string|array $columns the columns to be selected. Defaults to '*', meaning all columns.
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. array('id', 'name')). * Columns can be specified in either a string (e.g. "id, name") or an array (e.g. array('id', 'name')).
* Columns can contain table prefixes (e.g. "tbl_user.id") and/or column aliases (e.g. "tbl_user.id AS user_id"). * Columns can contain table prefixes (e.g. "tbl_user.id") and/or column aliases (e.g. "tbl_user.id AS user_id").
* The method will automatically quote the column names unless a column contains some parenthesis * The method will automatically quote the column names unless a column contains some parenthesis
@ -882,7 +882,7 @@ class Query extends \yii\base\Object
* The merging is done according to the following rules: * The merging is done according to the following rules:
* *
* - [[select]]: the union of both queries' [[select]] property values. * - [[select]]: the union of both queries' [[select]] property values.
* - [[selectOption]], [[distinct]], [[limit]], [[offset]]: the new query * - [[selectOption]], [[distinct]], [[from]], [[limit]], [[offset]]: the new query
* takes precedence over this query. * takes precedence over this query.
* - [[where]], [[having]]: the new query's corresponding property value * - [[where]], [[having]]: the new query's corresponding property value
* will be 'AND' together with the existing one. * will be 'AND' together with the existing one.
@ -913,6 +913,10 @@ class Query extends \yii\base\Object
$this->distinct = $query->distinct; $this->distinct = $query->distinct;
} }
if ($query->from !== null) {
$this->from = $query->from;
}
if ($query->limit !== null) { if ($query->limit !== null) {
$this->limit = $query->limit; $this->limit = $query->limit;
} }

13
framework/db/dao/TableSchema.php

@ -41,9 +41,9 @@ class TableSchema extends \yii\base\Object
*/ */
public $quotedName; public $quotedName;
/** /**
* @var string|array primary key name of this table. If composite key, an array of key names is returned. * @var array primary keys of this table.
*/ */
public $primaryKey; public $primaryKey = array();
/** /**
* @var string sequence name for the primary key. Null if no sequence. * @var string sequence name for the primary key. Null if no sequence.
*/ */
@ -85,11 +85,20 @@ class TableSchema extends \yii\base\Object
return array_keys($this->columns); return array_keys($this->columns);
} }
/**
* Manually specifies the primary key for this table.
* @param string|array $keys the primary key (can be composite)
* @throws \yii\db\Exception if the specified key cannot be found in the table.
*/
public function fixPrimaryKey($keys) public function fixPrimaryKey($keys)
{ {
if (!is_array($keys)) { if (!is_array($keys)) {
$keys = array($keys); $keys = array($keys);
} }
$this->primaryKey = $keys;
foreach ($this->columns as $column) {
$column->isPrimaryKey = false;
}
foreach ($keys as $key) { foreach ($keys as $key) {
if (isset($this->columns[$key])) { if (isset($this->columns[$key])) {
$this->columns[$key]->isPrimaryKey = true; $this->columns[$key]->isPrimaryKey = true;

6
framework/db/dao/mysql/Driver.php

@ -212,13 +212,7 @@ class Driver extends \yii\db\dao\Driver
$column = $this->createColumn($column); $column = $this->createColumn($column);
$table->columns[$column->name] = $column; $table->columns[$column->name] = $column;
if ($column->isPrimaryKey) { if ($column->isPrimaryKey) {
if ($table->primaryKey === null) {
$table->primaryKey = $column->name;
} elseif (is_string($table->primaryKey)) {
$table->primaryKey = array($table->primaryKey, $column->name);
} else {
$table->primaryKey[] = $column->name; $table->primaryKey[] = $column->name;
}
if ($column->autoIncrement) { if ($column->autoIncrement) {
$table->sequenceName = ''; $table->sequenceName = '';
} }

10
framework/db/dao/sqlite/Driver.php

@ -108,18 +108,12 @@ class Driver extends \yii\db\dao\Driver
$column = $this->createColumn($column); $column = $this->createColumn($column);
$table->columns[$column->name] = $column; $table->columns[$column->name] = $column;
if ($column->isPrimaryKey) { if ($column->isPrimaryKey) {
if ($table->primaryKey === null) {
$table->primaryKey = $column->name;
} elseif (is_string($table->primaryKey)) {
$table->primaryKey = array($table->primaryKey, $column->name);
} else {
$table->primaryKey[] = $column->name; $table->primaryKey[] = $column->name;
} }
} }
} if (count($table->primaryKey) === 1 && !strncasecmp($table->columns[$table->primaryKey[0]]->dbType, 'int', 3)) {
if (is_string($table->primaryKey) && !strncasecmp($table->columns[$table->primaryKey]->dbType, 'int', 3)) {
$table->sequenceName = ''; $table->sequenceName = '';
$table->columns[$table->primaryKey]->autoIncrement = true; $table->columns[$table->primaryKey[0]]->autoIncrement = true;
} }
return true; return true;

23
framework/db/dao/sqlite/QueryBuilder.php

@ -23,23 +23,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
/** /**
* @var array mapping from abstract column types (keys) to physical column types (values). * @var array mapping from abstract column types (keys) to physical column types (values).
*/ */
public $typeMap = array( public $typeMap = array(Driver::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', Driver::TYPE_STRING => 'varchar(255)', Driver::TYPE_TEXT => 'text', Driver::TYPE_SMALLINT => 'smallint', Driver::TYPE_INTEGER => 'integer', Driver::TYPE_BIGINT => 'bigint', Driver::TYPE_FLOAT => 'float', Driver::TYPE_DECIMAL => 'decimal', Driver::TYPE_DATETIME => 'datetime', Driver::TYPE_TIMESTAMP => 'timestamp', Driver::TYPE_TIME => 'time', Driver::TYPE_DATE => 'date', Driver::TYPE_BINARY => 'blob', Driver::TYPE_BOOLEAN => 'tinyint(1)', Driver::TYPE_MONEY => 'decimal(19,4)',);
Driver::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
Driver::TYPE_STRING => 'varchar(255)',
Driver::TYPE_TEXT => 'text',
Driver::TYPE_SMALLINT => 'smallint',
Driver::TYPE_INTEGER => 'integer',
Driver::TYPE_BIGINT => 'bigint',
Driver::TYPE_FLOAT => 'float',
Driver::TYPE_DECIMAL => 'decimal',
Driver::TYPE_DATETIME => 'datetime',
Driver::TYPE_TIMESTAMP => 'timestamp',
Driver::TYPE_TIME => 'time',
Driver::TYPE_DATE => 'date',
Driver::TYPE_BINARY => 'blob',
Driver::TYPE_BOOLEAN => 'tinyint(1)',
Driver::TYPE_MONEY => 'decimal(19,4)',
);
/** /**
* Resets the sequence value of a table's primary key. * Resets the sequence value of a table's primary key.
@ -53,9 +37,8 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
{ {
if ($table->sequenceName !== null) { if ($table->sequenceName !== null) {
if ($value === null) { if ($value === null) {
$value = $this->connection->createCommand("SELECT MAX(`{$table->primaryKey}`) FROM {$table->quotedName}")->queryScalar(); $value = $this->connection->createCommand("SELECT MAX(`{$table->primaryKey[0]}`) FROM {$table->quotedName}")->queryScalar();
} } else {
else {
$value = (int)$value - 1; $value = (int)$value - 1;
} }
try { try {

2
tests/unit/MysqlTestCase.php

@ -6,7 +6,7 @@ class MysqlTestCase extends TestCase
{ {
function __construct() function __construct()
{ {
if(!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) { if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
$this->markTestSkipped('pdo and pdo_mysql extensions are required.'); $this->markTestSkipped('pdo and pdo_mysql extensions are required.');
} }
} }

18
tests/unit/framework/db/ar/ActiveRecordTest.php

@ -62,13 +62,13 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertEquals('user2', $customer->name); $this->assertEquals('user2', $customer->name);
// find by attributes // find by attributes
$customer = Customer::find(array('name'=>'user2'))->one(); $customer = Customer::find(array('name' => 'user2'))->one();
$this->assertTrue($customer instanceof Customer); $this->assertTrue($customer instanceof Customer);
$this->assertEquals(2, $customer->id); $this->assertEquals(2, $customer->id);
// find by Query // find by Query
$query = new Query; $query = new Query;
$query->where('id=:id', array(':id'=>2)); $query->where('id=:id', array(':id' => 2));
$customer = Customer::find($query)->one(); $customer = Customer::find($query)->one();
$this->assertTrue($customer instanceof Customer); $this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name); $this->assertEquals('user2', $customer->name);
@ -89,7 +89,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertEquals(3, count($customers)); $this->assertEquals(3, count($customers));
// find with parameter binding // find with parameter binding
$customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id'=>2))->one(); $customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one();
$this->assertTrue($customer instanceof Customer); $this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name); $this->assertEquals('user2', $customer->name);
@ -116,7 +116,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertEquals(3, $customer->id); $this->assertEquals(3, $customer->id);
$this->assertEquals(null, $customer->name); $this->assertEquals(null, $customer->name);
} }
/* /*
public function testGetSql() public function testGetSql()
{ {
// sql for all // sql for all
@ -140,10 +140,10 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
public function testMisc() public function testMisc()
{ {
// Customer::exists() // Customer::exists()
// Customer::updateAll() // Customer::updateAll()
// Customer::updateCounters() // Customer::updateCounters()
// Customer::deleteAll() // Customer::deleteAll()
} }
public function testInsert() public function testInsert()
@ -171,5 +171,5 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
{ {
} }
*/ */
} }
Loading…
Cancel
Save