我想创建一个新的模型类,它将在所有其他类中扩展。我想在管理员添加/编辑任何记录时自动设置一些字段。即 createdDate、createdIp、updatedDate、updatedIp..等。
现在我的问题所有领域都不会在所有模型中通用。所以我想检查对象是否有 createdDate 属性然后只设置它的值。如果没有条件并且模型没有 createdDate 成员,那么它将产生错误。下面是我正在尝试的代码。但它不起作用。
public function beforeSave()
{
if(isset($this->createdDate))
die('this model have createdDate field');
else
die('this model does not have createdDate field');
}
模型可能不直接,它可能会自动扩展数据库表相关类。下面是打印的对象。createdDate 字段在私人列表中。
如果我使用var_dump(property_exists($this, 'createdDate'));
它是给我假的。
AdvertisementRate Object
(
[bannerLocation_search] =>
[bannerType_search] =>
[_md:private] => CActiveRecordMetaData Object
(
[tableSchema] => CMysqlTableSchema Object
(
[schemaName] =>
[name] => tbl_advertisement_rate
[rawName] => `tbl_advertisement_rate`
[primaryKey] => advertisementRateId
[sequenceName] =>
[columns] => Array
(
[createdDate] => CMysqlColumnSchema Object
(
[name] => createdDate
[rawName] => `createdDate`
[allowNull] =>
[dbType] => datetime
[type] => string
[defaultValue] =>
[size] =>
[precision] =>
[scale] =>
[isPrimaryKey] =>
[isForeignKey] =>
[autoIncrement] =>
[_e:private] =>
[_m:private] =>
)
请帮我添加这个逻辑。
是的.. CActiveRecord 扩展到我的班级。
MasterAdmin.php
class MasterAdmin extends CActiveRecord
{
public function beforeSave()
{
echo '<pre>';
var_dump(property_exists($this, 'createdDate'));
exit;
}
}
用户.php
class User extend MasterAdmin
{
// So whenever any record added or edited, beforeSave will be called and i can autoset createddate value.
}