El modelo ignora la propiedad de la clave principal

EDITAR

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] => 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'
    ));

}
?>

preguntado el 28 de agosto de 11 a las 04:08

Did you recently update your model or modify the user_entity_varchars table? since cake caches your model, you can try deleting the cache and see if it works. you can find it in tmp/cache/models -

Even with clearing the cache, I have the same issue. -

It's interesting. I never found the time to run it down, but I think I've seen the same thing. Setting $this->Model->id still generated a new record while setting $this->data['Model']['id'] generated the update. I thought I was just misreading something so I went with what worked and forgot to come back to it. -

Can you post your controller code. Quick hint make sure you don't have logic that is accidentally calling $this->Model->create() -

Are you sure the model file is even loaded? Try adding a die() somewhere in there. Maybe a typo in the file name. -

2 Respuestas

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!

Respondido el 14 de Septiembre de 11 a las 20:09

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.

respondido 17 nov., 11:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.