Monday, March 28, 2011

Re: issue with saving data in model

There's no mystery (especially if you read the guide). As I said, if you don't include the create statement you are not creating a new record (the value of $this->id hasn't changed) therefore you stay on the first record you create or navigate to and only ever update.

Jeremy Burns
Class Outfit

jeremyburns@classoutfit.com
http://www.classoutfit.com

On 28 Mar 2011, at 18:42, Rishab Jain wrote:

> Thanks Jeremy. It worked.
>
> If possible, do feed in your 2 cents on how this worked.
>
> regards,
> Rishab
>
> On 3/28/11, Jeremy Burns | Class Outfit <jeremyburns@classoutfit.com> wrote:
>> Add $this->create(); to your loop before you do the save. If not, you are
>> just updating the same record.
>>
>> Jeremy Burns
>> Class Outfit
>>
>> jeremyburns@classoutfit.com
>> http://www.classoutfit.com
>>
>> On 28 Mar 2011, at 16:21, Rishab Jain wrote:
>>
>>> Hi All,
>>>
>>> I'm facing a very weird problem. I'm trying to save data in a loop
>>> where 1 person is sending msg to many people.. Here's the print_r of
>>> the 3 arrays before I call the 'save' function in the model.
>>>
>>> Array
>>> (
>>> [Message] => Array
>>> (
>>> [sender_uid_fk_users] => 1
>>> [subject] => hey
>>> [body] => hey
>>> [recipient_uid_fk_users] => 2
>>> [date] => 2011:03:28 10:52:41
>>> )
>>>
>>> )
>>> Array
>>> (
>>> [Message] => Array
>>> (
>>> [sender_uid_fk_users] => 1
>>> [subject] => hey
>>> [body] => hey
>>> [recipient_uid_fk_users] => 4
>>> [date] => 2011:03:28 10:52:41
>>> )
>>>
>>> )
>>> Array
>>> (
>>> [Message] => Array
>>> (
>>> [sender_uid_fk_users] => 1
>>> [subject] => hey
>>> [body] => hey
>>> [recipient_uid_fk_users] => 6
>>> [date] => 2011:03:28 10:52:41
>>> )
>>>
>>> )
>>>
>>> The only difference here is the different 'recipient_uid_fk_users' in
>>> all the 3 arrays with values '2', '4', and, '6'.
>>>
>>> All I'm doing then is,
>>> foreach(recipient...)
>>> {
>>> /// prepare $arrayMessagesInfo /////
>>> $this->save($arrayMessagesInfo);
>>> }
>>>
>>> The issue I'm facing is that only the first value gets inserted, and
>>> the rest of the two queries becomes 'update' queries. I'm not even
>>> mentioning the primary key in the array.
>>>
>>> These are the cakephp debugging logs:
>>>
>>> 4 INSERT INTO `messages` (`sender_uid_fk_users`, `subject`,
>>> `body`, `recipient_uid_fk_users`, `date`) VALUES (1, 'hey', 'hey', 2,
>>> '2011:03:28 10:48:33') 1 0
>>> 5 SELECT LAST_INSERT_ID() AS insertID 1 1 0
>>> 6 SELECT COUNT(*) AS `count` FROM `messages` AS `Message` WHERE
>>> `Message`.`id` = 3 1 1 0
>>> 7 SELECT COUNT(*) AS `count` FROM `messages` AS `Message` WHERE
>>> `Message`.`id` = 3 1 1 0
>>> 8 UPDATE `messages` SET `sender_uid_fk_users` = 1, `subject` =
>>> 'hey', `body` = 'hey', `recipient_uid_fk_users` = 4, `date` =
>>> '2011:03:28 10:48:33' WHERE `messages`.`id` = 3 1
>>> 0
>>> 9 SELECT COUNT(*) AS `count` FROM `messages` AS `Message` WHERE
>>> `Message`.`id` = 3 1 1 0
>>> 10 SELECT COUNT(*) AS `count` FROM `messages` AS `Message` WHERE
>>> `Message`.`id` = 3 1 1 0
>>> 11 UPDATE `messages` SET `sender_uid_fk_users` = 1, `subject` =
>>> 'hey', `body` = 'hey', `recipient_uid_fk_users` = 6, `date` =
>>> '2011:03:28 10:48:33' WHERE `messages`.`id` = 3
>>>
>>> Here's the table structure:
>>> CREATE TABLE `messages` (
>>> `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
>>> `subject` varchar(100) NOT NULL,
>>> `body` varchar(500) NOT NULL,
>>> `sender_uid_fk_users` int(10) unsigned NOT NULL,
>>> `date` datetime NOT NULL,
>>> `recipient_uid_fk_users` int(10) unsigned NOT NULL,
>>> PRIMARY KEY (`id`),
>>> KEY `sender_uid_fk_users` (`sender_uid_fk_users`),
>>> KEY `recipient_uid_fk_users` (`recipient_uid_fk_users`),
>>> ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
>>>
>>>
>>> It would be great if anybody can help me out here.
>>>
>>> regards,
>>> Rishab
>>>
>>> --
>>> Our newest site for the community: CakePHP Video Tutorials
>>> http://tv.cakephp.org
>>> Check out the new CakePHP Questions site http://ask.cakephp.org and help
>>> others with their CakePHP related questions.
>>>
>>>
>>> To unsubscribe from this group, send email to
>>> cake-php+unsubscribe@googlegroups.com For more options, visit this group
>>> at http://groups.google.com/group/cake-php
>>
>> --
>> Our newest site for the community: CakePHP Video Tutorials
>> http://tv.cakephp.org
>> Check out the new CakePHP Questions site http://ask.cakephp.org and help
>> others with their CakePHP related questions.
>>
>>
>> To unsubscribe from this group, send email to
>> cake-php+unsubscribe@googlegroups.com For more options, visit this group at
>> http://groups.google.com/group/cake-php
>>
>
> --
> Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: