我一直试图为我的AR关系实现这个: http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/ 做:
public function actionCreate()
{
$application = new Application;
$company = new AppCompany;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Application'], $_POST['AppCompany']))
{
$application->attributes=$_POST['Application'];
$company->attributes=$_POST['AppCompany'];
$valid=$application->validate();
$valid=$company->validate() && $valid;
if($valid)
{
if($application->save(false))
{
$company->applicant_id = $application->id;
$company->save(false);
$this->redirect(array('view','id'=>$application->id));
}
}
}
$this->render('create',array(
'application'=>$application,
'company'=>$company,
));
}
然后,当我尝试创建一个新应用程序并输入所有有效数据时 - 此数据成功保存到两个表中。但是如果我将一些字段留空(虽然它们是必需的),那么我有这个错误:类AppCompany的对象在验证时无法转换为字符串。
拜托,请你帮我看看我的错误?