Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
$model->name = $_POST['User']['name'];
$model->first_name = $_POST['User']['first_name'];
$model->description = $_POST['User']['description'];
$user->attributes = $_POST['User'];
$profile->attributes = $_POST['Profile'];
$transaction = $db->beginTransaction();
if ($user->save() && $profile->save()) {
...
$transaction->commit();
} else {
$transaction->rollback();
}
$model->name = $_POST['User']['name'];
$user->attributes = $_POST['User'];
$profile->attributes = $_POST['Profile'];
public function actionCreate(){
$modelUser = new User();
$modelProfile = new Profile();
. . .
$this->render('form',array(
'modelUser'=>$modelUser,
'modelProfile'=>$modelProfile,
));
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'UserForm',
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($modelUser); ?>
<?php echo $form->errorSummary($modelProfile); ?>
<div class="row">
<?php echo $form->textField($modelUser,'login',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($modelUser,'login'); ?>
</div>
<div class="row">
<?php echo $form->textField($modelProfile,'name'); ?>
<?php echo $form->error($modelProfile,'name'); ?>
</div>
. . .
$modelUser->profile = $modelProfile
или чуть более корректно $modelUser->setRelatedRecord('profile', $modelProfile, false);
CActiveForm::errorSummary() можно передать массив моделей.<?php echo $form->errorSummary(array($modelUser, $modelProfile)); ?>
$user->attributes = $_POST['User'];
'name'=>'Имя',
'first_name'=>'Фамилия',
'description'=>'Описание'
Пошаговое руководство сохранения связанных данных Yii