Answering my earlier question...
To update set a particular field value across all records:
$this->ExampleModel->updateAll(array('`ExampleModel`.`example_field`'
=> 'example_value'), 1);
SQL equivalent:
UPDATE `example_table` AS `ExampleModel` SET
`ExampleModel`.`example_field` = 'example_value' WHERE 1
On Oct 6, 5:38 pm, "Ryan McKillen" <[EMAIL PROTECTED]> wrote:
> Once I get it down, I'll be glad to.
>
> On Mon, Oct 6, 2008 at 4:45 PM, teknoid <[EMAIL PROTECTED]> wrote:
>
> > And I will log a vote, that once you get it figured out by looking at
> > test cases, you add the required samples to the book (using the 'edit'
> > feature) :)
>
> > On Oct 6, 2:00 pm, "Ryan McKillen" <[EMAIL PROTECTED]> wrote:
> > > Martin, thanks. That was a big help. I was working inside a foreach loop,
> > so
> > > I imagine you saved me a big headache with the create() tip. This helped
> > out
> > > on updating an individual record, but what about changing that field
> > value
> > > to 0 for all records?
>
> > > teknoid, thanks as well for the link. I'll spend some time in there, but
> > > will still log a vote for a set of examples in the Cookbook.
>
> > > On Mon, Oct 6, 2008 at 1:33 PM, [EMAIL PROTECTED] <
>
> > > [EMAIL PROTECTED]> wrote:
>
> > > > Hi Ryan,
>
> > > > 1: Not that I am aware of. It is a very good idea for a page in the
> > > > cookbook, though.
>
> > > > 2: This is usually very simple. Sometimes you have to watch out
> > > > though.
> > > > Usually this is totally automatic.
>
> > > > $one = $this->ExampleModel->findById($id);
> > > > $one['ExampleModel']['example_field'] = 0;
> > > > $this->ExampleModel->save($one);
>
> > > > Cake will know to use update in most cases like in the example above.
> > > > Sometimes, though you will have to help out a little. Like when you
> > > > are looping a number of records and saving each in turn, you must call
> > > > create before each save to avoid saving over the same record each
> > > > time.
>
> > > > There is also sometimes call for these drastic measuers.
> > > > You may not always be sure wether insert of create should be used:
>
> > > > $one = $this->ExampleModel->findById($id);
>
> > > > if ( empty($one) ) {
> > > > $one = array('ExampleModel'=>array());
> > > > }
>
> > > > $one['ExampleModel']['example_field'] = 0;
>
> > > > $this->ExampleModel->create();
> > > > $this->ExampleModel->set($one);
> > > > $this->ExampleModel->save($one);
>
> > > > That is, I think, it. Some people prefer setting the id of a Model
> > > > before using set(). I have seen tests in the core using both methods
> > > > so I guess both work fine. When I have done similar saves I have not
> > > > found it necessary setting Model->id before using set(). It may depend
> > > > on circumstances I am not fully aware of.
>
> > > > /Martin
>
> > > > On Oct 6, 6:51 pm, Ryan <[EMAIL PROTECTED]> wrote:
> > > > > As a new Cake user, I am having a bit of trouble transitioning from
> > > > > writing my own MySQL queries, to letting Cake do the heavy lifting
> > for
> > > > > me. I think I have SELECT and INSERT down, but am still struggling
> > > > > with UPDATE. Two questions:
>
> > > > > 1.) Is there a good resource I can refer to that shows examples of
> > raw
> > > > > SQL with the cake equivalent? This would really help my learning.
>
> > > > > 2.) The specific issue I am having that prompted this thread... I've
> > > > > got a primary key id number for a record in a table, and for that
> > > > > record, I want to update change the value of a given field from 1 to
> > > > > 0. How can I perform that simple operation with a Cake query?
>
> > > > > Thanks
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---