Quantcast

How can i update a field using a function

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How can i update a field using a function

Richard Neil Roque

Hi i'm a new developer using CakePHP.
I would like to know
How can i update a field using a function.
Example is

UPDATE Users SET AGE=AGE+2+3+4 WHERE id=1???

Currently i do have  a code like this one.

  $this->User->id = $user['User']['id'];
  $this->User->saveField('count_login', 'count_login+1');
  $this->User->saveField('last_login', date('Y-m-d H:i:s'));

Thanks.

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can i update a field using a function

Dr. Tarique Sani
If you construct your Data Array properly with the fields you want to
save you can directly call $this->User->save()

So you already have $user['User']['id'];

Add to it

$user['User']['count_login'] = $count_login +1;
$user['User']['last_login'] = date('Y-m-d H:i:s');

Then just save it

Read http://book.cakephp.org/view/1031/Saving-Your-Data

HTH

Tarique

On Wed, Sep 7, 2011 at 12:24 PM, Richard Neil Roque
<[hidden email]> wrote:

>
> Hi i'm a new developer using CakePHP.
> I would like to know
> How can i update a field using a function.
> Example is
>
> UPDATE Users SET AGE=AGE+2+3+4 WHERE id=1???
>
> Currently i do have  a code like this one.
>
>  $this->User->id = $user['User']['id'];
>  $this->User->saveField('count_login', 'count_login+1');
>  $this->User->saveField('last_login', date('Y-m-d H:i:s'));
>
> Thanks.
>
> --
> 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
> [hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
>



--
=============================================================
PHP for E-Biz: http://sanisoft.com
=============================================================

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can i update a field using a function

Ryan Schmidt

On Sep 7, 2011, at 02:38, Dr. Tarique Sani wrote:

> On Wed, Sep 7, 2011 at 12:24 PM, Richard Neil Roque wrote:
>>
>> Hi i'm a new developer using CakePHP.
>> I would like to know
>> How can i update a field using a function.
>> Example is
>>
>> UPDATE Users SET AGE=AGE+2+3+4 WHERE id=1???
>
> If you construct your Data Array properly with the fields you want to
> save you can directly call $this->User->save()
>
> So you already have $user['User']['id'];
>
> Add to it
>
> $user['User']['count_login'] = $count_login +1;
> $user['User']['last_login'] = date('Y-m-d H:i:s');
>
> Then just save it

No, you don't want to be doing that. That's not atomic. In the case of counting how many times a user has logged in it probably doesn't matter; a single user probably won't be simultaneously logging in from multiple places. But if you're counting things like downloads or page views, then you certainly might have multiple users downloading the same file or accessing the same page simultaneously, and with the race condition in the above code, you're going to miscount.

You really do want to be doing it the way the OP said: by running an atomic UPDATE statement that does the increment directly in the database, not in PHP code. I'm sure it's easy to do that in CakePHP but I don't have the code handy; perhaps someone else does.


--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can i update a field using a function

Richard Neil Roque
In reply to this post by Richard Neil Roque
Thanks for your reply.

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can i update a field using a function

Dr. Tarique Sani
In reply to this post by Ryan Schmidt
On Thu, Sep 8, 2011 at 6:14 AM, Ryan Schmidt <[hidden email]> wrote:
> You really do want to be doing it the way the OP said: by running an atomic UPDATE statement that does the increment directly in the database, not in PHP code. I'm sure it's easy to do that in CakePHP but I don't have the code handy; perhaps someone else does.
>

Ahem! the question was about updating fields ;-)

Tarique


--
=============================================================
PHP for E-Biz: http://sanisoft.com
=============================================================

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can i update a field using a function

phpMagpie
@Richard: I model-ise my user_visits so I know latest and previous
visits etc. and then have a CounterCache on that join.

But, this certainly works $this->ShopProduct-
>updateAll(array('ShopProduct.view_count'=>'view_count + 1'),
array('ShopProduct.id'=>$product['ShopProduct']['id']));

HTH, Paul
@phpMagpie

P.S. @Tarique, I agree Ryan did jump down your throat a bit, but it is
always a good idea to provide the most effective solution to a
question and the OP did ask how to run a function within the database
call to update a field.

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can i update a field using a function

Jonathan Meyer
In reply to this post by Richard Neil Roque
I think what you are looking for is this:


      $this->Invite->id = $data['Invite']['id'];
      $this->Invite->set(array(
        'send_count' => $this->Invite->getDataSource()->expression('send_count + 1'),     
        'sent'       => $this->Invite->getDataSource()->expression('NOW()')
      ));
      $this->Invite->save();

~Jonathan

On Wednesday, September 7, 2011 2:54:12 AM UTC-4, Richard Neil Roque wrote:

Hi i'm a new developer using CakePHP.
I would like to know
How can i update a field using a function.
Example is

UPDATE Users SET AGE=AGE+2+3+4 WHERE id=1???

Currently i do have  a code like this one.

  $this->User->id = $user['User']['id'];
  $this->User->saveField('count_login', 'count_login+1');
  $this->User->saveField('last_login', date('Y-m-d H:i:s'));

Thanks.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Loading...