Getting the ids of multiple inserted hasMany items in CakePHP -
description / explanation:
on "add event" page, can add multiple schedules. when saveall(), saves event data , schedule(s) data - working correctly.
but then, want process schedule(s) data , build individual rows in "dates" table.
event hasmany schedule schedule belongsto event schedule hasmany date date belongsto schedule
so - i'm loading date model, , repeating through each schedule passed, , processing/inserting dates (based on repeat, start date...etc etc).
my problem / question:
i need id of each schedule it's dates ("schedule_id" field). how id's of individual schedules during repeat? currently, i'm doing this:
foreach($this->data['schedule'] $i => $value) { $this->data['date']['schedule_id'] = $this->event->schedule->id; //other stuff here $this->date->saveall($this->data['date']); }
but gives me same id in every loop.
the problem is, $this->event->schedule->id
holds last id of insert. why same id.
i suppose doing foreach loop within add-function of events-controller. if want each own insert-id shouldn't saveall on event , rather loop through each schedule (as do) , save schedules there. this:
foreach ($this->data['schedule'] $schedule) { $schedule['event_id'] = $this->event->id; //this needed because don't saveall on event anymore $this->event->schedule->save($schedule); //here comes part $this->data['date']['schedule_id'] = $this->event->schedule->id; //other stuff here $this->event->schedule->date->saveall($this->data['date']); }
hope helps!
Comments
Post a Comment