Good afternoon,
My issue is really similar with this one: http://www.yiiframework.com/forum/index.php/topic/29534-dropdownlist-value-not-being-saved/ (in my case I got Slide and SlideGroup, where slide has a foreign key to Slidegroup).
SlideForm (Slide Model):
class SlideForm extends CActiveRecord
{
public $hyperlink;
public $imagePath;
public $imageTitle;
public $slidegroup_fk;
public $selectedGroup;
public $image;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'slide';
}
public function relations() {
return array(
'slidegroup' => array(self::BELONGS_TO, 'SlideGroupForm', 'slidegroup_fk'),
);
}
public function attributeLabels()
{
return array(
'hyperlink' => 'Navigate',
'imagePath' => 'Name',
'imageTitle' => 'Title',
'slidegroup_fk'=> 'Group'
);
}
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
array('hyperlink', 'required'),
array('imageTitle', 'required'),
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('imageTitle',$this->imageTitle, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>20),
'sort'=>array(
'defaultOrder'=>array(
'imagetitle'=>false
)
)
));
}
}
SlideGroupForm (SlideGroup Model):
class SlideGroupForm extends CActiveRecord
{
public $folderPath;
public $groupTitle;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'slidegroup';
}
public function relations() {
return array(
'slides' => array(self::HAS_MANY, 'SlideForm', 'slidegroup_fk'),
);
}
public function attributeLabels()
{
return array(
'folderPath' => 'Name',
'groupTitle' => 'Group Title'
);
}
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
array('folderPath', 'required'),
array('groupTitle', 'required')
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('groupTitle',$this->groupTitle, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>20),
'sort'=>array(
'defaultOrder'=>array(
'groupTitle'=>false
)
)
));
}
}
Controller Action:
public function actionCreateSlide()
{
$model = new SlideForm();
if(isset($_POST['SlideForm']))
{
$model->attributes=$_POST['SlideForm'];
if($model->save())
{
} else
throw new CHttpException(500, 'Something went wrong');
}
$this->render('createSlide',array(
'model'=>$model
));
}
View:
<div class="well" style="float:left;">
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'slides-form',
'type' => 'horizontal',
'enableAjaxValidation'=>false,
'enableClientValidation' => true
)); ?>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->textFieldRow($model, 'imageTitle', array('class'=>'span5','maxlength'=>128)); ?>
<?php echo $form->textFieldRow($model, 'hyperlink', array('class'=>'span5','maxlength'=>128)); ?>
<?php
$options = CHtml::listData(SlideGroupForm::model()->findAll(array('order'=>'groupTitle')),'id','groupTitle');
echo $form->dropDownListRow($model, 'selectedGroup', array_filter($options));
?>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>'Add Slide'))
?>
</div>
<?php $this->endWidget(); ?>
</div>
Using chrome tools, I’m able to see that post data is being set correctly:
SlideForm[imageTitle]:yyy
SlideForm[hyperlink]:yyy
SlideForm[selectedGroup]:15
imageTitle and hyperlink are being saved correctly. selectedGroup isn’t, for some reason. The column name from the database table has the same name, so it should work as well.
I believe the line below is not able to populate one of the attributes for some reason.
$model->attributes=$_POST['SlideForm'];
I’m able to access it via $_POST[‘SlideForm’][‘selectedGroup’] though.
Any ideas?
Sincerely,
Apidcloud