Hi,
I’m newbie in Yii Framework and I have some fields stored in my DB Schema. From that, I wanna pre fill a TbSelect2 with an information that I have.
I can get the correct value, but I can’t save this value in DB.
My DB Schema is:
Lugar
idLugar
lugar
idLocalidade (FK)
Localidade
idLocalidade
freguesia
concelho
distrito
5526
I have Monte selected (correct), but this is because I ran a sql query and returned this.
But now I want choose other value from all information of Localidade.
5527
In Localidade I have information like:
30717, Monte, Fafe, Braga
30709, Fafe, Fafe, Braga
10146, Ajude, Póvoa de Lanhoso, Braga
…
…
The problem is: My model doesn’t update with the chosen information.
Summing up, I wanna bring to the form the values that corresponds to the fields stored in my database for update them, but I cannot GET the value of TbSelect2.
If I choose Fafe, Fafe, I’m not able to take the value 30709 for store in Lugar table.
My code is this:
In _form.php of Lugar.php:
<div class="row">
<?php echo $form->labelEx($modelLocalidade,'Concelho /Freguesia'); ?>
<?php
if ($model->isNewRecord) {
$this->widget('bootstrap.widgets.TbSelect2', array(
'model' => $modelLocalidade,
'attribute' => 'idLocalidade', //'id' CAMPO A BUSCAR
'data' => CHtml::listData($modelLocalidade->findAll(), 'idLocalidade', 'freguesia', 'concelho'),
'htmlOptions' => array('placeholder' => 'Selecione a nova localidade',),
)); //listData($modelDESEJADO->findAll(), 'CAMPO A SER MODIFICADO', 'CAMPO A MOSTRAR NO COMBOBOX')
} else if (!$model->isNewRecord) {
$qry = "SELECT lugar.idLocalidade, freguesia, concelho from localidade, lugar where lugar.idLocalidade = localidade.idLocalidade";
$result = Yii::app()->db->createCommand($qry)->queryAll();
$data=CHtml::listData($result, 'idLocalidade', 'freguesia', 'concelho');
$this->widget('bootstrap.widgets.TbSelect2', array(
'model' => $model,
'attribute' => 'idLocalidade', //'id' CAMPO A BUSCAR
'data' => CHtml::listData($modelLocalidade->findAll(), 'idLocalidade', 'freguesia', 'concelho'),
'htmlOptions' => array(
'options' => $data,
),
));
}
?>
</br></br>
<?php echo $form->error($modelLocalidade,'idLocalidade'); ?>
</div>
In LugarController.php:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$modelLocalidade = new localidade;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['lugar']) && isset($_POST['localidade']))
{
$model->attributes=$_POST['lugar'];
$modelLocalidade->attributes=$_POST['localidade'];
$model->idLocalidade = $modelLocalidade->idLocalidade;
if($model->save()) {
$model->save();
$this->redirect(array('view','id'=>$model->idLugar));
}
}
$this->render('update',array(
'model'=>$model,
));
}
My actionCreate() is OK, but actionUpdate() not.
Anyone help me, please.
Thanks.