How do I save more than one ModelB at te same time?

On 5/1/06, Chris Cassell <[EMAIL PROTECTED]> wrote:
>
> I had to solve this problem myself. Here's what I do--perhaps not the most
> elegant, but it does work. (This code is off the top of my head and may need
> some tweaking)
>
> In your view set your inputs with their various modelname/fieldname name
> attributes:
> echo $html->input ('ModelA/date');
> echo $html->input ('ModelB/input1');

I already added this on my view.
echo $html->input ('ModelB2/input1');
echo $html->input ('ModelB3/input1');

> echo $html->input ('ModelD/input1');
>
> Then in your controller (or your model) remove the ModelB and ModelC arrays
> from $this->params['data']:
>
> $modelb = $this->params['data']['ModelB'];
> unset ($this->params['data']['ModelB']);
I also added this...
$modelb2 = $this->params['data']['ModelB2'];
 unset ($this->params['data']['ModelB2']);
$modelb3 = $this->params['data']['ModelB3'];
 unset ($this->params['data']['ModelB3']);

> $modeld = $this->params['data']['ModelD'];
> unset ($this->params['data']['ModelD']);
>
> Then after saving Model A, get its id and add it to the $modelb and $modeld
> arrays:
>
> if ($this->ModelA->save ($this->params['data']))
> {
> $a_id = $this->ModelA->getLastInsertId ();
> $modelb['A_id'] = $a_id;
> $modeld['A_id'] = $a_id;
> $this->ModelA->ModelB->save (array ('ModelB' => $modelb));
$this->ModelA->ModelB->save (array ('ModelB' => $modelb2));
$this->ModelA->ModelB->save (array ('ModelB' => $modelb3));
 ..This code first saves the data of $modelb2 and then replaces it
with the data of $modelb3. How do I save $modelb3 to a new 'ModelB'?
Thanks!

> $this->ModelA->ModelD->save (array ('ModelD' => $modeld));
> }
>
> If you're going to do it in the model, you'll need to  break it into the
> beforeSave and afterSave methods for Model A and you'll need to add some
> logic to check for editing or adding so that you don't kill the A_id.
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to