I have three tables.
1.Autos
Table
autos.
2.Featues-
Table with fields Id and fields.
3.AutoFeatures
Table - auto-features fields (auto_id and feature_id)
In Auto Model I have.
class AutoListings extends CActiveRecord
{
/**
* Array to manage the HAS_MANY relation.
* You can't assign values directly to $features:
* it's a relation, so you have to bypass it
*/
public $featureIds = array();
public function relations()
{
'features' => array(self::MANY_MANY, 'AutoFeatures', '{{auto_listings_features}}(listing_id, feature_id)'),
}
protected function afterFind()
{
parent::afterFind();
/**
* To sync the two "twins": the relation called 'features'
* and the public variable 'featureIds'. If you don't do that
* you will not see checkbox checked for the features
* that are currently assigned to the an auto
*/
if(!empty($this->features))
{
foreach($this->features as $n => $feature)
$this->featureIds[] = $feature->id;
}
}
//Extension used for many-to-many relationship management
public function behaviors()
{
return array('CAdvancedArBehavior' => array(
'class' => 'application.extensions.CAdvancedArBehavior.CAdvancedArBehavior'));
}
}
Here is my Features model
class AutoFeatures extends CActiveRecord
{
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'autoListing' => array(self::MANY_MANY, 'Autos', '{{auto_features}}(feature_id, auto_id)'),
);
}
/*
* Get the features list
*/
public function getFeaturesOptions()
{
return CHtml::listData(AutoFeatures::model()->findAll(), 'id', 'features');
}
}
In my controller for autos where everything is happening.
For create action
public function actionCreate()
{
$model->features = $_POST['Autos']['featureIds']
if($model->save())
{
$this->redirect(array('view','id'=>$model->id));
}
}
In update section.
public function actionUpdate()
{
$model->features = $_POST['Autos']['featureIds']
if($model->save())
{
$this->redirect(array('view','id'=>$model->id));
}
}
Next come the form
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'auto-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'type'),
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
<div class="fleft marg_left">
<div class="row">
<?php echo $form->labelEx($model,'features'); ?>
<?php
/*$this->widget('application.extensions.EchMultiselect.EchMultiselect', array(
//the data model associated with this widget:
'model' => $model,
// the attribute associated with drop down list of this widget:
//'dropDownAttribute' => 'featureIds',
// data for generating the options of the drop down list:
'data' => $model->getFeaturesOptions(),
// the name of the drop down list (this must be set if 'model' and 'dropDownAttribute' are not set):
'name' => 'model_color',
// the selected input value(s) (used only if 'model' and 'dropDownAttribute' are not set)
'value' => array($model, 'featureIds'),
// additional HTML attributes for the drop down list:
'dropDownHtmlOptions'=> array(
'style'=>'width:240px;',
),
// options for the jQuery UI MultiSelect Widget
// (see the project page for available options):
'options' => array(
'header'=>true,
'height'=>175,
'minWidth'=>225,
'checkAllText'=>Yii::t('application','Check all'),
'uncheckAllText'=>Yii::t('application','Uncheck all'),
'noneSelectedText'=>Yii::t('application','Select an Option'),
'selectedText'=>Yii::t('application','# selected'),
'selectedList'=>false,
'show'=>'',
'hide'=>'',
'autoOpen'=>false,
'multiple'=>true,
'classes'=>'',
'position'=>array(),
// set this to true, if you want to use the Filter Widget
'filter'=>false,
),
// options for the jQuery UI MultiSelect Filter Widget
'filterOptions'=> array(
'label' => Yii::t('application','Filter:'),
'width'=>100,
'placeholder'=>Yii::t('application','Enter keywords'),
'autoReset'=>false,
),
));*/
?>
<?php echo $form->checkBoxList($model, 'featureIds', $model->getFeaturesOptions());
?>
</div>
</div>
As in the above when I use my code I had previously
<?php echo $form->checkBoxList($model, 'featureIds', $model->getFeaturesOptions());
?>
Everything works fine.
But when I apply the extension the values are showing fine but when I select and submit the error is in the action update and create. Undefined value featureIds. The $model->getFeaturesOptions() is the one pulling values from the database for the features so that an auto can have as many values for the features as possible linked through the third table auto-features
As you can see the relationship is through features but featureIds takes in the afterfind method.
The problem is how should I place featureIds in your extension widget. If I do with my case and following the forum that had this issue my code is working fine creating and updating the database I don’t get any error.
When I view the source code on the form featureIds is shown using my line of code but the extension is not showing and the issue might be the where to call the featureIds as in my case.