Commit 965fe5c4 by Qiang Xue

...

parent cf10943c
...@@ -27,8 +27,10 @@ class CActiveRecordBehavior extends CModelBehavior ...@@ -27,8 +27,10 @@ class CActiveRecordBehavior extends CModelBehavior
public function events() public function events()
{ {
return array_merge(parent::events(), array( return array_merge(parent::events(), array(
'onBeforeSave' => 'beforeSave', 'onBeforeInsert' => 'beforeInsert',
'onAfterSave' => 'afterSave', 'onAfterInsert' => 'afterInsert',
'onBeforeUpdate' => 'beforeUpdate',
'onAfterUpdate' => 'afterUpdate',
'onBeforeDelete' => 'beforeDelete', 'onBeforeDelete' => 'beforeDelete',
'onAfterDelete' => 'afterDelete', 'onAfterDelete' => 'afterDelete',
'onBeforeFind' => 'beforeFind', 'onBeforeFind' => 'beforeFind',
...@@ -42,7 +44,7 @@ class CActiveRecordBehavior extends CModelBehavior ...@@ -42,7 +44,7 @@ class CActiveRecordBehavior extends CModelBehavior
* You may set {@link CModelEvent::isValid} to be false to quit the saving process. * You may set {@link CModelEvent::isValid} to be false to quit the saving process.
* @param CModelEvent $event event parameter * @param CModelEvent $event event parameter
*/ */
public function beforeSave($event) public function beforeInsert($event)
{ {
} }
...@@ -51,7 +53,26 @@ class CActiveRecordBehavior extends CModelBehavior ...@@ -51,7 +53,26 @@ class CActiveRecordBehavior extends CModelBehavior
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}. * Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* @param CModelEvent $event event parameter * @param CModelEvent $event event parameter
*/ */
public function afterSave($event) public function afterInsert($event)
{
}
/**
* Responds to {@link CActiveRecord::onBeforeSave} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* You may set {@link CModelEvent::isValid} to be false to quit the saving process.
* @param CModelEvent $event event parameter
*/
public function beforeUpdate($event)
{
}
/**
* Responds to {@link CActiveRecord::onAfterSave} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* @param CModelEvent $event event parameter
*/
public function afterUpdate($event)
{ {
} }
......
<?php <?php
/**
* ActiveRelation 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; namespace yii\db\ar;
class ActiveRelation extends \yii\base\Object use yii\db\dao\BaseQuery;
/**
* ActiveRelation represents the specification of a relation declared in [[ActiveRecord::relations()]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveRelation extends BaseQuery
{ {
/**
* @var string the name of this relation
*/
public $name; public $name;
public $modelClass;
public $hasMany;
public $joinType;
public $tableAlias;
public $on;
public $via;
public $with;
public $scopes;
/** /**
* @var string|array the columns being selected. This refers to the SELECT clause in a SQL * @var string the name of the model class that this relation represents
* statement. It can be either a string (e.g. `'id, name'`) or an array (e.g. `array('id', 'name')`).
* If not set, if means all columns.
* @see select()
*/ */
public $select; public $modelClass;
/** /**
* @var string|array query condition. This refers to the WHERE clause in a SQL statement. * @var boolean whether this relation is a one-many relation
* For example, `age > 31 AND team = 1`.
* @see where()
*/ */
public $where; public $hasMany;
/** /**
* @var integer maximum number of records to be returned. If not set or less than 0, it means no limit. * @var string the join type (e.g. INNER JOIN, LEFT JOIN). Defaults to 'LEFT JOIN'.
*/ */
public $limit; public $joinType = 'LEFT JOIN';
/** /**
* @var integer zero-based offset from where the records are to be returned. If not set or * @var string the table alias used for the corresponding table during query
* less than 0, it means starting from the beginning.
*/ */
public $offset; public $tableAlias;
/** /**
* @var string|array how to sort the query results. This refers to the ORDER BY clause in a SQL statement. * @var string the name of the column that the result should be indexed by.
* It can be either a string (e.g. `'id ASC, name DESC'`) or an array (e.g. `array('id ASC', 'name DESC')`). * This is only useful when [[hasMany]] is true.
*/ */
public $orderBy; public $indexBy;
/** /**
* @var string|array how to group the query results. This refers to the GROUP BY clause in a SQL statement. * @var string the ON clause of the join query
* It can be either a string (e.g. `'company, department'`) or an array (e.g. `array('company', 'department')`).
*/ */
public $groupBy; public $on;
/** /**
* @var string|array how to join with other tables. This refers to the JOIN clause in a SQL statement. * @var string
* 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; public $via;
/** /**
* @var string|array the condition to be applied in the GROUP BY clause. * @var array the relations that should be queried together (eager loading)
* It can be either a string or an array. Please refer to [[where()]] on how to specify the condition.
*/ */
public $having; public $with;
/** /**
* @var array list of query parameter values indexed by parameter placeholders. * @var array the scopes that should be applied during query
* For example, `array(':name'=>'Dan', ':age'=>31)`.
*/ */
public $params; public $scopes;
} }
...@@ -64,27 +64,15 @@ class Command extends \yii\base\Component ...@@ -64,27 +64,15 @@ class Command extends \yii\base\Component
/** /**
* Constructor. * Constructor.
* Instead of using the `new` operator, you may use [[Connection::createCommand()]]
* to create a new Command object.
* @param Connection $connection the database connection * @param Connection $connection the database connection
* @param string|array|Query $query the DB query to be executed. This can be: * @param string $sql the SQL statement to be executed
* * @param array $params the parameters to be bound to the SQL statement
* - a string representing the SQL statement to be executed
* - a [[Query]] object representing the SQL query
* - an array that will be used to create and initialize the [[Query]] object
*/ */
public function __construct($connection, $query = null) public function __construct($connection, $sql = null, $params = array())
{ {
$this->connection = $connection; $this->connection = $connection;
if (is_array($query)) { $this->_sql = $sql;
$query = Query::newInstance($query); $this->bindValues($params);
}
if ($query instanceof Query) {
$this->_sql = $query->getSql($connection);
$this->bindValues($query->params);
} else {
$this->_sql = $query;
}
} }
/** /**
...@@ -233,8 +221,6 @@ class Command extends \yii\base\Component ...@@ -233,8 +221,6 @@ class Command extends \yii\base\Component
$paramLog = "\nParameters: " . var_export($this->_params, true); $paramLog = "\nParameters: " . var_export($this->_params, true);
} }
echo "Executing SQL: {$sql}{$paramLog}" . "\n\n";
\Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__); \Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__);
try { try {
...@@ -368,8 +354,6 @@ echo "Executing SQL: {$sql}{$paramLog}" . "\n\n"; ...@@ -368,8 +354,6 @@ echo "Executing SQL: {$sql}{$paramLog}" . "\n\n";
$paramLog = "\nParameters: " . var_export($this->_params, true); $paramLog = "\nParameters: " . var_export($this->_params, true);
} }
echo "Executing SQL: {$sql}{$paramLog}" . "\n\n";
\Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__); \Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__);
if ($db->queryCachingCount > 0 && $db->queryCachingDuration >= 0 && $method !== '') { if ($db->queryCachingCount > 0 && $db->queryCachingDuration >= 0 && $method !== '') {
......
...@@ -399,17 +399,14 @@ class Connection extends \yii\base\ApplicationComponent ...@@ -399,17 +399,14 @@ class Connection extends \yii\base\ApplicationComponent
/** /**
* Creates a command for execution. * Creates a command for execution.
* @param string|array|Query $query the DB query to be executed. This can be: * @param string $sql the SQL statement to be executed
* * @param array $params the parameters to be bound to the SQL statement
* - a string representing the SQL statement to be executed
* - a [[Query]] object representing the SQL query
* - an array that will be used to initialize [[Query]]
* @return Command the DB command * @return Command the DB command
*/ */
public function createCommand($query = null) public function createCommand($sql = null, $params = array())
{ {
$this->open(); $this->open();
return new Command($this, $query); return new Command($this, $sql, $params);
} }
/** /**
......
...@@ -13,9 +13,9 @@ namespace yii\db\dao; ...@@ -13,9 +13,9 @@ namespace yii\db\dao;
use yii\db\Exception; use yii\db\Exception;
/** /**
* QueryBuilder builds a SQL statement based on the specification given as a [[Query]] object. * QueryBuilder builds a SELECT SQL statement based on the specification given as a [[BaseQuery]] object.
* *
* QueryBuilder is often used behind the scenes by [[Query]] to build a DBMS-dependent SQL statement * QueryBuilder can also be used to build SQL statements such as INSERT, UPDATE, DELETE, CREATE TABLE,
* from a [[Query]] object. * from a [[Query]] object.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
...@@ -57,22 +57,13 @@ class QueryBuilder extends \yii\base\Object ...@@ -57,22 +57,13 @@ class QueryBuilder extends \yii\base\Object
} }
/** /**
* Generates a SQL statement from a [[Query]] object. * Generates a SELECT SQL statement from a [[BaseQuery]] object.
* Note that when generating SQL statements for INSERT and UPDATE queries, * @param BaseQuery $query the [[Query]] object from which the SQL statement will be generated
* the query object's [[Query::params]] property may be appended with new parameters.
* @param Query $query the [[Query]] object from which the SQL statement will be generated
* @return string the generated SQL statement * @return string the generated SQL statement
*/ */
public function build($query) public function build($query)
{ {
$this->query = $query; $this->query = $query;
if ($query->operation !== null) {
// non-SELECT query
$params = $query->operation;
$method = array_shift($params);
return call_user_func_array(array($this, $method), $params);
} else {
// SELECT query
$clauses = array( $clauses = array(
$this->buildSelect(), $this->buildSelect(),
$this->buildFrom(), $this->buildFrom(),
...@@ -86,7 +77,6 @@ class QueryBuilder extends \yii\base\Object ...@@ -86,7 +77,6 @@ class QueryBuilder extends \yii\base\Object
); );
return implode($this->separator, array_filter($clauses)); return implode($this->separator, array_filter($clauses));
} }
}
/** /**
* Creates and executes an INSERT SQL statement. * Creates and executes an INSERT SQL statement.
...@@ -123,7 +113,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -123,7 +113,7 @@ class QueryBuilder extends \yii\base\Object
$count++; $count++;
} }
} }
if ($this->query instanceof Query) { if ($this->query instanceof BaseQuery) {
$this->query->addParams($params); $this->query->addParams($params);
} }
...@@ -167,7 +157,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -167,7 +157,7 @@ class QueryBuilder extends \yii\base\Object
$count++; $count++;
} }
} }
if ($this->query instanceof Query) { if ($this->query instanceof BaseQuery) {
$this->query->addParams($params); $this->query->addParams($params);
} }
$sql = 'UPDATE ' . $this->quoteTableName($table) . ' SET ' . implode(', ', $lines); $sql = 'UPDATE ' . $this->quoteTableName($table) . ' SET ' . implode(', ', $lines);
...@@ -189,14 +179,18 @@ class QueryBuilder extends \yii\base\Object ...@@ -189,14 +179,18 @@ 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 $condition the condition that will be put in the WHERE part. Please * @param mixed $condition the condition that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify condition. * refer to [[Query::where()]] on how to specify condition.
* @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, $condition = '') public function delete($table, $condition = '', $params = array())
{ {
$sql = 'DELETE FROM ' . $this->quoteTableName($table); $sql = 'DELETE FROM ' . $this->quoteTableName($table);
if (($where = $this->buildCondition($condition)) != '') { if (($where = $this->buildCondition($condition)) != '') {
$sql .= ' WHERE ' . $where; $sql .= ' WHERE ' . $where;
} }
if ($params !== array() && $this->query instanceof BaseQuery) {
$this->query->addParams($params);
}
return $sql; return $sql;
} }
...@@ -631,7 +625,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -631,7 +625,7 @@ class QueryBuilder extends \yii\base\Object
protected function buildSelect() protected function buildSelect()
{ {
$select = $this->query->distinct ? 'SELECT DISTINCT' : 'SELECT'; $select = $this->query->distinct ? 'SELECT DISTINCT' : 'SELECT';
if ($this->query->selectOption != '') { if ($this->query->selectOption !== null) {
$select .= ' ' . $this->query->selectOption; $select .= ' ' . $this->query->selectOption;
} }
......
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