Answer the question
In order to leave comments, you need to log in
How to correctly write the update method for 2 models?
The question is this: there is a method in the associated model that saves data to 2 tables at once
public function actionCreate()
{
$model=new Fio;
$phone = new Phone;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Fio'], $_POST['Phone']))
{
$model->attributes=$_POST['Fio']; // сначало сохраняем fio
// $model->Street_ID=6;
$model->Street_ID = $_POST['Street_ID']; // затем говорим ему что Street_ID нужно брать из Street_ID которое обьявлено в DynamicStreets методе
$phone->attributes=$_POST['Phone'];
//$phone->Type_ID=2;
//$phone->FIO_ID = $model -> $_POST['ID'];
if(($model->save())){
$phone->FIO_ID = $model-> ID;
if ($phone->save())
$this->redirect(array('view', 'id' => $model->ID));
}
}
$this->redirect(array('index'));
}
<div class="row">
<?PHP echo CHtml::form();
$data = CHtml::listData(Town::model()->findAll(), 'ID', 'Town'); // первое поле, тут запрашивается город и его id
echo CHtml::dropDownList('ID', '',$data,
array(
'prompt'=>'Select Region',
'ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('Fio/dynamicStreets'), //ссылка на метод в контроллере
'update'=>'#Street_ID', //тэг для второго поля
'data'=>array('ID'=>'js:this.value'),
)));
echo CHtml::dropDownList('Street_ID','', array(), array('prompt'=>'Select City'));?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Surname'); ?>
<?php echo $form->textArea($model,'Surname',array('rows'=>1, 'cols'=>40)); ?>
<?php echo $form->error($model,'Surname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Patronymic'); ?>
<?php echo $form->textArea($model,'Patronymic',array('rows'=>1, 'cols'=>40)); ?>
<?php echo $form->error($model,'Patronymic'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Name'); ?>
<?php echo $form->textArea($model,'Name',array('rows'=>1, 'cols'=>40)); ?>
<?php echo $form->error($model,'Name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Building'); ?>
<?php echo $form->textField($model,'Building'); ?>
<?php echo $form->error($model,'Building'); ?>
</div>
<div>
<?php echo $form->labelEx($phone,'Number'); ?>
<?php echo $form->textArea($phone,'Number',array('rows'=>1, 'cols'=>40)); ?>
<?php echo $form->error($phone,'Number'); ?>
</div>
<div>
<?php echo $form->labelEx($phone,'Type_ID'); ?>
<?php echo $form->dropDownList($phone,'Type_ID', CHtml ::listData(Type::model()->findAll(), 'ID','Type_Phone')); ?>
<?php echo $form->error($phone,'Type_ID'); ?>
</div>
<?php $this->renderPartial('_form', array('model'=>$model,
// 'phone'=>$phone
)); ?>
he swears at $phone, this renderer is in update view, the same renderer is registered in index, where it is accepted without problems. When you try to switch to update with this code, it says that $phone is from the form<div>
<?php echo $form->labelEx($phone,'Number'); ?>
<?php echo $form->textArea($phone,'Number',array('rows'=>1, 'cols'=>40)); ?>
<?php echo $form->error($phone,'Number'); ?>
</div>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question