【发布时间】:2012-08-15 01:18:54
【问题描述】:
我有一个扩展 CActiveRecord 的模型,它具有以下 rules() 方法:
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('name, locationId', 'required'),
array('locationId', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>1024),
array('name', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, description, locationId', 'safe', 'on'=>'search'),
);
}
这是各自控制器中的actionCreate() 方法:
public function actionCreate()
{
$model = new Destination;
if ( isset($_POST['Destination']) )
{
$model->attributes = $_POST['Destination'];
// logging statement!
Yii::log("Name: {$model->name}\nLocationID: {$model->locationId}\nAll attribs:\n".CVarDumper::dumpAsString($model->attributes), 'warning', 'application.controllers.DestinationController.actionCreate');
if ( $model->save() )
$this->redirect(array('view', 'id'=>$model->id));
}
$this->render( 'create', array (
'model' => $model,
));
}
不幸的是,当我尝试使用此操作创建新记录时,我收到name 和locationId 不能为空的错误,即使它们已在表单中设置。上面actionCreate() 中的日志记录语句的输出如下所示:
2012/08/14 18:04:51 [warning] [application.controllers.DestinationController.actionCreate] Name:
LocationID:
All attribs:
array
(
'name' => 'Test Name'
'description' => 'Test Description'
'locationId' => '93'
'contactId' => ''
'accessId' => ''
'fullDistance' => ''
)
如您所见,转储 $model->attributes 显示正确的数据,但尝试获取单个属性(例如调用 $model->name 或 $model->locationId)不会返回任何结果。我通过save()、validate() 等调用跟踪了这一点,看起来这导致name 和locationId 的required 规则失败。
有什么想法吗?
编辑:附加日志信息
2012/08/15 16:58:03 [info] [application] Model:
Destination#1
(
[CActiveRecord:_md] => CActiveRecordMetaData#2
(
[tableSchema] => CMysqlTableSchema#3
(
[schemaName] => null
[name] => 'destination'
[rawName] => '`destination`'
[primaryKey] => 'id'
[sequenceName] => ''
[foreignKeys] => array
(
'accessId' => array
(
'0' => 'access'
'1' => 'id'
)
'contactId' => array
(
'0' => 'contact'
'1' => 'id'
)
'locationId' => array
(
'0' => 'location'
'1' => 'id'
)
)
[columns] => array
(
'id' => CMysqlColumnSchema#4
(
[name] => 'id'
[rawName] => '`id`'
[allowNull] => false
[dbType] => 'int(11)'
[type] => 'integer'
[defaultValue] => null
[size] => 11
[precision] => 11
[scale] => null
[isPrimaryKey] => true
[isForeignKey] => false
[autoIncrement] => true
[CComponent:_e] => null
[CComponent:_m] => null
)
'name' => CMysqlColumnSchema#5
(
[name] => 'name'
[rawName] => '`name`'
[allowNull] => false
[dbType] => 'varchar(1024)'
[type] => 'string'
[defaultValue] => null
[size] => 1024
[precision] => 1024
[scale] => null
[isPrimaryKey] => false
[isForeignKey] => false
[autoIncrement] => false
[CComponent:_e] => null
[CComponent:_m] => null
)
'description' => CMysqlColumnSchema#6
(
[name] => 'description'
[rawName] => '`description`'
[allowNull] => true
[dbType] => 'text'
[type] => 'string'
[defaultValue] => null
[size] => null
[precision] => null
[scale] => null
[isPrimaryKey] => false
[isForeignKey] => false
[autoIncrement] => false
[CComponent:_e] => null
[CComponent:_m] => null
)
'locationId' => CMysqlColumnSchema#7
(
[name] => 'locationId'
[rawName] => '`locationId`'
[allowNull] => false
[dbType] => 'int(11)'
[type] => 'integer'
[defaultValue] => null
[size] => 11
[precision] => 11
[scale] => null
[isPrimaryKey] => false
[isForeignKey] => true
[autoIncrement] => false
[CComponent:_e] => null
[CComponent:_m] => null
)
'contactId' => CMysqlColumnSchema#8
(
[name] => 'contactId'
[rawName] => '`contactId`'
[allowNull] => true
[dbType] => 'int(11)'
[type] => 'integer'
[defaultValue] => null
[size] => 11
[precision] => 11
[scale] => null
[isPrimaryKey] => false
[isForeignKey] => true
[autoIncrement] => false
[CComponent:_e] => null
[CComponent:_m] => null
)
'accessId' => CMysqlColumnSchema#9
(
[name] => 'accessId'
[rawName] => '`accessId`'
[allowNull] => true
[dbType] => 'int(11)'
[type] => 'integer'
[defaultValue] => null
[size] => 11
[precision] => 11
[scale] => null
[isPrimaryKey] => false
[isForeignKey] => true
[autoIncrement] => false
[CComponent:_e] => null
[CComponent:_m] => null
)
'fullDistance' => CMysqlColumnSchema#10
(
[name] => 'fullDistance'
[rawName] => '`fullDistance`'
[allowNull] => true
[dbType] => 'int(11)'
[type] => 'integer'
[defaultValue] => null
[size] => 11
[precision] => 11
[scale] => null
[isPrimaryKey] => false
[isForeignKey] => false
[autoIncrement] => false
[CComponent:_e] => null
[CComponent:_m] => null
)
)
[CComponent:_e] => null
[CComponent:_m] => null
)
[columns] => array
(
'id' => CMysqlColumnSchema#4(...)
'name' => CMysqlColumnSchema#5(...)
'description' => CMysqlColumnSchema#6(...)
'locationId' => CMysqlColumnSchema#7(...)
'contactId' => CMysqlColumnSchema#8(...)
'accessId' => CMysqlColumnSchema#9(...)
'fullDistance' => CMysqlColumnSchema#10(...)
)
[relations] => array
(
'location' => CBelongsToRelation#11
(
[joinType] => 'LEFT OUTER JOIN'
[on] => ''
[alias] => null
[with] => array()
[together] => null
[scopes] => null
[name] => 'location'
[className] => 'Location'
[foreignKey] => 'locationId'
[select] => '*'
[condition] => ''
[params] => array()
[group] => ''
[join] => ''
[having] => ''
[order] => ''
[CComponent:_e] => null
[CComponent:_m] => null
)
'activities' => CManyManyRelation#12
(
[limit] => -1
[offset] => -1
[index] => null
[through] => null
[joinType] => 'LEFT OUTER JOIN'
[on] => ''
[alias] => null
[with] => array()
[together] => null
[scopes] => null
[name] => 'activities'
[className] => 'Activity'
[foreignKey] => 'destinationHasActivity(destinationId, activityId)'
[select] => '*'
[condition] => ''
[params] => array()
[group] => ''
[join] => ''
[having] => ''
[order] => ''
[CComponent:_e] => null
[CComponent:_m] => null
)
'attributes' => CManyManyRelation#13
(
[limit] => -1
[offset] => -1
[index] => null
[through] => null
[joinType] => 'LEFT OUTER JOIN'
[on] => ''
[alias] => null
[with] => array()
[together] => null
[scopes] => null
[name] => 'attributes'
[className] => 'Attribute'
[foreignKey] => 'destinationHasAttribute(destinationId, attributeId)'
[select] => '*'
[condition] => ''
[params] => array()
[group] => ''
[join] => ''
[having] => ''
[order] => ''
[CComponent:_e] => null
[CComponent:_m] => null
)
'surfaces' => CManyManyRelation#14
(
[limit] => -1
[offset] => -1
[index] => null
[through] => null
[joinType] => 'LEFT OUTER JOIN'
[on] => ''
[alias] => null
[with] => array()
[together] => null
[scopes] => null
[name] => 'surfaces'
[className] => 'Surface'
[foreignKey] => 'destinationHasSurface(destinationId, surfaceId)'
[select] => '*'
[condition] => ''
[params] => array()
[group] => ''
[join] => ''
[having] => ''
[order] => ''
[CComponent:_e] => null
[CComponent:_m] => null
)
'images' => CManyManyRelation#15
(
[limit] => -1
[offset] => -1
[index] => null
[through] => null
[joinType] => 'LEFT OUTER JOIN'
[on] => ''
[alias] => null
[with] => array()
[together] => null
[scopes] => null
[name] => 'images'
[className] => 'Image'
[foreignKey] => 'destinationHasImage(destinationId, imageId)'
[select] => '*'
[condition] => ''
[params] => array()
[group] => ''
[join] => ''
[having] => ''
[order] => ''
[CComponent:_e] => null
[CComponent:_m] => null
)
)
[attributeDefaults] => array()
[CActiveRecordMetaData:_model] => Destination#16
(
[CActiveRecord:_md] => CActiveRecordMetaData#2(...)
[CActiveRecord:_new] => false
[CActiveRecord:_attributes] => array()
[CActiveRecord:_related] => array()
[CActiveRecord:_c] => null
[CActiveRecord:_pk] => null
[CActiveRecord:_alias] => 't'
[CModel:_errors] => array()
[CModel:_validators] => null
[CModel:_scenario] => ''
[CComponent:_e] => null
[CComponent:_m] => null
)
)
[CActiveRecord:_new] => true
[CActiveRecord:_attributes] => array()
[CActiveRecord:_related] => array
(
'attributes' => array
(
'name' => 'Test name'
'description' => ''
'contactId' => ''
'fullDistance' => ''
)
)
[CActiveRecord:_c] => null
[CActiveRecord:_pk] => null
[CActiveRecord:_alias] => 't'
[CModel:_errors] => array()
[CModel:_validators] => null
[CModel:_scenario] => 'insert'
[CComponent:_e] => null
[CComponent:_m] => null
)
安全属性:
2012/08/15 16:58:03 [info] [application] Safe attributes:
array
(
'0' => 'name'
'1' => 'locationId'
'2' => 'fullDistance'
'3' => 'description'
)
【问题讨论】:
-
$model->getSafeAttributeNames() 显示什么?也许还有 $model 的 var_dump (在批量分配之后)?最后,一个愚蠢的问题,但你还没有覆盖你的模型中的设置器吗?
-
@ernie 在批量分配和转储 $model->getSafeAttributes() 名称后添加了 $model 转储。没有什么不寻常的地方吗?不覆盖我的模型中的任何设置器。
-
赋值后属性为空的样子有点奇怪,即
[CActiveRecord:_attributes] => array();实际上,我不确定当属性为空时您之前的日志记录行是如何工作的?更不用说转储的 $model 不包含先前记录的数据(例如,没有描述或 locationID) -
哦,我刚刚想通了。该数据库中有一个“属性”表,以及一个引用该表的关系 $model->attributes,这搞砸了大规模分配。感谢您的帮助!
-
rad - 很高兴你能找到问题!
标签: validation activerecord yii