Commit 702476d8 by Qiang Xue

...

parent 1295fbe2
......@@ -22,9 +22,9 @@ namespace yii\base;
*
* Model also provides a set of events for further customization:
*
* - [[onAfterConstruct]]: an event raised at the end of constructor
* - [[onBeforeValidate]]: an event raised at the beginning of [[validate]]
* - [[onAfterValidate]]: an event raised at the end of [[validate]]
* - [[onAfterInit]]: an event raised at the end of [[init()]]
* - [[onBeforeValidate]]: an event raised at the beginning of [[validate()]]
* - [[onAfterValidate]]: an event raised at the end of [[validate()]]
*
* You may directly use Model to store model data, or extend it with customization.
* You may also customize Model by attaching [[ModelBehavior|model behaviors]].
......@@ -46,7 +46,6 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
public function __construct($scenario = '')
{
$this->_scenario = $scenario;
$this->afterConstruct();
}
/**
......@@ -62,6 +61,7 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
public function init()
{
$this->attachBehaviors($this->behaviors());
$this->afterInit();
}
/**
......@@ -136,7 +136,7 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
* - validator type: required, specifies the validator to be used. It can be the name of a model
* class method, the name of a built-in validator, or a validator class (or its path alias).
* - on: optional, specifies the [[scenario|scenarios]] (separated by commas) when the validation
* rule can be applied. If this option is not set, the rule will apply to any scenario.
* rule can be applied. If this option is not set, the rule will apply to all scenarios.
* - additional name-value pairs can be specified to initialize the corresponding validator properties.
* Please refer to individual validator class API for possible properties.
*
......@@ -232,15 +232,15 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
}
/**
* This method is invoked at the end of model constructor.
* The default implementation raises the [[onAfterConstruct]] event.
* This method is invoked at the end of [[init()]].
* The default implementation raises the [[onAfterInit]] event.
* You may override this method to do postprocessing after model creation.
* Make sure you call the parent implementation so that the event is raised properly.
*/
public function afterConstruct()
public function afterInit()
{
if ($this->hasEventHandlers('onAfterConstruct')) {
$this->onAfterConstruct(new Event($this));
if ($this->hasEventHandlers('onAfterInit')) {
$this->onAfterInit(new Event($this));
}
}
......@@ -285,10 +285,10 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
}
/**
* This event is raised after the model instance is created by new operator.
* This event is raised at the end of [[init()]].
* @param Event $event the event parameter
*/
public function onAfterConstruct($event)
public function onAfterInit($event)
{
$this->raiseEvent(__FUNCTION__, $event);
}
......@@ -584,7 +584,7 @@ class Model extends Component implements Initable, \IteratorAggregate, \ArrayAcc
public function onUnsafeAttribute($name, $value)
{
if (YII_DEBUG) {
\Yii::warning(sprintf('Failed to set unsafe attribute "%s" in "%s".', $name, get_class($this)));
\Yii::warning("Failed to set unsafe attribute '$name' in '" . get_class($this) . "'.");
}
}
......
......@@ -24,7 +24,7 @@ class ModelBehavior extends Behavior
* Declares event handlers for owner's events.
* The default implementation returns the following event handlers:
*
* - `onAfterConstruct` event: [[afterConstruct]]
* - `onAfterInit` event: [[afterInit]]
* - `onBeforeValidate` event: [[beforeValidate]]
* - `onAfterValidate` event: [[afterValidate]]
*
......@@ -34,18 +34,18 @@ class ModelBehavior extends Behavior
public function events()
{
return array(
'onAfterConstruct' => 'afterConstruct',
'onAfterInit' => 'afterInit',
'onBeforeValidate' => 'beforeValidate',
'onAfterValidate' => 'afterValidate',
);
}
/**
* Responds to [[Model::onAfterConstruct]] event.
* Responds to [[Model::onAfterInit]] event.
* Override this method if you want to handle the corresponding event of the [[owner]].
* @param Event $event event parameter
*/
public function afterConstruct($event)
public function afterInit($event)
{
}
......
......@@ -50,7 +50,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public function renameColumn($table, $oldName, $newName)
{
$quotedTable = $this->driver->quoteTableName($table);
$quotedTable = $this->quoteTableName($table);
$row = $this->connection->createCommand('SHOW CREATE TABLE ' . $quotedTable)->queryRow();
if ($row === false) {
throw new Exception("Unable to find '$oldName' in table '$table'.");
......@@ -64,13 +64,17 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches)) {
foreach ($matches[1] as $i => $c) {
if ($c === $oldName) {
return "ALTER TABLE $quotedTable CHANGE " . $this->driver->quoteColumnName($oldName)
. ' ' . $this->driver->quoteColumnName($newName) . ' ' . $matches[2][$i];
return "ALTER TABLE $quotedTable CHANGE "
. $this->quoteColumnName($oldName, true) . ' '
. $this->quoteColumnName($newName, true) . ' '
. $matches[2][$i];
}
}
}
// try to give back a SQL anyway
return "ALTER TABLE $quotedTable CHANGE " . $this->driver->quoteColumnName($oldName) . ' ' . $newName;
return "ALTER TABLE $quotedTable CHANGE "
. $this->quoteColumnName($oldName, true) . ' '
. $this->quoteColumnName($newName, true);
}
/**
......@@ -81,7 +85,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public function dropForeignKey($name, $table)
{
return 'ALTER TABLE ' . $this->driver->quoteTableName($table)
. ' DROP FOREIGN KEY ' . $this->driver->quoteColumnName($name);
return 'ALTER TABLE ' . $this->quoteTableName($table)
. ' DROP FOREIGN KEY ' . $this->quoteColumnName($name);
}
}
......@@ -83,7 +83,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public function truncateTable($table)
{
return "DELETE FROM " . $this->driver->quoteTableName($table);
return "DELETE FROM " . $this->quoteTableName($table);
}
/**
......@@ -94,7 +94,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public function dropIndex($name, $table)
{
return 'DROP INDEX ' . $this->driver->quoteTableName($name);
return 'DROP INDEX ' . $this->quoteTableName($name);
}
/**
......@@ -105,7 +105,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public function dropColumn($table, $column)
{
throw new Exception('Dropping DB column is not supported by SQLite.');
throw new Exception(__METHOD__ . ' is not supported by SQLite.');
}
/**
......@@ -117,7 +117,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public function renameColumn($table, $oldName, $newName)
{
throw new Exception('Renaming a DB column is not supported by SQLite.');
throw new Exception(__METHOD__ . ' is not supported by SQLite.');
}
/**
......@@ -136,7 +136,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
{
throw new Exception('Adding a foreign key constraint to an existing table is not supported by SQLite.');
throw new Exception(__METHOD__ . ' is not supported by SQLite.');
}
/**
......@@ -147,7 +147,7 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public function dropForeignKey($name, $table)
{
throw new Exception('Dropping a foreign key constraint is not supported by SQLite.');
throw new Exception(__METHOD__ . ' is not supported by SQLite.');
}
/**
......@@ -162,6 +162,6 @@ class QueryBuilder extends \yii\db\dao\QueryBuilder
*/
public function alterColumn($table, $column, $type)
{
throw new Exception('Altering a DB column is not supported by SQLite.');
throw new Exception(__METHOD__ . ' is not supported by SQLite.');
}
}
......@@ -26,14 +26,14 @@ class CommandTest extends \yiiunit\MysqlTestCase
$query = new Query;
$query->select('id')->from('tbl_user');
$command = $db->createCommand($query);
$this->assertEquals("SELECT `id`\nFROM `tbl_user`", $command->sql);
$this->assertEquals("SELECT `id` FROM `tbl_user`", $command->sql);
// array
$command = $db->createCommand(array(
'select' => 'name',
'from' => 'tbl_user',
));
$this->assertEquals("SELECT `name`\nFROM `tbl_user`", $command->sql);
$this->assertEquals("SELECT `name` FROM `tbl_user`", $command->sql);
}
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