On Fri, Jan 2, 2009 at 11:53 PM, brian <[email protected]> wrote:
> I have an admin_index page for a model that requires a listing for
> each row, which includes a *separate* form with a couple of
> checkboxes. The controller action is:
>
> public function admin_index()
> {
>        $this->data = $this->paginate();
> }
>
> Now, in the view, I cannot figure out how to get the FormHelper to
> properly set the "checked" attribute on these checkboxes. Why doesn't
> this work?
>
> (I'm not interested in editing multiple records with a single form, btw.)
>
> foreach($this->data as $k => $foo)
> {
> ?>
> <?= $form->create('Foo', array('action' => 'edit')) ?>
> <?= $form->hidden("Foo.${k}.id") ?>
> <table>
>        <tr>
>                <td>
>                        <?= $foo['User']['label'] ?>
>                        (<?= $foo['User']['id'] ?>)
>                </td>
>                <td>
>                        <?= $form->input("Foo.${k}.something") ?>
>                </td>
>                <td>
>                        <?= $form->input("Foo.${k}.something_else") ?>
>                </td>
>                <td>
>                        <?= $form->submit('edit') ?>
>                </td>
>        </tr>
> </table>
> <?= $form->end() ?>
>
> I thought FormHelper was able to do this if $this->data is set. I know
> that I can just type out the form markup, but ...
>

I just clued in to the fact that the data array was in the wrong form.
I'm using contain to grab the User's name info and the array looked
like:

Array
(
    [0] => Array
        (
            [Foo] => Array
                (
                    [id] => 1
                    [bar] => 1
                    [another_field] => 0
                    [user_id] => 3
                )

            [User] => Array
                (
                    [first_name] => ...
                    [last_name] => ...
                    [organisation_name] => ...
                    [id] => 3
                    [label] => ...
                )

        )
        ...
                
Of course, I need:

Array
(
    [Foo] => Array
        (
            [0] => Array
                (
                    [first_name] => ...
                    [last_name] => ...
                    [organisation_name] => ...
                    [id] => 1
                    [label] => ...
                    [bar] => 1
                    [another_field] => 0
                    [user_id] => 3
                )
                ...

I solved it by using the most excellent Set class.

$tmp = $this->paginate();

$this->data['Foo'] = Set::merge(
        Set::extract($tmp, '{n}.User'),
        Set::extract($tmp, '{n}.Foo')
);
unset($tmp);

I had to put the User extraction first in the merge params so that
Foo->id wouldn't get clobbered because contain grabs User->id even if
it's not in the fields list.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to