| 包 | zii.behaviors |
|---|---|
| 继承 | class CTimestampBehavior » CActiveRecordBehavior » CModelBehavior » CBehavior » CComponent |
| 实现 | IBehavior |
| 源自 | 1.1 |
| 版本 | $Id: CTimestampBehavior.PHP 3229 2011-05-21 00:20:29Z alexander.makarow $ |
| 源码 |
CTimestampBehavior会自动填充日期和时间相关的属性。
会自动填充日期时间属性当active record 被创建以及/或者更新时。 你可以像下面这样指定一个active record模型类来使用此behavior:
默认情况下,update属性只在记录被更新是才被设置。如果你想让其在记录被创建的时候也被设置, 可将setUpdateOnCreate选项设置为true。
尽管CTimestampBehavior会自行决定将什么样的值写入timestamp类型的属性, 你仍然可以通过timestampExpression用自定义的值来替代它
会自动填充日期时间属性当active record 被创建以及/或者更新时。 你可以像下面这样指定一个active record模型类来使用此behavior:
public function behaviors(){
return array(
'CTimestampBehavior' => array(
'class' => 'zii.behaviors.CTimestampBehavior',
'createAttribute' => 'create_time_attribute',
'updateAttribute' => 'update_time_attribute',
)
);
}
createAttribute和updateAttribute选项实际上分别默认为‘create_time’和‘update_time’,
因此设置它们并非必须。如果你不希望CTimestampBehavior
为该record的更新或创建设置一个timestamp值,可以设定相应的属性选项为null。
默认情况下,update属性只在记录被更新是才被设置。如果你想让其在记录被创建的时候也被设置, 可将setUpdateOnCreate选项设置为true。
尽管CTimestampBehavior会自行决定将什么样的值写入timestamp类型的属性, 你仍然可以通过timestampExpression用自定义的值来替代它
公共属性
| 属性 | 类型 | 描述 | 定义在 |
|---|---|---|---|
| createAttribute | mixed | 用来存储创建时间属性的名称。 设置为null是创建属性不使用一个时间戳。默认是‘create_time’ | CTimestampBehavior |
| enabled | boolean | 事件是否被启用。 | CBehavior |
| owner | CComponent | 获得附加行为的组件。 | CBehavior |
| setUpdateOnCreate | bool | 更新属性是否设置在创建时创建时间戳。 否则将被单独留下。默认为false。 | CTimestampBehavior |
| timestampExpression | mixed | 将用于生成时间戳的表达式。 它可以是一个字符串,代表一个PHP表达式(例如:'itme()'), 或者一个CDbExpression对象代表一个DB表达式(例如:new CDbExpression('NOW()'))。 默认为空,这意味着我们将尝试自动计算出相应的时间戳。 如果我们不能找到合适的时间戳, 然后,它会后退到当前的UNIX时间戳 | CTimestampBehavior |
| updateAttribute | mixed | 用来存储修改时间属性的名称。 设置为null是更新属性不使用一个时间戳。默认是‘update_time’ | CTimestampBehavior |
受保护属性
| 属性 | 类型 | 描述 | 定义在 |
|---|---|---|---|
| map | array | 映射列类型到数据库的方法 | CTimestampBehavior |
公共方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| __call() | 如果类中没有调的方法名,则调用这个方法。 | CComponent |
| __get() | 返回一个属性值、一个事件处理程序列表或一个行为名称。 | CComponent |
| __isset() | 检查一个属性是否为null。 | CComponent |
| __set() | 设置一个组件的属性值。 | CComponent |
| __unset() | 设置一个组件的属性为null。 | CComponent |
| afterConstruct() | 响应CModel::onAfterConstruct事件。 | CModelBehavior |
| afterDelete() | 响应到 CActiveRecord::onAfterDelete 事件。 | CActiveRecordBehavior |
| afterFind() | 响应到 CActiveRecord::onAfterFind 事件。 | CActiveRecordBehavior |
| afterSave() | 响应到 CActiveRecord::onAfterSave 事件。 | CActiveRecordBehavior |
| afterValidate() | 响应CModel::onAfterValidate事件。 | CModelBehavior |
| asa() | 返回这个名字的行为对象。 | CComponent |
| attach() | 附加事件处理程序到组件。 | CBehavior |
| attachBehavior() | 附加一个行为到组件。 | CComponent |
| attachBehaviors() | 附加一个行为列表到组件。 | CComponent |
| attachEventHandler() | 为事件附加一个事件处理程序。 | CComponent |
| beforeDelete() | 响应到 CActiveRecord::onBeforeDelete 事件。 | CActiveRecordBehavior |
| beforeFind() | 响应到 CActiveRecord::onBeforeFind 事件。 | CActiveRecordBehavior |
| beforeSave() | 响应CModel::onBeforeSave事件。 | CTimestampBehavior |
| beforeValidate() | 响应CModel::onBeforeValidate事件。 | CModelBehavior |
| canGetProperty() | 确定属性是否可读。 | CComponent |
| canSetProperty() | 确定属性是否可写。 | CComponent |
| detach() | 从组件分离行为对象。 | CBehavior |
| detachBehavior() | 从组件中分离一个行为。 | CComponent |
| detachBehaviors() | 从组件中分离所有行为。 | CComponent |
| detachEventHandler() | 分离一个存在的事件处理程序。 | CComponent |
| disableBehavior() | 禁用一个附加行为。 | CComponent |
| disableBehaviors() | 禁用组件附加的所有行为。 | CComponent |
| enableBehavior() | 启用一个附加行为。 | CComponent |
| enableBehaviors() | 启用组件附加的所有行为。 | CComponent |
| evaLuateExpression() | 计算一个PHP表达式,或根据组件上下文执行回调。 | CComponent |
| events() | 定义事件和相应的事件处理方法。 | CActiveRecordBehavior |
| getEnabled() | 返回事件是否被启用。 | CBehavior |
| getEventHandlers() | 返回一个事件的附加处理程序列表。 | CComponent |
| getOwner() | 返回获得附加行为的组件。 | CBehavior |
| hasEvent() | 确定一个事件是否定义。 | CComponent |
| hasEventHandler() | 检查事件是否有附加的处理程序。 | CComponent |
| hasProperty() | 确定属性是否被定义。 | CComponent |
| raiseEvent() | 发起一个事件。 | CComponent |
| setEnabled() | 设置是否启用行为 | CBehavior |
受保护方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| getTimestampByAttribute() | 获得合适的时间戳取决于列类型$attribute | CTimestampBehavior |
| getTimestampByColumnType() | 返回合适的时间戳取决于$columnType | CTimestampBehavior |
属性详细
createAttribute
属性
public mixed $createAttribute;
用来存储创建时间属性的名称。 设置为null是创建属性不使用一个时间戳。默认是‘create_time’
map
属性
protected static array $map;
映射列类型到数据库的方法
setUpdateOnCreate
属性
public bool $setUpdateOnCreate;
更新属性是否设置在创建时创建时间戳。 否则将被单独留下。默认为false。
timestampExpression
属性
public mixed $timestampExpression;
将用于生成时间戳的表达式。 它可以是一个字符串,代表一个PHP表达式(例如:'itme()'), 或者一个CDbExpression对象代表一个DB表达式(例如:new CDbExpression('NOW()'))。 默认为空,这意味着我们将尝试自动计算出相应的时间戳。 如果我们不能找到合适的时间戳, 然后,它会后退到当前的UNIX时间戳
updateAttribute
属性
public mixed $updateAttribute;
用来存储修改时间属性的名称。 设置为null是更新属性不使用一个时间戳。默认是‘update_time’
方法详细
beforeSave()
方法
|
public void beforeSave(CModelEvent $event)
| ||
| $event | CModelEvent | 事件参数 |
public function beforeSave($event) {
if ($this->getOwner()->getIsNewRecord() && ($this->createAttribute !== null)) {
$this->getOwner()->{$this->createAttribute} = $this->getTimestampByAttribute($this->createAttribute);
}
if ((!$this->getOwner()->getIsNewRecord() || $this->setUpdateOnCreate) && ($this->updateAttribute !== null)) {
$this->getOwner()->{$this->updateAttribute} = $this->getTimestampByAttribute($this->updateAttribute);
}
}
响应CModel::onBeforeSave事件。 设置创建或修改的属性值的配置
getTimestampByAttribute()
方法
|
protected mixed getTimestampByAttribute(string $attribute)
| ||
| $attribute | string | $attribute |
| {return} | mixed | 时间戳(例如,unix时间戳或者一个mysql函数) |
protected function getTimestampByAttribute($attribute) {
if ($this->timestampExpression instanceof CDbExpression)
return $this->timestampExpression;
else if ($this->timestampExpression !== null)
return @eval('return '.$this->timestampExpression.';');
$columnType = $this->getOwner()->getTableSchema()->getColumn($attribute)->dbType;
return $this->getTimestampByColumnType($columnType);
}
获得合适的时间戳取决于列类型$attribute
getTimestampByColumnType()
方法
|
protected mixed getTimestampByColumnType(string $columnType)
| ||
| $columnType | string | $columnType |
| {return} | mixed | 时间戳(例如,unix时间戳或者一个mysql函数) |
protected function getTimestampByColumnType($columnType) {
return isset(self::$map[$columnType]) ? new CDbExpression(self::$map[$columnType]) : time();
}
返回合适的时间戳取决于$columnType