| 包 | system.web |
|---|---|
| 继承 | class CFormModel » CModel » CComponent |
| 实现 | ArrayAccess, Traversable, IteratorAggregate |
| 子类 | CCodeModel |
| 源自 | 1.0 |
| 版本 | $Id: CFormModel.php 3515 2011-12-28 12:29:24Z mDOMba $ |
| 源码 |
CFormModel代表收集HTML表单输入的数据模型。
不同于CActiveRecord,CFormModel收集的数据只存储在内存中, 而不是数据库中。
收集用户输入,你可能会扩展CFormModel定义的属性以接受用户的输入信息。 你可能会根据应用的属性覆盖 rules()来声明 验证规则。
不同于CActiveRecord,CFormModel收集的数据只存储在内存中, 而不是数据库中。
收集用户输入,你可能会扩展CFormModel定义的属性以接受用户的输入信息。 你可能会根据应用的属性覆盖 rules()来声明 验证规则。
公共属性
| 属性 | 类型 | 描述 | 定义在 |
|---|---|---|---|
| attributes | array | Returns all attribute values. | CModel |
| errors | array | Returns the errors for all attribute or a single attribute. | CModel |
| iterator | CMapIterator | Returns an iterator for traversing the attributes in the model. | CModel |
| safeAttributeNames | array | Returns the attribute names that are safe to be massively assigned. | CModel |
| scenario | string | Returns the scenario that this model is used in. | CModel |
| validatorList | CList | Returns all the validators declared in the model. | CModel |
| validators | array | Returns the validators applicable to the current scenario. | CModel |
公共方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| __call() | 如果类中没有调的方法名,则调用这个方法。 | CComponent |
| __construct() | 构造方法。 | CFormModel |
| __get() | 返回一个属性值、一个事件处理程序列表或一个行为名称。 | CComponent |
| __isset() | 检查一个属性是否为null。 | CComponent |
| __set() | 设置一个组件的属性值。 | CComponent |
| __unset() | 设置一个组件的属性为null。 | CComponent |
| addError() | Adds a new error to the specified attribute. | CModel |
| addErrors() | Adds a list of errors. | CModel |
| asa() | 返回这个名字的行为对象。 | CComponent |
| attachBehavior() | 附加一个行为到组件。 | CComponent |
| attachBehaviors() | 附加一个行为列表到组件。 | CComponent |
| attachEventHandler() | 为事件附加一个事件处理程序。 | CComponent |
| attributeLabels() | Returns the attribute labels. | CModel |
| attributeNames() | 返回属性名列表。 | CFormModel |
| behaviors() | Returns a list of behaviors that this model should behave as. | CModel |
| canGetProperty() | 确定属性是否可读。 | CComponent |
| canSetProperty() | 确定属性是否可写。 | CComponent |
| clearErrors() | Removes errors for all attributes or a single attribute. | CModel |
| createValidators() | Creates validator objects based on the specification in rules. | CModel |
| detachBehavior() | 从组件中分离一个行为。 | CComponent |
| detachBehaviors() | 从组件中分离所有行为。 | CComponent |
| detachEventHandler() | 分离一个存在的事件处理程序。 | CComponent |
| disableBehavior() | 禁用一个附加行为。 | CComponent |
| disableBehaviors() | 禁用组件附加的所有行为。 | CComponent |
| enableBehavior() | 启用一个附加行为。 | CComponent |
| enableBehaviors() | 启用组件附加的所有行为。 | CComponent |
| evaLuateExpression() | 计算一个PHP表达式,或根据组件上下文执行回调。 | CComponent |
| generateAttributeLabel() | Generates a user friendly attribute label. | CModel |
| getAttributeLabel() | Returns the text label for the specified attribute. | CModel |
| getAttributes() | Returns all attribute values. | CModel |
| getError() | Returns the first error of the specified attribute. | CModel |
| getErrors() | Returns the errors for all attribute or a single attribute. | CModel |
| getEventHandlers() | 返回一个事件的附加处理程序列表。 | CComponent |
| getIterator() | Returns an iterator for traversing the attributes in the model. | CModel |
| getSafeAttributeNames() | Returns the attribute names that are safe to be massively assigned. | CModel |
| getScenario() | Returns the scenario that this model is used in. | CModel |
| getValidatorList() | Returns all the validators declared in the model. | CModel |
| getValidators() | Returns the validators applicable to the current scenario. | CModel |
| hasErrors() | Returns a value indicating whether there is any validation error. | CModel |
| hasEvent() | 确定一个事件是否定义。 | CComponent |
| hasEventHandler() | 检查事件是否有附加的处理程序。 | CComponent |
| hasProperty() | 确定属性是否被定义。 | CComponent |
| init() | 初始化模型。 | CFormModel |
| isAttributeRequired() | Returns a value indicating whether the attribute is required. | CModel |
| isAttributeSafe() | Returns a value indicating whether the attribute is safe for massive assignments. | CModel |
| offsetExists() | Returns whether there is an element at the specified offset. | CModel |
| offsetGet() | Returns the element at the specified offset. | CModel |
| offsetSet() | Sets the element at the specified offset. | CModel |
| offsetUnset() | Unsets the element at the specified offset. | CModel |
| onAfterConstruct() | This event is raised after the model instance is created by new operator. | CModel |
| onAfterValidate() | This event is raised after the validation is performed. | CModel |
| onBeforeValidate() | This event is raised before the validation is performed. | CModel |
| onUnsafeAttribute() | This method is invoked when an unsafe attribute is being massively assigned. | CModel |
| raiseEvent() | 发起一个事件。 | CComponent |
| rules() | Returns the validation rules for attributes. | CModel |
| setAttributes() | Sets the attribute values in a massive way. | CModel |
| setScenario() | Sets the scenario for the model. | CModel |
| unsetAttributes() | Sets the attributes to be null. | CModel |
| validate() | Performs the validation. | CModel |
受保护方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| afterConstruct() | This method is invoked after a model instance is created by new operator. | CModel |
| afterValidate() | This method is invoked after validation ends. | CModel |
| beforeValidate() | This method is invoked before validation starts. | CModel |
事件
| 事件 | 描述 | 定义在 |
|---|---|---|
| onAfterConstruct | This event is raised after the model instance is created by new operator. | CModel |
| onBeforeValidate | This event is raised before the validation is performed. | CModel |
| onAfterValidate | This event is raised after the validation is performed. | CModel |
| onUnsafeAttribute | This method is invoked when an unsafe attribute is being massively assigned. | CModel |
方法详细
__construct()
方法
|
public void __construct(string $scenario='')
| ||
| $scenario | string | 模型使用的场景名称。 See CModel::scenario如何使用模型场景。 |
public function __construct($scenario='')
{
$this->setScenario($scenario);
$this->init();
$this->attachBehaviors($this->behaviors());
$this->afterConstruct();
}
构造方法。
参见
- getScenario
attributeNames()
方法
|
public array attributeNames()
| ||
| {return} | array | 属性名列表。默认为类的所有公共属性。 |
public function attributeNames()
{
$className=get_class($this);
if(!isset(self::$_names[$className]))
{
$class=new ReflectionClass(get_class($this));
$names=array();
foreach($class->getProperties() as $property)
{
$name=$property->getName();
if($property->isPublic() && !$property->isStatic())
$names[]=$name;
}
return self::$_names[$className]=$names;
}
else
return self::$_names[$className];
}
返回属性名列表。 默认,这个方法返回所有的公共属性。 您可以重写此方法来更改默认。
init()
方法
|
public void init()
|
public function init()
{
}
初始化模型。 在scenario设置后调用此方法构造。 您可以重写此方法,以提供所需要的代码来初始化模型 (如:设置初始属性值。)