开发者

Duplicate entry when trying to edit a record

开发者 https://www.devze.com 2023-01-06 02:46 出处:网络
When i try to a edit a entry.. its creating a duplicate entry in the database. i have made sure i add an hidden id field when i go to the edit form.

When i try to a edit a entry.. its creating a duplicate entry in the database.

i have made sure i add an hidden id field when i go to the edit form.

Restaurant

RestaurantAttribute

RestaurantContact

these are the models i am using.

tried reinitialize the id

$this->Restaurant->id = $this->Restaurant->id;

strange... in the db the extra entry is not there...

but in cake its displaying..tried to clear the cache.. still happening

http://harshamv.com/files/screenshots/2010-07-07_1524.png

http://harshamv.com/files/screenshots/2010-07-07_1525.png

i have posted the code here

http://forum.开发者_JAVA技巧phpvideotutorials.com/showthread.php?p=81936

here is the index action http://bin.cakephp.org/view/660872993

here is the model http://bin.cakephp.org/view/1228539627

http://harshamv.com/files/screenshots/2010-07-10_0937.png

SQL Statement http://pastebin.com/EmdXmNeH

Restaurant Model http://pastebin.com/W1sLiL7W


If there is only one record showing in phpmyadmin and multiple showing in the app then you have a problem with your relations.

I would start by removing hasOne and belongsTo and see. I have had this before, and it was always a bad relation.


I got the answer from teknoid from the MIRC.

In my edit view i have only set the Restaurant.id. none of the ids of the related models are set in the view.

By cake's convention we need to have the id of any record from the db u update. so this was creating a duplicate record in the related models.

In the index action since there is a JOIN of related models. it was creating duplicate entries because of repeated entries in the related model

by adding the ids of all the related model records. this solved the problem.

Thanks all for your time. Finally i managed to get it working.


Check if the ID column is set to be primary key as well as AUTOINCREMENT. Duplicated entries cannot be inserted if this is set.

This

$this->Restaurant->id = $this->Restaurant->id; 

doing noting.

Edit:

Ok, my fault that I haven't seen that you said that in the DB entries are ok. Then:

  1. Check if $this->paginate() return these duplicated entries - print_r() it. If the entries are duplicated there, then you are messing the relations in the models. Also change the debug option to 2 and see what SQLs are generated.

  2. If the entries are unique in the check above, then you are messing something in the loop when displaying the results of $restaurants

I think that it's more likely the first suggestion

0

精彩评论

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

关注公众号