Re: Updating a single database rows field
So really weird. This was my previous action in the controller. function verify($hash = NULL) { } That would fail the saveField(), and the saveField would try to do an insert. It would also grab the wrong id for the User model. But once I changed it to the following it works fine. function verify($hash = '') { } Is there a reason why that fails? Is it in the docs, or is it a bug? --~--~-~--~~~---~--~~ 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: Updating a single database rows field
I even moved all the code into the Model, did what you said, and its still doing an insert instead of an update. Heres the method I call in the controller: function verifyEmailExpiration($username, $password, $hash, $expHours) { $userObj = $this->find('first', array( 'conditions' => array( 'User.username' => $username, 'User.password' => $password, 'User.hash' => $hash ), 'fields' => array('User.id', 'User.signupDate', 'User.status') )); if (!empty($userObj)) { $this->create(); $this->id = $userObj['User']['id']; // Get timeframe $signupTime = strtotime('+'. $expHours .' days', $userObj['User'] ['signupDate']); $currentTime = time(); if ($userObj['User']['status'] == 'active') { $this->invalidate('', 'alreadyVerified'); return false; } else if ($currentTime < $signupTime) { $this->saveField('status', 'active'); return true; } else { $this->saveField('status', 'inactive'); $this->invalidate('', 'verifyTimeElapsed', array($expHours)); return false; } } else { $this->invalidate('', 'verifyFailure'); } return false; } Also I thought models by default didnt inherit the locale stuff, is the app_model an exception? --~--~-~--~~~---~--~~ 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: Updating a single database rows field
On Dec 18, 12:44 pm, gearvOsh wrote: > Like I said, I dont want to Import the locale in every model method. Hint: you don't need to, and it doesn't say you should. > > Also saveField fails at this point: > > if (!empty($this->id)) { > if (!$db->update($this, $fields, $values)) { > $success = false; > } > > } > > Because of this, When I know for sure the row exists. > > if (!$this->__exists && $count > 0) { > $this->id = false; > > } > > Which fails because it runs this query: > > SELECT COUNT(*) AS `count` FROM `users` AS `User` WHERE `User`.`id` = > 'eac20adf536b50f9fc0be58550fde763' That either means you're looking at a wrong/irrelevant sql statement or you are not calling create as you said above https://trac.cakephp.org/browser/branches/1.2.x.x/cake/libs/model/model.php#L968 > > Because for some reason the id is being pulled from the controller > action argument, which makes no god damn sense. That's the problem of showing a 'window' view of your code instead of the method in its enirety. The following code snippet would generate an update: $this->User->create(); $this->User->id = $this->User->field('id'); // find a random user $this->User->saveField('field', 'value'); die; irrespective of where you put it - try based on that example. AD --~--~-~--~~~---~--~~ 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: Updating a single database rows field
Like I said, I dont want to Import the locale in every model method. Also saveField fails at this point: if (!empty($this->id)) { if (!$db->update($this, $fields, $values)) { $success = false; } } Because of this, When I know for sure the row exists. if (!$this->__exists && $count > 0) { $this->id = false; } Which fails because it runs this query: SELECT COUNT(*) AS `count` FROM `users` AS `User` WHERE `User`.`id` = 'eac20adf536b50f9fc0be58550fde763' Because for some reason the id is being pulled from the controller action argument, which makes no god damn sense. eac20adf536b50f9fc0be58550fde763 = $hash in verify($hash) { http://cake/users/verify/eac20adf536b50f9fc0be58550fde763 I echo the id before $this->exists() and its correct. But when I echo the id after that is called, it is wrong. --~--~-~--~~~---~--~~ 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: Updating a single database rows field
On Dec 18, 11:57 am, gearvOsh wrote: > I cant use i18n/l10n in models now can I. yes you can. start here: http://book.cakephp.org/view/163/Internationalization-in-CakePHP or use your best friend ;) > Please show me how I would > add errors to the $this->validationErrors array with a locale. If > something works, who cares where it belongs. My name is not google, anyway have fun. AD --~--~-~--~~~---~--~~ 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: Updating a single database rows field
Also I wish to not import and initiate a locale class in the model everytime I need it, which would be most of the time. --~--~-~--~~~---~--~~ 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: Updating a single database rows field
I cant use i18n/l10n in models now can I. Please show me how I would add errors to the $this->validationErrors array with a locale. If something works, who cares where it belongs. @grigri - I did updateAll, I just dislike how it doesnt escape and quote the data. --~--~-~--~~~---~--~~ 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: Updating a single database rows field
On Dec 18, 8:12 am, gearvOsh wrote: > I still have yet to find a straight forward way to do this, so ill > simply do something like this: > > function update($user_id, $fields) { > if (is_array($fields)) { > App::import('Sanitize'); > Sanitize::clean($fields); > > $cleanFields = array(); > foreach ($fields as $field => $value) { > $cleanFields[] = "User.". $field ." = '". $value ."'"; > } > > $sql = "UPDATE users AS User SET ". implode(', ', > $cleanFields) ." > WHERE User.id = ". Sanitize::escape($user_id) ." LIMIT 1"; > return $this->query($sql); > } > > return NULL; > > } What the hell is that :D? FIND the reason your save is failing, that's even worse that putting model logic in a controller. If nothing else debug the save method and see where it returns false. It's always amusing to see the hoops people jump through rather than debug/investigate the core - it is afer all 'just' PHP. AD --~--~-~--~~~---~--~~ 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: Updating a single database rows field
On Dec 17, 10:05 pm, gearvOsh wrote: > Oh but the extra code is for extra form validation on my end. Model logic. > Using > invalidate() adds errors to the array that I can then loop through and > display in the view (I dislike having the error show up after the > input). > So don't use the input-errors - loop on $this->validationErrors. IMO there is no 'excuse' for doing what you're doing - it's model logic. AD --~--~-~--~~~---~--~~ 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: Updating a single database rows field
What about updateAll() ? $this->User->updateAll( array('User.status' => 'active'), array('User.id' => 1) ); Seems pretty straightforward to me... On Dec 18, 7:12 am, gearvOsh wrote: > I still have yet to find a straight forward way to do this, so ill > simply do something like this: > > function update($user_id, $fields) { > if (is_array($fields)) { > App::import('Sanitize'); > Sanitize::clean($fields); > > $cleanFields = array(); > foreach ($fields as $field => $value) { > $cleanFields[] = "User.". $field ." = '". $value ."'"; > } > > $sql = "UPDATE users AS User SET ". implode(', ', > $cleanFields) ." > WHERE User.id = ". Sanitize::escape($user_id) ." LIMIT 1"; > return $this->query($sql); > } > > return NULL; > > } --~--~-~--~~~---~--~~ 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: Updating a single database rows field
I still have yet to find a straight forward way to do this, so ill simply do something like this: function update($user_id, $fields) { if (is_array($fields)) { App::import('Sanitize'); Sanitize::clean($fields); $cleanFields = array(); foreach ($fields as $field => $value) { $cleanFields[] = "User.". $field ." = '". $value ."'"; } $sql = "UPDATE users AS User SET ". implode(', ', $cleanFields) ." WHERE User.id = ". Sanitize::escape($user_id) ." LIMIT 1"; return $this->query($sql); } return NULL; } --~--~-~--~~~---~--~~ 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: Updating a single database rows field
Oh but the extra code is for extra form validation on my end. Using invalidate() adds errors to the array that I can then loop through and display in the view (I dislike having the error show up after the input). On Dec 17, 3:24 am, AD7six wrote: > On Dec 17, 9:56 am, gearvOsh wrote: > > > Nope, still doesn't work. This seems like a lot of hassle/steps in > > between just to do something simple like update. > > That's a lot of fat controller code for something as simple as that > too ;). e.g. You should almost never need to do this in a controller > "$this->User->invalidate..". > > Of the snippet you pasted, your controller code should be *no more* > than > > function xyz($a, $param) { > if ($this->data) { > .. > if ($this->User->save($data, extra, params)) { > $this->Session->setFlash('success'); > $this->redirect('/wherever'); > > } > .. > } > > AD > > > > > Heres a snippet of my code: > > > if ($this->User->validates()) { > > $userObj = $this->User->find('first', array( > > 'conditions' => array( > > 'User.username' => $this->data['User']['username'], > > 'User.password' => $this->data['User']['password'], > > 'User.hash' => $hash > > ), > > 'fields' => array('User.id', 'User.signupDate') > > )); > > > if (!empty($userObj)) { > > $this->User->create(); > > $this->User->id = $userObj['User']['id']; > > > if ($this->User->verifyEmail($userObj['User']['signupDate'], > > $this->Settings->grab('email_verify_exp_hours'))) { > > $this->User->saveField('status', 'active'); > > } else { > > $this->User->invalidate('', sprintf(__d('errors', > > 'verifyTimeElapsed', true), $this->Settings->grab > > ('email_verify_exp_hours'))); > > $this->User->saveField('status', 'inactive'); > > } > > } else { > > $this->User->invalidate('', __d('errors', 'verifyFailure', > > true)); > > } > > > } --~--~-~--~~~---~--~~ 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: Updating a single database rows field
On Dec 17, 9:56 am, gearvOsh wrote: > Nope, still doesn't work. This seems like a lot of hassle/steps in > between just to do something simple like update. That's a lot of fat controller code for something as simple as that too ;). e.g. You should almost never need to do this in a controller "$this->User->invalidate..". Of the snippet you pasted, your controller code should be *no more* than function xyz($a, $param) { if ($this->data) { .. if ($this->User->save($data, extra, params)) { $this->Session->setFlash('success'); $this->redirect('/wherever'); } .. } AD > > Heres a snippet of my code: > > if ($this->User->validates()) { > $userObj = $this->User->find('first', array( > 'conditions' => array( > 'User.username' => $this->data['User']['username'], > 'User.password' => $this->data['User']['password'], > 'User.hash' => $hash > ), > 'fields' => array('User.id', 'User.signupDate') > )); > > if (!empty($userObj)) { > $this->User->create(); > $this->User->id = $userObj['User']['id']; > > if ($this->User->verifyEmail($userObj['User']['signupDate'], > $this->Settings->grab('email_verify_exp_hours'))) { > $this->User->saveField('status', 'active'); > } else { > $this->User->invalidate('', sprintf(__d('errors', > 'verifyTimeElapsed', true), $this->Settings->grab > ('email_verify_exp_hours'))); > $this->User->saveField('status', 'inactive'); > } > } else { > $this->User->invalidate('', __d('errors', 'verifyFailure', > true)); > } > > } --~--~-~--~~~---~--~~ 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: Updating a single database rows field
Hi, I had a few suggestions, but I should state that I have at times found this simple task of saving data a bit confusing... these are just suggestions. First create or not? saveField() calls create internally. Your create should not do much one way or the other. See: https://trac.cakephp.org/browser/branches/1.2.x.x/cake/libs/model/model.php#L1069 The manual states that you should set the id "just before" calling saveField. One thing to note from that your sequence goes: set the id call verifyEmail (call invalidate) call saveField Could Cake possibly do some "clean up" for you in the verification code or invalidate? Try moving the line setting id down to "just before" saveField. Two copies is less dry but give it a try. One this that has saved me a few times is read(). It costs you an extra query to the db but I have found that it helps the "id" stick around and not magically disappear. (This is one of those thing I have found confusing) Also, sometimes set() is not at all the same as just assigning the id a value. $this->User->id = $userObj['User']['id']; $this->User->set($this->User->primaryKey, $userObj['User']['id']); I have so far stayed away from set as much as possible... because it confuses me sometimes. :) But, that said, I have seen code that have made others happy (in tests) by using set like this: $this-> User->id = 1; $this-> User->set('name', 'Martin'); $this-> User->save(); Since I haven't had your specific problem I can't know which of these, if any, is likely to help you. Good luck. /Martin On Dec 17, 10:27 am, "Amit Badkas" wrote: > It's not necessary that you must call create() method before inserting > record. This method is used to reset model parameters like id, validation > errors > etc.,https://trac.cakephp.org/browser/branches/1.2.x.x/cake/libs/model/mod... > > 2008/12/17 gearvOsh > > > > > I was only doing what the other guy suggested, I usually do not have > > the create() method. > > -- > Amit > > http://amitrb.wordpress.com/http://coppermine-gallery.net/http://cheesecake-photoblog.org/http://www.sanisoft.com/blog/author/amitbadkas --~--~-~--~~~---~--~~ 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: Updating a single database rows field
It's not necessary that you must call create() method before inserting record. This method is used to reset model parameters like id, validation errors etc., https://trac.cakephp.org/browser/branches/1.2.x.x/cake/libs/model/model.php#L968 2008/12/17 gearvOsh > > I was only doing what the other guy suggested, I usually do not have > the create() method. > > > -- Amit http://amitrb.wordpress.com/ http://coppermine-gallery.net/ http://cheesecake-photoblog.org/ http://www.sanisoft.com/blog/author/amitbadkas --~--~-~--~~~---~--~~ 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: Updating a single database rows field
I was only doing what the other guy suggested, I usually do not have the create() method. --~--~-~--~~~---~--~~ 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: Updating a single database rows field
You are calling $this->User->create(); signifying that you want to create a new field not update. On Dec 17, 12:56 am, gearvOsh wrote: > Nope, still doesn't work. This seems like a lot of hassle/steps in > between just to do something simple like update. > > Heres a snippet of my code: > > if ($this->User->validates()) { > $userObj = $this->User->find('first', array( > 'conditions' => array( > 'User.username' => $this->data['User']['username'], > 'User.password' => $this->data['User']['password'], > 'User.hash' => $hash > ), > 'fields' => array('User.id', 'User.signupDate') > )); > > if (!empty($userObj)) { > $this->User->create(); > $this->User->id = $userObj['User']['id']; > > if ($this->User->verifyEmail($userObj['User']['signupDate'], > $this->Settings->grab('email_verify_exp_hours'))) { > $this->User->saveField('status', 'active'); > } else { > $this->User->invalidate('', sprintf(__d('errors', > 'verifyTimeElapsed', true), $this->Settings->grab > ('email_verify_exp_hours'))); > $this->User->saveField('status', 'inactive'); > } > } else { > $this->User->invalidate('', __d('errors', 'verifyFailure', > true)); > } > > } --~--~-~--~~~---~--~~ 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: Updating a single database rows field
Nope, still doesn't work. This seems like a lot of hassle/steps in between just to do something simple like update. Heres a snippet of my code: if ($this->User->validates()) { $userObj = $this->User->find('first', array( 'conditions' => array( 'User.username' => $this->data['User']['username'], 'User.password' => $this->data['User']['password'], 'User.hash' => $hash ), 'fields' => array('User.id', 'User.signupDate') )); if (!empty($userObj)) { $this->User->create(); $this->User->id = $userObj['User']['id']; if ($this->User->verifyEmail($userObj['User']['signupDate'], $this->Settings->grab('email_verify_exp_hours'))) { $this->User->saveField('status', 'active'); } else { $this->User->invalidate('', sprintf(__d('errors', 'verifyTimeElapsed', true), $this->Settings->grab ('email_verify_exp_hours'))); $this->User->saveField('status', 'inactive'); } } else { $this->User->invalidate('', __d('errors', 'verifyFailure', true)); } } --~--~-~--~~~---~--~~ 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: Updating a single database rows field
Please try once again by calling *$this->User->create()* before setting id 2008/12/17 gearvOsh > > Tried it again that way and heres the error: > > Warning (512): SQL Error: 1062: Duplicate entry '' for key 2 [CORE\cake > \libs\model\datasources\dbo_source.php, line 521] > Query: INSERT INTO `users` (`status`) VALUES ('inactive') > > It keeps inserting, even if I set the id. > > > -- Amit http://amitrb.wordpress.com/ http://coppermine-gallery.net/ http://cheesecake-photoblog.org/ http://www.sanisoft.com/blog/author/amitbadkas --~--~-~--~~~---~--~~ 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: Updating a single database rows field
Tried it again that way and heres the error: Warning (512): SQL Error: 1062: Duplicate entry '' for key 2 [CORE\cake \libs\model\datasources\dbo_source.php, line 521] Query: INSERT INTO `users` (`status`) VALUES ('inactive') It keeps inserting, even if I set the id. --~--~-~--~~~---~--~~ 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: Updating a single database rows field
Ive done that also, it still inserted a new row. --~--~-~--~~~---~--~~ 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: Updating a single database rows field
You need to set user's id before running saveField() like *$this->User->id = 1*, then you need to call *$this->User->saveField('status', 'active')*. Hope this helps 2008/12/17 gearvOsh > > Ok, I may be stupid... but how would I update a single field on a > single row. Something like the following without having to write a > custom query. > > UPDATE users SET status = 'active' WHERE id = 1 > > Ive tried doing saveField() but that inserts a new row. Ive also tried > using updateAll() but that doesnt clean/escape the data correctly. Ive > also tried $this->Model->set() and then doing a save but also no. Any > ideas? > > > -- Amit http://amitrb.wordpress.com/ http://coppermine-gallery.net/ http://cheesecake-photoblog.org/ http://www.sanisoft.com/blog/author/amitbadkas --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---