Commit 9d8a4012 by Qiang Xue

...

parent 6d6d8d95
......@@ -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);
......
......@@ -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()
{
......
......@@ -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)
{
......
<?php
/**
* ValidationEvent class file.
* ModelEvent class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
......@@ -10,19 +10,18 @@
namespace yii\base;
/**
* ValidationEvent class.
* ModelEvent class.
*
* ValidationEvent represents the parameter needed by model validation events.
* ModelEvent represents the parameter needed by model events.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ValidationEvent extends Event
class ModelEvent 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
* @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;
}
......@@ -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();
......
......@@ -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;
}
......
<?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;
......
......@@ -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);
}
}
}
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