Suggestions for FK in two model form

Hi,

I am using the two model one form method. Everything seems to be working okay, but I can’t seem to get the PK on $maintenance->id into $service->maintenance_events_id as FK. Here’s my controller. Is there something I’m missing?

Thanks :)


    public function actionNewService() {

        $maintenance = new MaintenanceEvents;

        $service = new ServiceSchedules;


        if (isset($_POST['MaintenanceEvents'],$_POST['ServiceSchedules']))

        {


            $service->attributes = $_POST['ServiceSchedules'];

            $maintenance->attributes = $_POST['MaintenanceEvents'];

     

            $valid=$service->validate();

            $valid=$maintenance->validate() && $valid;

            

            if($valid)  

            {  

                if($maintenance->save(false))  

                {  

                    $maintenance->id = $service->maintenance_events_id;

                    $service->save(false);  

                    $this->redirect(array('view', 'id' => $service->id)); 

                }  

            }

    

        }

        $this->render('createNewService', array(

            'maintenance' => $maintenance,

            'service' => $service,

        ));

    }



May be your code should look like this piece:




...

                    $service->maintenance_events_id = $maintenance->id;

...



I tried that, but that didn’t work either.

I figured this out. In my ServiceSchedules model I removed ‘maintenance_events_id’ as required, and included the following as unique check so there would only be one service record for one maintenance event.


      array('maintenance_events_id','unique','className'=>'ServiceSchedules',

            'message'=>'{attribute} exist on database.'), 

Thanks again for your help :)