Re: unwanted user_id increase
Looks like a documentation bug, you need to get the inserted ID before you can save the related data, so the code really should be: $this->data['Profile']['user_id'] = $user['User']['id']; Alternatively, you could use: $this->data['Profile']['user_id'] = $this->User->getLastInsertId(); Which is the way I usually do this. On Jan 15, 8:14 pm, Delirium tremens wrote: > I removed the line, but it still does not work, because user_id is > empty. > > On 16 jan, 00:51, stefano wrote: > > > why are you doing the save into User's model? remove this line > > $usersaved = $this->User->save($this->data); > > > function add() { > > if (!empty ($this->data)) { > > $this->data['Post']['user_id'] = $this->User->id; > > $postsaved = $this->User->Post->save($this->data); > > if($postsaved) { > > $this->flash('The post has been saved.', '/posts'); > > } > > } > > > } > > On Thu, Jan 15, 2009 at 11:45 PM, Delirium tremens > > wrote: > > > > I have a User hasMany Post and a Post belongs to User. The function to > > > add a post with an user id is inserting a user with null email and > > > null password into the users table and a post with an user id > > > increased by one into the posts table. Take a look... > > > > function add() { > > > if (!empty ($this->data)) { > > > $usersaved = $this->User->save($this->data); > > > $this->data['Post']['user_id'] = $this->User->id; > > > $postsaved = $this->User->Post->save($this->data); > > > if($postsaved && $usersaved) { > > > $this->flash('The post has been saved.', '/posts'); > > > } > > > } > > > } > > > > How to solve this problem? > > > -- > > Stefano Salvatori M.http://stefano.salvatori.cl/ > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: unwanted user_id increase
I removed the line, but it still does not work, because user_id is empty. On 16 jan, 00:51, stefano wrote: > why are you doing the save into User's model? remove this line > $usersaved = $this->User->save($this->data); > > function add() { > if (!empty ($this->data)) { > $this->data['Post']['user_id'] = $this->User->id; > $postsaved = $this->User->Post->save($this->data); > if($postsaved) { > $this->flash('The post has been saved.', '/posts'); > } > } > > > > } > On Thu, Jan 15, 2009 at 11:45 PM, Delirium tremens wrote: > > > I have a User hasMany Post and a Post belongs to User. The function to > > add a post with an user id is inserting a user with null email and > > null password into the users table and a post with an user id > > increased by one into the posts table. Take a look... > > > function add() { > > if (!empty ($this->data)) { > > $usersaved = $this->User->save($this->data); > > $this->data['Post']['user_id'] = $this->User->id; > > $postsaved = $this->User->Post->save($this->data); > > if($postsaved && $usersaved) { > > $this->flash('The post has been saved.', '/posts'); > > } > > } > > } > > > How to solve this problem? > > -- > Stefano Salvatori M.http://stefano.salvatori.cl/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: unwanted user_id increase
The order is actually: 1. $this->User->save($this->data); 2. $this->data['Profile']['user_id'] = $this->User->id; 3. $this->User->Profile->save($this->data); On 16 jan, 01:03, Delirium tremens wrote: > On 16 jan, 00:51, stefano wrote: > > > why are you doing the save into User's model? > > http://book.cakephp.org/view/84/Saving-Related-Model-Data-hasOne-hasM... > > The add example on top of that page uses... > > 1. $this->User->Profile->save($this->data); > 2. $this->data['Profile']['user_id'] = $this->User->id; > 3. $this->User->save($this->data); > > If I'm using User hasMany Post and Post belongsTo User, why does the > example need the three lines, but I don't? > > > remove this line > > $usersaved = $this->User->save($this->data); > > > function add() { > > if (!empty ($this->data)) { > > $this->data['Post']['user_id'] = $this->User->id; > > $postsaved = $this->User->Post->save($this->data); > > if($postsaved) { > > $this->flash('The post has been saved.', '/posts'); > > } > > } > > > } > > On Thu, Jan 15, 2009 at 11:45 PM, Delirium tremens > > wrote: > > > > I have a User hasMany Post and a Post belongs to User. The function to > > > add a post with an user id is inserting a user with null email and > > > null password into the users table and a post with an user id > > > increased by one into the posts table. Take a look... > > > > function add() { > > > if (!empty ($this->data)) { > > > $usersaved = $this->User->save($this->data); > > > $this->data['Post']['user_id'] = $this->User->id; > > > $postsaved = $this->User->Post->save($this->data); > > > if($postsaved && $usersaved) { > > > $this->flash('The post has been saved.', '/posts'); > > > } > > > } > > > } > > > > How to solve this problem? > > > -- > > Stefano Salvatori M.http://stefano.salvatori.cl/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: unwanted user_id increase
On 16 jan, 00:51, stefano wrote: > why are you doing the save into User's model? http://book.cakephp.org/view/84/Saving-Related-Model-Data-hasOne-hasMany-belongsTo The add example on top of that page uses... 1. $this->User->Profile->save($this->data); 2. $this->data['Profile']['user_id'] = $this->User->id; 3. $this->User->save($this->data); If I'm using User hasMany Post and Post belongsTo User, why does the example need the three lines, but I don't? > remove this line > $usersaved = $this->User->save($this->data); > > function add() { > if (!empty ($this->data)) { > $this->data['Post']['user_id'] = $this->User->id; > $postsaved = $this->User->Post->save($this->data); > if($postsaved) { > $this->flash('The post has been saved.', '/posts'); > } > } > > > > } > On Thu, Jan 15, 2009 at 11:45 PM, Delirium tremens wrote: > > > I have a User hasMany Post and a Post belongs to User. The function to > > add a post with an user id is inserting a user with null email and > > null password into the users table and a post with an user id > > increased by one into the posts table. Take a look... > > > function add() { > > if (!empty ($this->data)) { > > $usersaved = $this->User->save($this->data); > > $this->data['Post']['user_id'] = $this->User->id; > > $postsaved = $this->User->Post->save($this->data); > > if($postsaved && $usersaved) { > > $this->flash('The post has been saved.', '/posts'); > > } > > } > > } > > > How to solve this problem? > > -- > Stefano Salvatori M.http://stefano.salvatori.cl/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: unwanted user_id increase
why are you doing the save into User's model? remove this line $usersaved = $this->User->save($this->data); function add() { if (!empty ($this->data)) { $this->data['Post']['user_id'] = $this->User->id; $postsaved = $this->User->Post->save($this->data); if($postsaved) { $this->flash('The post has been saved.', '/posts'); } } } On Thu, Jan 15, 2009 at 11:45 PM, Delirium tremens wrote: > > I have a User hasMany Post and a Post belongs to User. The function to > add a post with an user id is inserting a user with null email and > null password into the users table and a post with an user id > increased by one into the posts table. Take a look... > > function add() { >if (!empty ($this->data)) { >$usersaved = $this->User->save($this->data); >$this->data['Post']['user_id'] = $this->User->id; >$postsaved = $this->User->Post->save($this->data); >if($postsaved && $usersaved) { >$this->flash('The post has been saved.', '/posts'); >} >} > } > > How to solve this problem? > > > -- Stefano Salvatori M. http://stefano.salvatori.cl/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
unwanted user_id increase
I have a User hasMany Post and a Post belongs to User. The function to add a post with an user id is inserting a user with null email and null password into the users table and a post with an user id increased by one into the posts table. Take a look... function add() { if (!empty ($this->data)) { $usersaved = $this->User->save($this->data); $this->data['Post']['user_id'] = $this->User->id; $postsaved = $this->User->Post->save($this->data); if($postsaved && $usersaved) { $this->flash('The post has been saved.', '/posts'); } } } How to solve this problem? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---