开发者

Model ignores primary key property

EDIT

I have modified my table so that my primary key is id, and everything works correctly. Still trying to figure this issue out.

I have the model below that I am using to handle various eav values. The issue that I am seeing is that when I use $Model->save(), it will always try to insert and not update. Even if I set the property/value of the primaryKey.

Event when inserting, I have noticed that I get this query the query below in my logs. The primary key of the table is value_id.

SELECT COUNT(*) AS `count` FROM `user_entity_varchars` AS `UserEntityVarchar` 
    WHERE `UserEntityVarchar`.`id` = '1'

When I output the debug of the model's structure, this is what I am seeing:

[useDbConfig] => default
[useTable] => user_entity_varchars
[displayField] => 
[id] => 
[data开发者_如何学Python] => Array
    (
    )

[table] => user_entity_varchars
[primaryKey] => id

For some reason it is not recognizing that the primary key is set in the model class.

This is the model that I am using:

<?php
class UserEntityVarchar extends UserAgentAppModel {
var $name = 'UserEntityVarchar';
var $primaryKey = 'value_id';

var $belongsTo = array(
    'UserEntity'=>array(
    'foreignKey' => 'entity_id'
    ));
var $hasOne = array(
    'EavAttribute' => array(
    'foreignKey' => 'attribute_id'
    ));

}
?>


You are setting the primaryKey on the model as "value_id", try changing this line to:

 <?php
class UserEntityVarchar extends UserAgentAppModel {
var $name = 'UserEntityVarchar';
/*var $primaryKey = 'value_id'; Change this to*/
var $primaryKey = 'id';

var $belongsTo = array(
    'UserEntity'=>array(
    'foreignKey' => 'entity_id'
    ));
var $hasOne = array(
    'EavAttribute' => array(
    'foreignKey' => 'attribute_id'
    ));

}
?>

Hope it helps! cheers

pd, it is my first post!


Try to print the data in controller before saving and check whether you are getting the Id in post or not. If you are not getting the Id in the post then model->save will insert and not update.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜