Hi folks,
i have a problem with a HABTM save.
2 models :
Song
class Song extends AppModel {
var $name = 'Song';
var $hasAndBelongsToMany = Array('Genre');
}
-----------------------------------------------------------------
class Genre extends AppModel {
var $name = 'Genre';
var $hasAndBelongsToMany = Array('Song');
}
-----------------------------------------------------------------
For editing the genres of a song I dont use lists, instead I use a lot
of input boxes with autocompleter.
The user start the edit of an input box and a new empty box shows up
bellow the last one.
When i edit i load the genres values into the inputs.
The problem is :
I empty the last field (no matter the value) and i post the data.
#debug($this->data['Genre']);
Array
(
[0] => Rock
[1] => Pop
[2] =>
[3] =>
)
Item[2] is the input box that I emptied. Item[3] is the empty input
box the permit to insert new genres. The last one It's always empty.
I prepare $data for saving. I find genres like the names, if i found i
fill up the Genre array, else i create the genre and i put it in the
array (this it's not the case)
#debug($GenreData);
Array
(
[Song] => Array
(
[id] => 27
)
[Genre] => Array
(
[0] => 3 //Comment : It's Rock
[1] => 5 //Comment : It's Pop
)
)
I save with : $this->Song->save($GenreData);
There are only 2 Genres. But i find in queries :
DELETE `GenresSong` FROM `genres_songs` AS `GenresSong` WHERE
`GenresSong`.`song_id` = 27 AND `GenresSong`.`genre_id` IN (3, 5, 7) //
Correct it empty all associations
SELECT COUNT(*) AS `count` FROM `genres_songs` AS `GenresSong` WHERE
`GenresSong`.`id` = 180 //Count something (180 is the id of my emptied
field, it was 'Country').
INSERT INTO `genres_songs` (`id`, `song_id`, `genre_id`) VALUES (180,
27, 7) //Re-insert the same last record
INSERT INTO `genres_songs` (`song_id`,`genre_id`) VALUES (27,'3'),
(27,'5') //Insert the 2 record.
Why Why Why it insert a ghost record ?
What I do wrong ?
Thanks in advice
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---