Browse Source

...

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
9d8a401273
  1. 11
      framework/YiiBase.php
  2. 8
      framework/base/Model.php
  3. 4
      framework/base/ModelBehavior.php
  4. 27
      framework/base/ModelEvent.php
  5. 2
      framework/base/Object.php
  6. 28
      framework/base/ValidationEvent.php
  7. 56
      framework/db/ar/ActiveFinder.php
  8. 26
      framework/db/ar/ActiveMetaData.php
  9. 731
      framework/db/ar/ActiveRecord.php
  10. 4
      framework/db/ar/JoinElement.php
  11. 20
      framework/util/Text.php

11
framework/YiiBase.php

@ -75,17 +75,16 @@ class YiiBase
);
/**
* @var array initial property values that will be applied to objects newly created via [[createObject]].
* The array keys are fully qualified namespaced class names, and the array values are the corresponding
* name-value pairs for initializing the created class instances. Please make sure class names are starting
* with a backslash. For example,
* The array keys are class names without leading backslashes "\", and the array values are the corresponding
* name-value pairs for initializing the created class instances. For example,
*
* ~~~
* array(
* '\Bar' => array(
* 'Bar' => array(
* 'prop1' => 'value1',
* 'prop2' => 'value2',
* ),
* '\mycompany\foo\Car' => array(
* 'mycompany\foo\Car' => array(
* 'prop1' => 'value1',
* 'prop2' => 'value2',
* ),
@ -375,7 +374,7 @@ class YiiBase
$object = new $class;
}
$class = '\\' . get_class($object);
$class = get_class($object);
if (isset(\Yii::$objectConfig[$class])) {
$config = array_merge(\Yii::$objectConfig[$class], $config);

8
framework/base/Model.php

@ -207,7 +207,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
public function beforeValidate()
{
if ($this->hasEventHandlers('onBeforeValidate')) {
$event = new ValidationEvent($this);
$event = new ModelEvent($this);
$this->onBeforeValidate($event);
return $event->isValid;
}
@ -229,7 +229,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
/**
* This event is raised before the validation is performed.
* @param ValidationEvent $event the event parameter
* @param ModelEvent $event the event parameter
*/
public function onBeforeValidate($event)
{
@ -457,7 +457,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/
public function generateAttributeLabel($name)
{
return Text::name2words($name, true);
return Text::camel2words($name, true);
}
/**
@ -583,7 +583,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
/**
* Returns an iterator for traversing the attributes in the model.
* This method is required by the interface IteratorAggregate.
* @return CMapIterator an iterator for traversing the items in the list.
* @return DictionaryIterator an iterator for traversing the items in the list.
*/
public function getIterator()
{

4
framework/base/ModelBehavior.php

@ -52,9 +52,9 @@ class ModelBehavior extends Behavior
/**
* Responds to [[Model::onBeforeValidate]] event.
* Override this method if you want to handle the corresponding event of the [[owner]].
* You may set the [[ValidationEvent::isValid|isValid]] property of the event parameter
* You may set the [[ModelEvent::isValid|isValid]] property of the event parameter
* to be false to cancel the validation process.
* @param ValidationEvent $event event parameter
* @param ModelEvent $event event parameter
*/
public function beforeValidate($event)
{

27
framework/base/ModelEvent.php

@ -0,0 +1,27 @@
<?php
/**
* ModelEvent class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* ModelEvent class.
*
* ModelEvent represents the parameter needed by model events.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ModelEvent extends Event
{
/**
* @var boolean whether the model is in valid status. Defaults to true.
* A model is in valid status if it passes validation, or other checks.
*/
public $isValid = true;
}

2
framework/base/Object.php

@ -299,7 +299,7 @@ class Object
*/
public static function newInstance($config = array())
{
$class = '\\' . get_called_class();
$class = get_called_class();
if (($n = func_num_args()) > 1) {
$args = func_get_args();

28
framework/base/ValidationEvent.php

@ -1,28 +0,0 @@
<?php
/**
* ValidationEvent class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* ValidationEvent class.
*
* ValidationEvent represents the parameter needed by model validation events.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ValidationEvent extends Event
{
/**
* @var boolean whether the model passes the validation by the event handler.
* Defaults to true. If it is set false, the [[Model::validate|model validation]] will be cancelled.
* @see Model::onBeforeValidate
*/
public $isValid = true;
}

56
framework/db/ar/ActiveQuery.php → framework/db/ar/ActiveFinder.php

@ -1,6 +1,6 @@
<?php
/**
* ActiveQuery class file.
* ActiveFinder class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
@ -18,10 +18,10 @@ use yii\db\Exception;
* ActiveFinder.php is ...
* todo: add SQL monitor
*
* todo: add ActiveQueryBuilder
* todo: add ActiveFinderBuilder
* todo: quote join/on part of the relational query
* todo: modify QueryBuilder about join() methods
* todo: unify ActiveQuery and ActiveRelation in query building process
* todo: unify ActiveFinder and ActiveRelation in query building process
* todo: intelligent table aliasing (first table name, then relation name, finally t?)
* todo: allow using tokens in primary query fragments
* todo: findBySql
@ -33,14 +33,14 @@ use yii\db\Exception;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \ArrayAccess, \Countable
class ActiveFinder extends \yii\base\Object implements \IteratorAggregate, \ArrayAccess, \Countable
{
/**
* @var string the name of the ActiveRecord class.
*/
public $modelClass;
/**
* @var \yii\db\dao\Query the Query object
* @var Query the Query object
*/
public $query;
/**
@ -278,7 +278,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* (which means the column contains a DB expression).
* @param string $option additional option that should be appended to the 'SELECT' keyword. For example,
* in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used.
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function select($columns, $option = '')
{
@ -289,7 +289,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
/**
* Sets the value indicating whether to SELECT DISTINCT or not.
* @param bool $value whether to SELECT DISTINCT or not.
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function distinct($value = true)
{
@ -304,7 +304,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* Table names can contain schema prefixes (e.g. 'public.tbl_user') and/or table aliases (e.g. 'tbl_user u').
* The method will automatically quote the table names unless it contains some parenthesis
* (which means the table is given as a sub-query or DB expression).
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function from($tables)
{
@ -379,7 +379,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* @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 ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see andWhere()
* @see orWhere()
*/
@ -400,7 +400,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* on how to specify this parameter.
* @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 ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see where()
* @see orWhere()
*/
@ -421,7 +421,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* on how to specify this parameter.
* @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 ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see where()
* @see andWhere()
*/
@ -445,7 +445,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* Please refer to [[where()]] on how to specify this parameter.
* @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 ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function join($table, $condition, $params = array())
{
@ -466,7 +466,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* @param string|array $condition the join condition that should appear in the ON part.
* Please refer to [[where()]] on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function leftJoin($table, $condition, $params = array())
{
@ -487,7 +487,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* @param string|array $condition the join condition that should appear in the ON part.
* Please refer to [[where()]] on how to specify this parameter.
* @param array $params the parameters (name=>value) to be bound to the query
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function rightJoin($table, $condition, $params = array())
{
@ -506,7 +506,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
* The method will automatically quote the table name unless it contains some parenthesis
* (which means the table is given as a sub-query or DB expression).
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function crossJoin($table)
{
@ -521,7 +521,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* Table name can contain schema prefix (e.g. 'public.tbl_user') and/or table alias (e.g. 'tbl_user u').
* The method will automatically quote the table name unless it contains some parenthesis
* (which means the table is given as a sub-query or DB expression).
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function naturalJoin($table)
{
@ -535,7 +535,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. array('id', 'name')).
* The method will automatically quote the column names unless a column contains some parenthesis
* (which means the column contains a DB expression).
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see addGroupBy()
*/
public function groupBy($columns)
@ -550,7 +550,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. array('id', 'name')).
* The method will automatically quote the column names unless a column contains some parenthesis
* (which means the column contains a DB expression).
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see groupBy()
*/
public function addGroupBy($columns)
@ -565,7 +565,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* Please refer to [[where()]] on how to specify this parameter.
* @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 ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see andHaving()
* @see orHaving()
*/
@ -586,7 +586,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* on how to specify this parameter.
* @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 ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see having()
* @see orHaving()
*/
@ -607,7 +607,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* on how to specify this parameter.
* @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 ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see having()
* @see andHaving()
*/
@ -627,7 +627,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array (e.g. array('id ASC', 'name DESC')).
* The method will automatically quote the column names unless a column contains some parenthesis
* (which means the column contains a DB expression).
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see addOrderBy()
*/
public function orderBy($columns)
@ -642,7 +642,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array (e.g. array('id ASC', 'name DESC')).
* The method will automatically quote the column names unless a column contains some parenthesis
* (which means the column contains a DB expression).
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see orderBy()
*/
public function addOrderBy($columns)
@ -654,7 +654,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
/**
* Sets the LIMIT part of the query.
* @param integer $limit the limit
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function limit($limit)
{
@ -665,7 +665,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
/**
* Sets the OFFSET part of the query.
* @param integer $offset the offset
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function offset($offset)
{
@ -676,7 +676,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
/**
* Appends a SQL statement using UNION operator.
* @param string|Query $sql the SQL statement to be appended using UNION
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
*/
public function union($sql)
{
@ -694,7 +694,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* @param array list of query parameter values indexed by parameter placeholders.
* For example, `array(':name'=>'Dan', ':age'=>31)`.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see addParams()
*/
public function params($params)
@ -708,7 +708,7 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* @param array list of query parameter values indexed by parameter placeholders.
* For example, `array(':name'=>'Dan', ':age'=>31)`.
* Please refer to [[where()]] on alternative syntax of specifying anonymous parameters.
* @return ActiveQuery the query object itself
* @return ActiveFinder the query object itself
* @see params()
*/
public function addParams($params)

26
framework/db/ar/ActiveMetaData.php

@ -14,6 +14,10 @@ use yii\db\dao\TableSchema;
class ActiveMetaData
{
/**
* @var ActiveMetaData[] list of ActiveMetaData instances indexed by the model class names
*/
public static $instances;
/**
* @var TableSchema the table schema information
*/
public $table;
@ -27,14 +31,32 @@ class ActiveMetaData
public $relations = array();
/**
* Returns an instance of ActiveMetaData for the specified model class.
* Note that each model class only has a single ActiveMetaData instance.
* This method will only create the ActiveMetaData instance if it is not previously
* done so for the specified model class.
* @param string $modelClass the model class name. Make sure the class name do NOT have a leading backslash "\".
* @param boolean $refresh whether to recreate the ActiveMetaData instance. Defaults to false.
* @return ActiveMetaData the ActiveMetaData instance for the specified model class.
*/
public static function getInstance($modelClass, $refresh = false)
{
if (isset(self::$instances[$modelClass]) && !$refresh) {
return self::$instances[$modelClass];
} else {
return self::$instances[$modelClass] = new self($modelClass);
}
}
/**
* Constructor.
* @param string $modelClass the model class name
*/
public function __construct($modelClass)
{
$this->modelClass = $modelClass;
$tableName = $modelClass::tableName();
$this->table = $modelClass::getDbConnection()->getDriver()->getTableSchema($tableName);
$this->modelClass = $modelClass;
if ($this->table === null) {
throw new Exception("Unable to find table '$tableName' for ActiveRecord class '$modelClass'.");
}
@ -71,7 +93,7 @@ class ActiveMetaData
$relation->name = $matches[1];
$modelClass = $matches[2];
if (strpos($modelClass, '\\') !== false) {
$relation->modelClass = '\\' . ltrim($modelClass, '\\');
$relation->modelClass = ltrim($modelClass, '\\');
} else {
$relation->modelClass = dirname($this->modelClass) . '\\' . $modelClass;
}

731
framework/db/ar/ActiveRecord.php

File diff suppressed because it is too large Load Diff

4
framework/db/ar/JoinElement.php

@ -1,6 +1,6 @@
<?php
/**
* ActiveQuery class file.
* JoinElement class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
@ -80,7 +80,7 @@ class JoinElement extends \yii\base\Object
$record = $modelClass::populateData($attributes);
foreach ($this->children as $child) {
if ($child->relation->select !== false) {
$record->initRelatedRecord($child->relation);
$record->initRelation($child->relation);
}
}
$this->records[$pk] = $record;

20
framework/util/Text.php

@ -57,26 +57,32 @@ class Text
}
/**
* Converts a class name into space-separated words.
* For example, 'PostTag' will be converted as 'Post Tag'.
* Converts a CamelCase name into space-separated words.
* For example, 'PostTag' will be converted to 'Post Tag'.
* @param string $name the string to be converted
* @param boolean $ucwords whether to capitalize the first letter in each word
* @return string the resulting words
*/
public static function name2words($name, $ucwords = true)
public static function camel2words($name, $ucwords = true)
{
$label = trim(strtolower(str_replace(array('-', '_', '.'), ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));
return $ucwords ? ucwords($label) : $label;
}
/**
* Converts a class name into a HTML ID.
* For example, 'PostTag' will be converted as 'post-tag'.
* Converts a CamelCase name into an ID in lowercase.
* Words in the ID may be concatenated using the specified character (defaults to '-').
* For example, 'PostTag' will be converted to 'post-tag'.
* @param string $name the string to be converted
* @param string $separator the character used to concatenate the words in the ID
* @return string the resulting ID
*/
public static function name2id($name)
public static function camel2id($name, $separator = '-')
{
return trim(strtolower(str_replace('_', '-', preg_replace('/(?<![A-Z])[A-Z]/', '-\0', $name))), '-');
if ($separator === '_') {
return trim(strtolower(preg_replace('/(?<![A-Z])[A-Z]/', '_\0', $name)), '_');
} else {
return trim(strtolower(str_replace('_', $separator, preg_replace('/(?<![A-Z])[A-Z]/', $separator . '\0', $name))), $separator);
}
}
}

Loading…
Cancel
Save