views:

57

answers:

3

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.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

+1  A: 

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.

dogmatic69
you remove the relation all together from the model to check ?here is the modelhttp://bin.cakephp.org/view/1228539627
Harsha M V
not sure what you are asking or saying there
dogmatic69
A: 

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

Nik
http://harshamv.com/files/screenshots/2010-07-10_0937.pngits been set
Harsha M V
Check my edited answer
Nik
guess ur right.. the sql statment its generating is the culprit,... but still not able to make out the mistake..SQL Statementhttp://pastebin.com/EmdXmNeHRestaurant Modelhttp://pastebin.com/W1sLiL7W
Harsha M V
A: 

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.

Harsha M V