Try putting:

$this->ProjectTasks->create();

in each loop of the foreach in Schedule::afterSave()

When you call save() multiple times on the same model, I have found
that I run into problems if I don't call create() in between each call
to save().


On Apr 17, 3:00 pm, Joshua McFarren <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Is there a case where Model::save() doesn't initiate the beforeSave
> and afterSave callbacks for that Model? Maybe this doesn't work from
> another afterSave callback? Are these callbacks only initiated when
> Model::save() is accessed from a Controller not another Model?
>
> I have a Schedule Model that has a start date (pivot_date) which is
> used to calculate the ProjectTask due dates that belong to it. My
> ProjectTask Model has beforeSave and afterSave callbacks which cascade
> through the list of tasks and update the due dates. This works really
> well.
>
> However when a Schedule changes these due dates need to be
> recalculated as well. So I want to cause this cascade of updates to
> its ProjectTasks by staring with the first one. Unfortunately 
> $this->ProjectTasks->save($task) only updates the task but doesn't kick in
>
> the beforeSave and afterSave callbacks which update the other tasks.
>
> I put in some debug items to verify this. The task name gets updated
> in the DB but the ProjectTask beforeSave doesn't kick in when accessed
> this way. I would expect to see the debug from there but I don't. If i
> remove that debug and exit still nothing happens.
>
> Any help would be greatly appreciated. I've read the Manual and APIs
> many times and I'm still not getting why this doesn't work.
>
> I'm running cake_1.2.0.6311-beta.
>
> Thanks,
> Joshua
>
> ######
>
> class Schedule extends AppModel {
>
>         var $name = 'Schedule';
>
>         function afterSave($created) {
>                /*
>                 *  If the pivot_date changes we need to recalculate due
>                 *  dates: Find the project_tasks which are not dependent
>                 *  on another ProjectTask. This will likely be the first
>                 *  task in the schedule. Now save those tasks to force
>                 *  a recalculation of the due date when the ProjectTask
>                 *  beforeSave is called. The ProjectTask afterSave will
>                 *  then cause a cascading set of updates to each successive
>                 *  task in the schedule and all the due dates will be fixed
>                 */
>                 if (! $created) {   // true if UPDATE not CREATE
>                         $schedule_id = $this->id;
>                         $independentTasks = $this->ProjectTasks->findAll(
>                                 "ProjectTasks.schedule_id = '$schedule_id'" .
>                                         "AND ProjectTasks.depends_on IS NULL",
>                                 NULL,
>                                 NULL
>                         );
>                         foreach ($independentTasks as $task) {
>                                 $task['ProjectTasks']['name'] =
>                                         $task['ProjectTasks']['name'] . " 
> Altered!";
>                                 $this->ProjectTasks->save($task);
>                         }
>                 }
>                 parent::afterSave($created);
>         }
>
> }
>
> ######
>
> class ProjectTask extends AppModel {
>
>         var $name = 'ProjectTask';
>
>         function beforeSave() {
>                /*
>                 *  We're using this to insert a calculated due date
>                 */
>                 echo debug($this->data);
>                 exit;
>                 if(isset($this->data)){
>                         $this->__setDueDate($this->data);
>                         return parent::beforeSave();
>                 } else {
>                         return false;
>                 }
>         }
>
>         function afterSave($created) {
>                /*
>                 *  Find all the ProjectTasks which are dependent
>                 *  on the task we just saved and update them to
>                 *  force a recalculation of the due date when before
>                 *  save is called. This will cause a cascading set
>                 *  of updates to each successive task in the schedule
>                 */
>                 if (! $created) {   // true if UPDATE not CREATE
>                         $project_task_id = $this->id;
>                         $dependentTasks = $this->findAll(
>                                 "ProjectTask.depends_on ='$project_task_id'",
>                                 NULL,
>                                 NULL
>                         );
>                         foreach ($dependentTasks as $task) $this->save($task);
>                 }
>                 parent::afterSave($created);
>         }
>
> }
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to