Commit c63c7838 by Qiang Xue

...

parent 730dc3db
...@@ -64,7 +64,7 @@ class YiiBase ...@@ -64,7 +64,7 @@ class YiiBase
/** /**
* @var yii\base\Application the application instance * @var yii\base\Application the application instance
*/ */
public static $app; public static $application;
/** /**
* @var array registered path aliases * @var array registered path aliases
* @see getAlias * @see getAlias
......
...@@ -116,7 +116,7 @@ abstract class Application extends Module ...@@ -116,7 +116,7 @@ abstract class Application extends Module
*/ */
public function __construct($config = null) public function __construct($config = null)
{ {
\Yii::$app = $this; \Yii::$application = $this;
// set basePath at early as possible to avoid trouble // set basePath at early as possible to avoid trouble
if (is_string($config)) { if (is_string($config)) {
......
...@@ -30,7 +30,7 @@ abstract class ApplicationComponent extends Component implements Initable ...@@ -30,7 +30,7 @@ abstract class ApplicationComponent extends Component implements Initable
/** /**
* Initializes the application component. * Initializes the application component.
* This method is invoked after the component is created and its property values are * This method is invoked after the component is created and its property values are
* initialized. The default implementation will call [[Component::attachBehaviors]] * initialized. The default implementation will call [[Component::attachBehaviors()]]
* to attach behaviors declared in [[behaviors]]. * to attach behaviors declared in [[behaviors]].
* If you override this method, make sure to call the parent implementation. * If you override this method, make sure to call the parent implementation.
*/ */
......
...@@ -294,7 +294,7 @@ class Object ...@@ -294,7 +294,7 @@ class Object
* $model->init(); * $model->init();
* ~~~ * ~~~
* *
* @return object the created object * @return Object the created object
* @throws Exception if the configuration is invalid. * @throws Exception if the configuration is invalid.
*/ */
public static function newInstance() public static function newInstance()
......
...@@ -120,7 +120,7 @@ abstract class CDbMigration extends yii\base\Component ...@@ -120,7 +120,7 @@ abstract class CDbMigration extends yii\base\Component
public function getDbConnection() public function getDbConnection()
{ {
if ($this->_db === null) { if ($this->_db === null) {
$this->_db = Yii::app()->getComponent('db'); $this->_db = \Yii::$application->getComponent('db');
if (!$this->_db instanceof CDbConnection) if (!$this->_db instanceof CDbConnection)
throw new CException(Yii::t('yii', 'The "db" application component must be configured to be a CDbConnection object.')); throw new CException(Yii::t('yii', 'The "db" application component must be configured to be a CDbConnection object.'));
} }
......
...@@ -611,7 +611,7 @@ abstract class ActiveRecord extends \yii\base\Model ...@@ -611,7 +611,7 @@ abstract class ActiveRecord extends \yii\base\Model
return self::$db; return self::$db;
else else
{ {
self::$db = Yii::app()->getDb(); self::$db = \Yii::$application->getDb();
if (self::$db instanceof CDbConnection) if (self::$db instanceof CDbConnection)
return self::$db; return self::$db;
else else
......
...@@ -23,7 +23,7 @@ class ColumnSchema extends \yii\base\Component ...@@ -23,7 +23,7 @@ class ColumnSchema extends \yii\base\Component
*/ */
public $name; public $name;
/** /**
* @var string raw name of this column. This is the quoted name that can be used in SQL queries. * @var string raw name of this column. This is the quoted name that can be directly used in SQL queries.
*/ */
public $quotedName; public $quotedName;
/** /**
...@@ -32,7 +32,8 @@ class ColumnSchema extends \yii\base\Component ...@@ -32,7 +32,8 @@ class ColumnSchema extends \yii\base\Component
public $allowNull; public $allowNull;
/** /**
* @var string logical type of this column. Possible logic types include: * @var string logical type of this column. Possible logic types include:
* string, text, boolean, smallint, integer, bigint, float, decimal, datetime, timestamp, time, date, binary, money * string, text, boolean, smallint, integer, bigint, float, decimal, datetime,
* timestamp, time, date, binary, and money.
*/ */
public $type; public $type;
/** /**
...@@ -41,7 +42,7 @@ class ColumnSchema extends \yii\base\Component ...@@ -41,7 +42,7 @@ class ColumnSchema extends \yii\base\Component
*/ */
public $phpType; public $phpType;
/** /**
* @var string the DB type of this column. * @var string the DB type of this column. Possible DB types vary according to the DBMS.
*/ */
public $dbType; public $dbType;
/** /**
......
...@@ -40,6 +40,10 @@ class QueryBuilder extends \yii\base\Object ...@@ -40,6 +40,10 @@ class QueryBuilder extends \yii\base\Object
$this->schema = $schema; $this->schema = $schema;
} }
/**
* @param Query $query
* @return string
*/
public function build($query) public function build($query)
{ {
$clauses = array( $clauses = array(
...@@ -126,7 +130,6 @@ class QueryBuilder extends \yii\base\Object ...@@ -126,7 +130,6 @@ class QueryBuilder extends \yii\base\Object
* @param string $table the table where the data will be deleted from. * @param string $table the table where the data will be deleted from.
* @param mixed $conditions the conditions that will be put in the WHERE part. Please * @param mixed $conditions the conditions that will be put in the WHERE part. Please
* refer to {@link where} on how to specify conditions. * refer to {@link where} on how to specify conditions.
* @param array $params the parameters to be bound to the query.
* @return integer number of rows affected by the execution. * @return integer number of rows affected by the execution.
*/ */
public function delete($table, $conditions = '') public function delete($table, $conditions = '')
...@@ -440,6 +443,10 @@ class QueryBuilder extends \yii\base\Object ...@@ -440,6 +443,10 @@ class QueryBuilder extends \yii\base\Object
protected function buildFrom($query) protected function buildFrom($query)
{ {
if (empty($query->from)) {
return '';
}
$tables = $query->from; $tables = $query->from;
if (is_string($tables) && strpos($tables, '(') !== false) { if (is_string($tables) && strpos($tables, '(') !== false) {
return 'FROM ' . $tables; return 'FROM ' . $tables;
...@@ -609,7 +616,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -609,7 +616,7 @@ class QueryBuilder extends \yii\base\Object
} }
if (!isset($conditions[1], $conditions[2])) { if (!isset($conditions[1], $conditions[2])) {
return ''; throw new Exception("Operator $operator requires at least two operands.");
} }
$column = $conditions[1]; $column = $conditions[1];
...@@ -617,6 +624,15 @@ class QueryBuilder extends \yii\base\Object ...@@ -617,6 +624,15 @@ class QueryBuilder extends \yii\base\Object
$column = $this->connection->quoteColumnName($column); $column = $this->connection->quoteColumnName($column);
} }
if ($operator === 'BETWEEN' || $operator === 'NOT BETWEEN') {
if (!isset($conditions[3])) {
throw new Exception("Operator $operator requires three operands.");
}
$value1 = is_string($conditions[2]) ? $this->connection->quoteValue($conditions[2]) : (string)$conditions[2];
$value2 = is_string($conditions[3]) ? $this->connection->quoteValue($conditions[3]) : (string)$conditions[3];
return "$column $operator $value1 AND $value2";
}
$values = $conditions[2]; $values = $conditions[2];
if (!is_array($values)) { if (!is_array($values)) {
$values = array($values); $values = array($values);
......
...@@ -86,7 +86,7 @@ abstract class Schema extends \yii\base\Object ...@@ -86,7 +86,7 @@ abstract class Schema extends \yii\base\Object
$db->queryCachingDuration = -1; $db->queryCachingDuration = -1;
} }
if (!in_array($name, $db->schemaCachingExclude) && $db->schemaCachingDuration >= 0 && ($cache = \Yii::app()->getComponent($db->schemaCacheID)) !== null) { if (!in_array($name, $db->schemaCachingExclude) && $db->schemaCachingDuration >= 0 && ($cache = \Yii::$application->getComponent($db->schemaCacheID)) !== null) {
$key = __CLASS__ . "/{$db->dsn}/{$db->username}/{$name}"; $key = __CLASS__ . "/{$db->dsn}/{$db->username}/{$name}";
if (($table = $cache->get($key)) === false) { if (($table = $cache->get($key)) === false) {
$table = $this->loadTableSchema($realName); $table = $this->loadTableSchema($realName);
...@@ -156,7 +156,7 @@ abstract class Schema extends \yii\base\Object ...@@ -156,7 +156,7 @@ abstract class Schema extends \yii\base\Object
public function refresh() public function refresh()
{ {
$db = $this->connection; $db = $this->connection;
if ($db->schemaCachingDuration >= 0 && ($cache = \Yii::app()->getComponent($db->schemaCacheID)) !== null) { if ($db->schemaCachingDuration >= 0 && ($cache = \Yii::$application->getComponent($db->schemaCacheID)) !== null) {
foreach ($this->_tables as $name => $table) { foreach ($this->_tables as $name => $table) {
$key = __CLASS__ . ":{$db->dsn}/{$db->username}/{$name}"; $key = __CLASS__ . ":{$db->dsn}/{$db->username}/{$name}";
$cache->delete($key); $cache->delete($key);
......
...@@ -71,7 +71,7 @@ class DbTarget extends Target ...@@ -71,7 +71,7 @@ class DbTarget extends Target
if ($this->_db !== null) { if ($this->_db !== null) {
return $this->_db; return $this->_db;
} }
$this->_db = \Yii::app()->getComponent($this->connectionID); $this->_db = \Yii::$application->getComponent($this->connectionID);
if (!$this->_db instanceof \yii\db\dao\Connection) { if (!$this->_db instanceof \yii\db\dao\Connection) {
throw new \yii\base\Exception('DbTarget.connectionID must refer to a valid application component ID'); throw new \yii\base\Exception('DbTarget.connectionID must refer to a valid application component ID');
} }
......
...@@ -52,7 +52,7 @@ class FileTarget extends Target ...@@ -52,7 +52,7 @@ class FileTarget extends Target
{ {
parent::init(); parent::init();
if ($this->logPath === null) { if ($this->logPath === null) {
$this->logPath = \Yii::app()->getRuntimePath(); $this->logPath = \Yii::$application->getRuntimePath();
} }
if (!is_dir($this->logPath) || !is_writable($this->logPath)) { if (!is_dir($this->logPath) || !is_writable($this->logPath)) {
throw new \yii\base\Exception("Directory '{$this->logPath}' does not exist or is not writable."); throw new \yii\base\Exception("Directory '{$this->logPath}' does not exist or is not writable.");
......
...@@ -72,7 +72,7 @@ class CProfileLogRoute extends CWebLogRoute ...@@ -72,7 +72,7 @@ class CProfileLogRoute extends CWebLogRoute
*/ */
public function processLogs($logs) public function processLogs($logs)
{ {
$app = Yii::app(); $app = \Yii::$application;
if (!($app instanceof CWebApplication) || $app->getRequest()->getIsAjaxRequest()) if (!($app instanceof CWebApplication) || $app->getRequest()->getIsAjaxRequest())
return; return;
......
...@@ -49,7 +49,7 @@ namespace yii\logging; ...@@ -49,7 +49,7 @@ namespace yii\logging;
* as follows: * as follows:
* *
* ~~~ * ~~~
* \Yii::app()->log->targets['file']->enabled = false; * \Yii::$application->log->targets['file']->enabled = false;
* ~~~ * ~~~
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
...@@ -77,7 +77,7 @@ class Router extends \yii\base\ApplicationComponent ...@@ -77,7 +77,7 @@ class Router extends \yii\base\ApplicationComponent
{ {
parent::init(); parent::init();
\Yii::getLogger()->attachEventHandler('onFlush', array($this, 'processMessages')); \Yii::getLogger()->attachEventHandler('onFlush', array($this, 'processMessages'));
if (($app = \Yii::app()) !== null) { if (($app = \Yii::$application) !== null) {
$app->attachEventHandler('onEndRequest', array($this, 'processMessages')); $app->attachEventHandler('onEndRequest', array($this, 'processMessages'));
} }
} }
......
...@@ -128,7 +128,7 @@ abstract class Target extends \yii\base\Component implements \yii\base\Initable ...@@ -128,7 +128,7 @@ abstract class Target extends \yii\base\Component implements \yii\base\Initable
if ($this->prefixSession && ($id = session_id()) !== '') { if ($this->prefixSession && ($id = session_id()) !== '') {
$prefix[] = "[$id]"; $prefix[] = "[$id]";
} }
if ($this->prefixUser && ($user = \Yii::app()->getComponent('user', false)) !== null) { if ($this->prefixUser && ($user = \Yii::$application->getComponent('user', false)) !== null) {
$prefix[] = '[' . $user->getName() . ']'; $prefix[] = '[' . $user->getName() . ']';
$prefix[] = '[' . $user->getId() . ']'; $prefix[] = '[' . $user->getId() . ']';
} }
...@@ -151,7 +151,7 @@ abstract class Target extends \yii\base\Component implements \yii\base\Initable ...@@ -151,7 +151,7 @@ abstract class Target extends \yii\base\Component implements \yii\base\Initable
protected function getContextMessage() protected function getContextMessage()
{ {
$context = array(); $context = array();
if ($this->logUser && ($user = \Yii::app()->getComponent('user', false)) !== null) { if ($this->logUser && ($user = \Yii::$application->getComponent('user', false)) !== null) {
$context[] = 'User: ' . $user->getName() . ' (ID: ' . $user->getId() . ')'; $context[] = 'User: ' . $user->getName() . ' (ID: ' . $user->getId() . ')';
} }
......
...@@ -47,7 +47,7 @@ class CWebLogRoute extends CLogRoute ...@@ -47,7 +47,7 @@ class CWebLogRoute extends CLogRoute
*/ */
protected function render($view, $data) protected function render($view, $data)
{ {
$app = Yii::app(); $app = \Yii::$application;
$isAjax = $app->getRequest()->getIsAjaxRequest(); $isAjax = $app->getRequest()->getIsAjaxRequest();
if ($this->showInFireBug) if ($this->showInFireBug)
......
...@@ -61,13 +61,13 @@ class CaptchaValidator extends Validator ...@@ -61,13 +61,13 @@ class CaptchaValidator extends Validator
public function getCaptchaAction() public function getCaptchaAction()
{ {
if (strpos($this->captchaAction, '/') !== false) { // contains controller or module if (strpos($this->captchaAction, '/') !== false) { // contains controller or module
$ca = Yii::app()->createController($this->captchaAction); $ca = \Yii::$application->createController($this->captchaAction);
if ($ca !== null) { if ($ca !== null) {
list($controller, $actionID) = $ca; list($controller, $actionID) = $ca;
$action = $controller->createAction($actionID); $action = $controller->createAction($actionID);
} }
} else { } else {
$action = Yii::app()->getController()->createAction($this->captchaAction); $action = \Yii::$application->getController()->createAction($this->captchaAction);
} }
if ($action === null) { if ($action === null) {
......
...@@ -82,7 +82,7 @@ class StringValidator extends Validator ...@@ -82,7 +82,7 @@ class StringValidator extends Validator
} }
if (function_exists('mb_strlen') && $this->encoding !== false) { if (function_exists('mb_strlen') && $this->encoding !== false) {
$length = mb_strlen($value, $this->encoding ? $this->encoding : Yii::app()->charset); $length = mb_strlen($value, $this->encoding ? $this->encoding : \Yii::$application->charset);
} else { } else {
$length = strlen($value); $length = strlen($value);
} }
......
...@@ -13,34 +13,27 @@ class CommandTest extends \yiiunit\MysqlTestCase ...@@ -13,34 +13,27 @@ class CommandTest extends \yiiunit\MysqlTestCase
{ {
$db = $this->getConnection(false); $db = $this->getConnection(false);
// null
$command = $db->createCommand(); $command = $db->createCommand();
$this->assertEquals("SELECT *\nFROM ", $command->sql); $this->assertEquals(null, $command->sql);
// string
$sql = 'SELECT * FROM yii_post'; $sql = 'SELECT * FROM yii_post';
$command = $db->createCommand($sql); $command = $db->createCommand($sql);
$this->assertEquals($sql, $command->sql); $this->assertEquals($sql, $command->sql);
// Query object
$query = new Query; $query = new Query;
$query->select('id')->from('tbl_user');
$command = $db->createCommand($query); $command = $db->createCommand($query);
$this->assertEquals($query, $command->query); $this->assertEquals("SELECT `id`\nFROM `tbl_user`", $command->sql);
$query = array('select' => 'id', 'from' => 'yii_post'); // array
$command = $db->createCommand($query); $command = $db->createCommand(array(
$this->assertEquals($query, $command->query->toArray()); 'select' => 'name',
} 'from' => 'tbl_user',
));
function testReset() $this->assertEquals("SELECT `name`\nFROM `tbl_user`", $command->sql);
{
$db = $this->getConnection();
$command = $db->createCommand('SELECT * FROM yii_user');
$command->queryRow();
$this->assertNotEquals(null, $command->pdoStatement);
$this->assertEquals('SELECT * FROM yii_user', $command->sql);
$command->reset();
$this->assertEquals(null, $command->pdoStatement);
$this->assertNotEquals('SELECT * FROM yii_user', $command->sql);
} }
function testGetSetSql() function testGetSetSql()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment