我想创建一个新的模型类,它将在所有其他类中扩展。我想在管理员添加/编辑任何记录时自动设置一些字段。即创建日期,创建IP,更新日期,更新IP.。等。
现在我的问题所有字段在所有模型中都不会通用。所以我想检查对象是否已创建日期属性,然后只设置它的值。如果没有条件并且模型尚未创建日期成员,则它将生成错误。下面是我正在尝试的代码。但它不起作用。
public function beforeSave()
{
if(isset($this->createdDate))
die('this model have createdDate field');
else
die('this model does not have createdDate field');
}
模型可能不是直截了当的,它可能会自动扩展数据库表相关的类。下面是打印对象。创建日期字段位于私有列表中。
如果我使用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扩展到我的班级。
硕士管理员.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.
}
您是否正在使用活动记录?
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#hasAttribute-detail
property_exists()
就是你要找的。
这样,如果该属性存在但设置为 NULL
,则上述函数将按预期返回 true,而 isset()
将返回 false,导致您认为该属性不存在。
为我工作。查看示例