Commit 965fe5c4 by Qiang Xue

...

parent cf10943c
......@@ -27,8 +27,10 @@ class CActiveRecordBehavior extends CModelBehavior
public function events()
{
return array_merge(parent::events(), array(
'onBeforeSave' => 'beforeSave',
'onAfterSave' => 'afterSave',
'onBeforeInsert' => 'beforeInsert',
'onAfterInsert' => 'afterInsert',
'onBeforeUpdate' => 'beforeUpdate',
'onAfterUpdate' => 'afterUpdate',
'onBeforeDelete' => 'beforeDelete',
'onAfterDelete' => 'afterDelete',
'onBeforeFind' => 'beforeFind',
......@@ -42,7 +44,7 @@ class CActiveRecordBehavior extends CModelBehavior
* You may set {@link CModelEvent::isValid} to be false to quit the saving process.
* @param CModelEvent $event event parameter
*/
public function beforeSave($event)
public function beforeInsert($event)
{
}
......@@ -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}.
* @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
/**
* 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;
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 $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
* 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()
* @var string the name of the model class that this relation represents
*/
public $select;
public $modelClass;
/**
* @var string|array query condition. This refers to the WHERE clause in a SQL statement.
* For example, `age > 31 AND team = 1`.
* @see where()
* @var boolean whether this relation is a one-many relation
*/
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
* less than 0, it means starting from the beginning.
* @var string the table alias used for the corresponding table during query
*/
public $offset;
public $tableAlias;
/**
* @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')`).
* @var string the name of the column that the result should be indexed by.
* 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.
* It can be either a string (e.g. `'company, department'`) or an array (e.g. `array('company', 'department')`).
* @var string the ON clause of the join query
*/
public $groupBy;
public $on;
/**
* @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()
* @var string
*/
public $join;
public $via;
/**
* @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.
* @var array the relations that should be queried together (eager loading)
*/
public $having;
public $with;
/**
* @var array list of query parameter values indexed by parameter placeholders.
* For example, `array(':name'=>'Dan', ':age'=>31)`.
* @var array the scopes that should be applied during query
*/
public $params;
public $scopes;
}
......@@ -64,27 +64,15 @@ class Command extends \yii\base\Component
/**
* 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 string|array|Query $query the DB query to be executed. This can be:
*
* - 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
* @param string $sql the SQL statement to be executed
* @param array $params the parameters to be bound to the SQL statement
*/
public function __construct($connection, $query = null)
public function __construct($connection, $sql = null, $params = array())
{
$this->connection = $connection;
if (is_array($query)) {
$query = Query::newInstance($query);
}
if ($query instanceof Query) {
$this->_sql = $query->getSql($connection);
$this->bindValues($query->params);
} else {
$this->_sql = $query;
}
$this->_sql = $sql;
$this->bindValues($params);
}
/**
......@@ -233,8 +221,6 @@ class Command extends \yii\base\Component
$paramLog = "\nParameters: " . var_export($this->_params, true);
}
echo "Executing SQL: {$sql}{$paramLog}" . "\n\n";
\Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__);
try {
......@@ -368,8 +354,6 @@ echo "Executing SQL: {$sql}{$paramLog}" . "\n\n";
$paramLog = "\nParameters: " . var_export($this->_params, true);
}
echo "Executing SQL: {$sql}{$paramLog}" . "\n\n";
\Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__);
if ($db->queryCachingCount > 0 && $db->queryCachingDuration >= 0 && $method !== '') {
......
......@@ -399,17 +399,14 @@ class Connection extends \yii\base\ApplicationComponent
/**
* Creates a command for execution.
* @param string|array|Query $query the DB query to be executed. This can be:
*
* - 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]]
* @param string $sql the SQL statement to be executed
* @param array $params the parameters to be bound to the SQL statement
* @return Command the DB command
*/
public function createCommand($query = null)
public function createCommand($sql = null, $params = array())
{
$this->open();
return new Command($this, $query);
return new Command($this, $sql, $params);
}
/**
......
......@@ -13,9 +13,9 @@ namespace yii\db\dao;
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.
*
* @author Qiang Xue <qiang.xue@gmail.com>
......@@ -57,35 +57,25 @@ class QueryBuilder extends \yii\base\Object
}
/**
* Generates a SQL statement from a [[Query]] object.
* Note that when generating SQL statements for INSERT and UPDATE queries,
* 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
* Generates a SELECT SQL statement from a [[BaseQuery]] object.
* @param BaseQuery $query the [[Query]] object from which the SQL statement will be generated
* @return string the generated SQL statement
*/
public function build($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(
$this->buildSelect(),
$this->buildFrom(),
$this->buildJoin(),
$this->buildWhere(),
$this->buildGroupBy(),
$this->buildHaving(),
$this->buildUnion(),
$this->buildOrderBy(),
$this->buildLimit(),
);
return implode($this->separator, array_filter($clauses));
}
$clauses = array(
$this->buildSelect(),
$this->buildFrom(),
$this->buildJoin(),
$this->buildWhere(),
$this->buildGroupBy(),
$this->buildHaving(),
$this->buildUnion(),
$this->buildOrderBy(),
$this->buildLimit(),
);
return implode($this->separator, array_filter($clauses));
}
/**
......@@ -123,7 +113,7 @@ class QueryBuilder extends \yii\base\Object
$count++;
}
}
if ($this->query instanceof Query) {
if ($this->query instanceof BaseQuery) {
$this->query->addParams($params);
}
......@@ -167,7 +157,7 @@ class QueryBuilder extends \yii\base\Object
$count++;
}
}
if ($this->query instanceof Query) {
if ($this->query instanceof BaseQuery) {
$this->query->addParams($params);
}
$sql = 'UPDATE ' . $this->quoteTableName($table) . ' SET ' . implode(', ', $lines);
......@@ -189,14 +179,18 @@ class QueryBuilder extends \yii\base\Object
* @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
* 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.
*/
public function delete($table, $condition = '')
public function delete($table, $condition = '', $params = array())
{
$sql = 'DELETE FROM ' . $this->quoteTableName($table);
if (($where = $this->buildCondition($condition)) != '') {
$sql .= ' WHERE ' . $where;
}
if ($params !== array() && $this->query instanceof BaseQuery) {
$this->query->addParams($params);
}
return $sql;
}
......@@ -631,7 +625,7 @@ class QueryBuilder extends \yii\base\Object
protected function buildSelect()
{
$select = $this->query->distinct ? 'SELECT DISTINCT' : 'SELECT';
if ($this->query->selectOption != '') {
if ($this->query->selectOption !== null) {
$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