Saturday, August 20, 2011

Re: Saving multiple records to multiple models

Your data array would look something like the following:

Array (
[Event] => Array(
[0] => Array(
[event_type_id] => 2
),
[1] => Array(
[event_type_id] => 2
)
),
[EventTransaction] => Array(
[0] => Array(
[inventory_type_id] => 1,
[amount] => 100.00
),
[1] => Array(
[inventory_type_id] => 2,
[amount] => 250.00
)
)
)

As you can see, the EventTransaction array is not related to the Event
array so saveAll could never associate these.

If you really want to save multiple events at the same time then you
need an array like the following:

Array (
[0] = Array(
[Event] => Array(
[event_type_id] => 2
),
[EventTransaction] => Array(
[inventory_type_id] => 1,
[amount] => 100.00
)
),
[1] = Array(
[Event] => Array(
[event_type_id] => 2
),
[EventTransaction] => Array(
[inventory_type_id] => 2,
[amount] => 250.00
)
)
)

You'll then have to loop through your data array and run multiple
saveAlls:

foreach($this->data AS $data) {
$this->Event->saveAll($data);
}

HTH, Paul.

On Aug 20, 4:29 pm, Greg Skerman <gsker...@gmail.com> wrote:
> Hi,
>
> Trying to work out the correct way of doing this.
>
> My database has an Events table, and several other tables for different
> event types (Event_Transactions, Event_Comments etc).
>
> The basic idea is that everything is an event, and the type of event
> dictates the table it joins to.
>
> As such, Event hasOne Event* (joined on the fly depending on usage).
>
> I want to be able to save multiple events on one form... in this case,
> multiple transactions...
>
> So, I have laid out my form as such:
>
> echo $this->Form->create('Event', array('url' => array('controller' =>
> 'transactions', 'action' => 'deposit')));
> echo $this->Form->hidden('Event.0.event_type_id', array('value' => 2));
> echo $this->Form->Select('EventTransaction.0.inventory_type_id',
> $InventoryTypes);
> echo $this->Form->Input('EventTransaction.0.amount');
> echo $this->Form->end('Submit');
>
> Javascript will be employed to add more fields, but you get the idea (second
> transaction will be Event.1.event_type_id and so forth)
>
> If i do a saveAll($this->data['event']); the resulting SQL is
>
> 10START TRANSACTION
> 0
> 0 11INSERT INTO `events` (`event_type_id`, `created`) VALUES (2, '2011-08-21
> 01:23:14')
> 1
> 0 12SELECT LAST_INSERT_ID() AS insertID
> 110 13COMMIT
>
> No mention of the EventTransaction. Same happens with plain old
> saveAll($this->data);
> If i remove the multi-row save stuff, and submit the form, the correct
> related information is saved.
>
> Have I missed something here? is my form structure right?

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate