Monday, September 27, 2010

Re: editing rows from index.cttp

You should add the action to the create() line. And that should come
before your foreach() loop. Also, include a hidden element for the
record's id at each iteration (where you echo 'ad_size') or the form
will have nothing to submit.

But saving multiple records with Cake has always been a bit confusing
for me. I'm really not clear on what the "best practices" are as the
documentation is muddled and apparently incomplete. But there are
several ways to do it. I've successfully put something together
following the approach taken here:

http://mrphp.com.au/code/update-multiple-records-using-multi-actions-cakephp

echo $this->Form->create('Advert', array('admin' => 1, 'action' => 'foo'));

...

foreach($data as $d)
{
...

echo $this->Form->input(
$d['Advert']['id'],
array(
'type' => 'checkbox',
'label'=>false,
'div' => false,
'name'=> "record[{$d['Advert']['id']}]",
'id'=>'listRecord_'.$d['Advert']['id']
)
);

}

echo $this->Form->end('update all');

In the action, put: die(debug($this->params['form'])); Because of the
way the inputs have been created, you won't get the normal $this->data
array. You should get something along these lines:

Array
(
[record] => Array
(
[1] => 1
[2] => 0
[3] => 0
[4] => 1
[5] => 0
[6] => 1
[7] => 1
[8] => 1
[9] => 0
[10] => 0
)
)

So, you can then use Set::extract or similar method to pare this array
down to just those ids where the value is 1. From there, you can
either loop (remember to call $this->Advert->create() each iteration)
and use saveField() or you could pass the entire array of ids to a
single query.

Of course, you should ensure that the controller knows not to look for
a view to render and to instead redirect. Next would be to use JS to
submit the form with AJAX, then maybe modify the updated rows'
appearance, etc.

On Mon, Sep 27, 2010 at 9:29 AM, james livsey
<jameslivsey@googlemail.com> wrote:
> Hi Jeremy thankyou for replying.
>
> yes there are multiple rows and the idea is that it would be more user
> friendly if the user could 'batch update' multiple records in one view.
>
> so in my index i have the normal columns of data eg
>
>     <td><?php echo $advert['Advert']['ad_size']; ?>&nbsp;</td>
>
> but at the end there is the addition of:
>
>     <td>
>     $this->Form->create('Advert');
>     echo $form->input('contract_received');
>     </td>
>
> with the form end after the foreach (so there isnt a submit for every row):
>
>    <?php echo $this->Form->end(__('Submit', true));?>
>
> On the controller i tried using saveAll as following
>
>    $this->Advert->set($this->data);
>    $this->Advert->saveAll($this->data);
>
> but i think im missing the way to 'fire' the event.
>
> Does that sound like the right way of doing it? At the moment the submit
> button doesnt do anything.
>
> Thanks - hope that isnt too confusing
>
> On Fri, Sep 17, 2010 at 2:47 PM, Jeremy Burns | Class Outfit
> <jeremyburns@classoutfit.com> wrote:
>>
>> Do you mean that there are multiple rows, each of which could have been
>> updated? If so, (assuming that your form encompasses all of the potential
>> updates) check your $this->data array when the form is submitted. Depending
>> upon its structure you can either do a saveAll($this->data) or loop through
>> and do a series of saves.
>>
>> Jeremy Burns
>> Class Outfit
>>
>> jeremyburns@classoutfit.com
>> http://www.classoutfit.com
>>
>> On 17 Sep 2010, at 14:38, james wrote:
>>
>> > Hi I have a field that shows if a contract has been received or not in
>> > my index view. This field needs to be editable from within the same
>> > view with one submit button committing the changes to the table. Is
>> > this possible? Im not entirely sure as to the correct way to go about
>> > this?
>> >
>> > Thanks in advance
>> >
>> > Check out the new CakePHP Questions site http://cakeqs.org and help
>> > others with their CakePHP related questions.
>> >
>> > 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+unsubscribe@googlegroups.com For more options, visit this group
>> > at http://groups.google.com/group/cake-php?hl=en
>>
>> Check out the new CakePHP Questions site http://cakeqs.org and help others
>> with their CakePHP related questions.
>>
>> 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+unsubscribe@googlegroups.com For more options, visit this group
>> at http://groups.google.com/group/cake-php?hl=en
>
> Check out the new CakePHP Questions site http://cakeqs.org and help others
> with their CakePHP related questions.
>
> 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+unsubscribe@googlegroups.com For more options, visit this group at
> http://groups.google.com/group/cake-php?hl=en
>

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: