Commit 95135f11 by Qiang Xue

...

parent a00afed1
...@@ -179,7 +179,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -179,7 +179,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co
* @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable` * @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable`
* @throws Exception if data is neither an array nor an iterator. * @throws Exception if data is neither an array nor an iterator.
*/ */
public function copyFrom($data) public function fromArray($data)
{ {
if (is_array($data) || $data instanceof \Traversable) { if (is_array($data) || $data instanceof \Traversable) {
if ($this->_d !== array()) { if ($this->_d !== array()) {
...@@ -211,7 +211,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -211,7 +211,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co
* @param mixed $data the data to be merged with, must be an array or object implementing Traversable * @param mixed $data the data to be merged with, must be an array or object implementing Traversable
* @param boolean $recursive whether the merging should be recursive. * @param boolean $recursive whether the merging should be recursive.
* *
* @throws CException If data is neither an array nor an iterator. * @throws Exception If data is neither an array nor an iterator.
*/ */
public function mergeWith($data, $recursive = true) public function mergeWith($data, $recursive = true)
{ {
......
...@@ -342,4 +342,27 @@ class Object ...@@ -342,4 +342,27 @@ class Object
return $object; return $object;
} }
/**
* Configures the object properties with the specified array.
* @param array $array name-value pairs to be used to initialize the properties of this object.
* @return Object the object itself
*/
public function fromArray($array)
{
foreach ($array as $name => $value) {
$this->$name = $value;
}
return $this;
}
/**
* Returns the object in terms of an array.
* The default implementation will return the result of PHP function `get_object_vars()`.
* @return array the array representation of this object.
*/
public function toArray()
{
return get_object_vars($this);
}
} }
...@@ -240,7 +240,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta ...@@ -240,7 +240,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta
* @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable` * @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable`
* @throws Exception if data is neither an array nor an object implementing `Traversable`. * @throws Exception if data is neither an array nor an object implementing `Traversable`.
*/ */
public function copyFrom($data) public function fromArray($data)
{ {
if (is_array($data) || $data instanceof \Traversable) { if (is_array($data) || $data instanceof \Traversable) {
if ($this->_c > 0) { if ($this->_c > 0) {
......
...@@ -220,7 +220,9 @@ class Query extends \yii\base\Object ...@@ -220,7 +220,9 @@ class Query extends \yii\base\Object
* the `NOT LIKE` predicates. * the `NOT LIKE` predicates.
* *
* @param string|array $condition the conditions that should be put in the WHERE part. * @param string|array $condition the conditions that should be put in the WHERE part.
* @param array $params the parameters (name=>value) to be bound to the query * @param array $params the parameters (name=>value) to be bound to the query.
* For anonymous parameters, they can alternatively be specified as separate parameters to this method.
* For example, `where('type=? AND status=?', 100, 1)`.
* @return Query the query object itself * @return Query the query object itself
* @see andWhere() * @see andWhere()
* @see orWhere() * @see orWhere()
...@@ -228,6 +230,10 @@ class Query extends \yii\base\Object ...@@ -228,6 +230,10 @@ class Query extends \yii\base\Object
public function where($condition, $params = array()) public function where($condition, $params = array())
{ {
$this->where = $condition; $this->where = $condition;
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
}
$this->addParams($params); $this->addParams($params);
return $this; return $this;
} }
...@@ -237,7 +243,8 @@ class Query extends \yii\base\Object ...@@ -237,7 +243,8 @@ class Query extends \yii\base\Object
* The new condition and the existing one will be joined using the 'AND' operator. * The new condition and the existing one will be joined using the 'AND' operator.
* @param string|array $condition the new WHERE condition. Please refer to [[where()]] * @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter. * on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query * @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return Query the query object itself * @return Query the query object itself
* @see where() * @see where()
* @see orWhere() * @see orWhere()
...@@ -249,6 +256,10 @@ class Query extends \yii\base\Object ...@@ -249,6 +256,10 @@ class Query extends \yii\base\Object
} else { } else {
$this->where = array('and', $this->where, $condition); $this->where = array('and', $this->where, $condition);
} }
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
}
$this->addParams($params); $this->addParams($params);
return $this; return $this;
} }
...@@ -258,7 +269,8 @@ class Query extends \yii\base\Object ...@@ -258,7 +269,8 @@ class Query extends \yii\base\Object
* The new condition and the existing one will be joined using the 'OR' operator. * The new condition and the existing one will be joined using the 'OR' operator.
* @param string|array $condition the new WHERE condition. Please refer to [[where()]] * @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter. * on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query * @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return Query the query object itself * @return Query the query object itself
* @see where() * @see where()
* @see andWhere() * @see andWhere()
...@@ -270,6 +282,10 @@ class Query extends \yii\base\Object ...@@ -270,6 +282,10 @@ class Query extends \yii\base\Object
} else { } else {
$this->where = array('or', $this->where, $condition); $this->where = array('or', $this->where, $condition);
} }
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
}
$this->addParams($params); $this->addParams($params);
return $this; return $this;
} }
...@@ -282,12 +298,18 @@ class Query extends \yii\base\Object ...@@ -282,12 +298,18 @@ class Query extends \yii\base\Object
* (which means the table is given as a sub-query or DB expression). * (which means the table is given as a sub-query or DB expression).
* @param string|array $condition the join condition that should appear in the ON part. * @param string|array $condition the join condition that should appear in the ON part.
* Please refer to [[where()]] on how to specify this parameter. * Please refer to [[where()]] on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query * @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return Query the query object itself * @return Query the query object itself
*/ */
public function join($table, $condition, $params = array()) public function join($table, $condition, $params = array())
{ {
$this->join[] = array('JOIN', $table, $condition); $this->join[] = array('JOIN', $table, $condition);
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
array_shift($params);
}
return $this->addParams($params); return $this->addParams($params);
} }
...@@ -305,6 +327,11 @@ class Query extends \yii\base\Object ...@@ -305,6 +327,11 @@ class Query extends \yii\base\Object
public function leftJoin($table, $condition, $params = array()) public function leftJoin($table, $condition, $params = array())
{ {
$this->join[] = array('LEFT JOIN', $table, $condition); $this->join[] = array('LEFT JOIN', $table, $condition);
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
array_shift($params);
}
return $this->addParams($params); return $this->addParams($params);
} }
...@@ -322,6 +349,11 @@ class Query extends \yii\base\Object ...@@ -322,6 +349,11 @@ class Query extends \yii\base\Object
public function rightJoin($table, $condition, $params = array()) public function rightJoin($table, $condition, $params = array())
{ {
$this->join[] = array('RIGHT JOIN', $table, $condition); $this->join[] = array('RIGHT JOIN', $table, $condition);
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
array_shift($params);
}
return $this->addParams($params); return $this->addParams($params);
} }
...@@ -399,7 +431,8 @@ class Query extends \yii\base\Object ...@@ -399,7 +431,8 @@ class Query extends \yii\base\Object
* Sets the HAVING part of the query. * Sets the HAVING part of the query.
* @param string|array $condition the conditions to be put after HAVING. * @param string|array $condition the conditions to be put after HAVING.
* Please refer to [[where()]] on how to specify this parameter. * Please refer to [[where()]] on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query * @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return Query the query object itself * @return Query the query object itself
* @see andHaving() * @see andHaving()
* @see orHaving() * @see orHaving()
...@@ -407,6 +440,10 @@ class Query extends \yii\base\Object ...@@ -407,6 +440,10 @@ class Query extends \yii\base\Object
public function having($condition, $params = array()) public function having($condition, $params = array())
{ {
$this->having = $condition; $this->having = $condition;
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
}
$this->addParams($params); $this->addParams($params);
return $this; return $this;
} }
...@@ -416,7 +453,8 @@ class Query extends \yii\base\Object ...@@ -416,7 +453,8 @@ class Query extends \yii\base\Object
* The new condition and the existing one will be joined using the 'AND' operator. * The new condition and the existing one will be joined using the 'AND' operator.
* @param string|array $condition the new HAVING condition. Please refer to [[where()]] * @param string|array $condition the new HAVING condition. Please refer to [[where()]]
* on how to specify this parameter. * on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query * @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return Query the query object itself * @return Query the query object itself
* @see having() * @see having()
* @see orHaving() * @see orHaving()
...@@ -428,6 +466,10 @@ class Query extends \yii\base\Object ...@@ -428,6 +466,10 @@ class Query extends \yii\base\Object
} else { } else {
$this->having = array('and', $this->having, $condition); $this->having = array('and', $this->having, $condition);
} }
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
}
$this->addParams($params); $this->addParams($params);
return $this; return $this;
} }
...@@ -437,7 +479,8 @@ class Query extends \yii\base\Object ...@@ -437,7 +479,8 @@ class Query extends \yii\base\Object
* The new condition and the existing one will be joined using the 'OR' operator. * The new condition and the existing one will be joined using the 'OR' operator.
* @param string|array $condition the new HAVING condition. Please refer to [[where()]] * @param string|array $condition the new HAVING condition. Please refer to [[where()]]
* on how to specify this parameter. * on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query * @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return Query the query object itself * @return Query the query object itself
* @see having() * @see having()
* @see andHaving() * @see andHaving()
...@@ -449,6 +492,10 @@ class Query extends \yii\base\Object ...@@ -449,6 +492,10 @@ class Query extends \yii\base\Object
} else { } else {
$this->having = array('or', $this->having, $condition); $this->having = array('or', $this->having, $condition);
} }
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
}
$this->addParams($params); $this->addParams($params);
return $this; return $this;
} }
...@@ -530,11 +577,15 @@ class Query extends \yii\base\Object ...@@ -530,11 +577,15 @@ class Query extends \yii\base\Object
* 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.
* For example, `array(':name'=>'Dan', ':age'=>31)`. * For example, `array(':name'=>'Dan', ':age'=>31)`.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return Query the query object itself * @return Query the query object itself
* @see addParams() * @see addParams()
*/ */
public function params($params) public function params($params)
{ {
if (!is_array($params)) {
$params = func_get_args();
}
$this->params = $params; $this->params = $params;
return $this; return $this;
} }
...@@ -543,11 +594,15 @@ class Query extends \yii\base\Object ...@@ -543,11 +594,15 @@ class Query extends \yii\base\Object
* Adds additional parameters to be bound to the query. * Adds additional 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.
* For example, `array(':name'=>'Dan', ':age'=>31)`. * For example, `array(':name'=>'Dan', ':age'=>31)`.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return Query the query object itself * @return Query the query object itself
* @see params() * @see params()
*/ */
public function addParams($params) public function addParams($params)
{ {
if (!is_array($params)) {
$params = func_get_args();
}
foreach ($params as $name => $value) { foreach ($params as $name => $value) {
if (is_integer($name)) { if (is_integer($name)) {
$this->params[] = $value; $this->params[] = $value;
...@@ -578,11 +633,18 @@ class Query extends \yii\base\Object ...@@ -578,11 +633,18 @@ class Query extends \yii\base\Object
* @param array $columns the column data (name=>value) to be updated. * @param array $columns the column data (name=>value) to be updated.
* @param string|array $condition the conditions that will be put in the WHERE part. * @param string|array $condition the conditions that will be put in the WHERE part.
* Please refer to [[where()]] on how to specify this parameter. * Please refer to [[where()]] on how to specify this parameter.
* @param array $params the parameters to be bound to the query. * @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return Query the query object itself * @return Query the query object itself
*/ */
public function update($table, $columns, $condition = '', $params = array()) public function update($table, $columns, $condition = '', $params = array())
{ {
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
array_shift($params);
array_shift($params);
}
$this->addParams($params); $this->addParams($params);
$this->operation = array(__FUNCTION__, $table, $columns, $condition, array()); $this->operation = array(__FUNCTION__, $table, $columns, $condition, array());
return $this; return $this;
...@@ -593,11 +655,17 @@ class Query extends \yii\base\Object ...@@ -593,11 +655,17 @@ class Query 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 string|array $condition the conditions that will be put in the WHERE part. * @param string|array $condition the conditions that will be put in the WHERE part.
* Please refer to [[where()]] on how to specify this parameter. * Please refer to [[where()]] on how to specify this parameter.
* @param array $params the parameters to be bound to the query. * @param array $params the parameters (name=>value) to be bound to the query.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return Query the query object itself * @return Query the query object itself
*/ */
public function delete($table, $condition = '', $params = array()) public function delete($table, $condition = '', $params = array())
{ {
if (!is_array($params)) {
$params = func_get_args();
array_shift($params);
array_shift($params);
}
$this->operation = array(__FUNCTION__, $table, $condition); $this->operation = array(__FUNCTION__, $table, $condition);
return $this->addParams($params); return $this->addParams($params);
} }
...@@ -690,13 +758,13 @@ class Query extends \yii\base\Object ...@@ -690,13 +758,13 @@ class Query extends \yii\base\Object
/** /**
* Builds and executes a SQL statement for renaming a column. * Builds and executes a SQL statement for renaming a column.
* @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
* @param string $name the old name of the column. The name will be properly quoted by the method. * @param string $oldName the old name of the column. The name will be properly quoted by the method.
* @param string $newName the new name of the column. The name will be properly quoted by the method. * @param string $newName the new name of the column. The name will be properly quoted by the method.
* @return Query the query object itself * @return Query the query object itself
*/ */
public function renameColumn($table, $name, $newName) public function renameColumn($table, $oldName, $newName)
{ {
$this->operation = array(__FUNCTION__, $table, $name, $newName); $this->operation = array(__FUNCTION__, $table, $oldName, $newName);
return $this; return $this;
} }
...@@ -813,18 +881,6 @@ class Query extends \yii\base\Object ...@@ -813,18 +881,6 @@ class Query extends \yii\base\Object
} }
/** /**
* Returns the query in terms of an array.
* The array keys are the query property names, and the array values
* the corresponding property values.
* @param boolean $includeEmptyValues whether to include empty property values in the result.
* @return array the array representation of the criteria
*/
public function toArray($includeEmptyValues = false)
{
return $includeEmptyValues ? get_object_vars($this) : array_filter(get_object_vars($this));
}
/**
* Merges this query with another one. * Merges this query with another one.
* *
* The merging is done according to the following rules: * The merging is done according to the following rules:
......
...@@ -83,17 +83,17 @@ class DictionaryTest extends \yiiunit\TestCase ...@@ -83,17 +83,17 @@ class DictionaryTest extends \yiiunit\TestCase
$this->assertFalse($this->dictionary->contains('key3')); $this->assertFalse($this->dictionary->contains('key3'));
} }
public function testCopyFrom() public function testFromArray()
{ {
$array=array('key3'=>$this->item3,'key4'=>$this->item1); $array=array('key3'=>$this->item3,'key4'=>$this->item1);
$this->dictionary->copyFrom($array); $this->dictionary->fromArray($array);
$this->assertEquals(2, $this->dictionary->getCount()); $this->assertEquals(2, $this->dictionary->getCount());
$this->assertEquals($this->item3, $this->dictionary['key3']); $this->assertEquals($this->item3, $this->dictionary['key3']);
$this->assertEquals($this->item1, $this->dictionary['key4']); $this->assertEquals($this->item1, $this->dictionary['key4']);
$this->setExpectedException('yii\base\Exception'); $this->setExpectedException('yii\base\Exception');
$this->dictionary->copyFrom($this); $this->dictionary->fromArray($this);
} }
public function testMergeWith() public function testMergeWith()
......
...@@ -110,13 +110,13 @@ class VectorTest extends \yiiunit\TestCase ...@@ -110,13 +110,13 @@ class VectorTest extends \yiiunit\TestCase
$this->assertEquals(-1,$this->vector->indexOf($this->item3)); $this->assertEquals(-1,$this->vector->indexOf($this->item3));
} }
public function testCopyFrom() public function testFromArray()
{ {
$array=array($this->item3,$this->item1); $array=array($this->item3,$this->item1);
$this->vector->copyFrom($array); $this->vector->fromArray($array);
$this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1); $this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1);
$this->setExpectedException('yii\base\Exception'); $this->setExpectedException('yii\base\Exception');
$this->vector->copyFrom($this); $this->vector->fromArray($this);
} }
public function testMergeWith() public function testMergeWith()
......
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