Problem : actionCreate for 2 relaed tables

I have 2 tables (with their related models)

Report -> model : Report

id , title

Attachment -> model : Attachment (with report_fk foreign key to Report table id field)

id , file , report_fk

This application has a form with fields from Report and Attachment both on the same page with one submitButton that it saves both models

actionCreate for Report


model1 = new Report;

model2 = new Attachment;


if ($model1->save()  &&  $model2->save()) {



Issue :

When a user creates a new report and he attaches some file to the report that they will save in Attachment table I can’t fill the report_fk. when I run actionCreate on Attachment controller it got error coz its report_fk can not be null and I don’t know the report id coz Report record will save after Attachment record is saved. it’s a problem about sequence.


it works now

if ($model1->save() )


    	if ($model2->save() )