I am trying to make my own EAV implementation, and I am stumbled upon this problem.
EAV will have these types:
text, catalog, article
text is a single textarea.
catalog is a complex view that renders a list of books from a database and returns multiple values delimited with (,)
article is a complex view that uses CKEditor with a template (practically a textbox).
My EAV implementation is as follows:
attribute_values
avid (PK)
field_id (where the types of text, catalog, article are stored)
section_id (where the section I want to place them is)
value (the variable value)
vieworder
I have already saved all the fields but for value I use a dropdownlist in the form.
Depending on the dropdownlist value (1,2,3) it brings me a partial view via ajax.
<div class="row">
<?php echo $form->labelEx($model,'attributeid'); ?>
<?php //echo $form->textField($model,'attributeid');
echo $form->dropDownList($model,'attributeid',CHtml::listData(Attributes::model()->findAll(),'attributeid','name'),
array(
'prompt' => 'Επιλογή control',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('attributeValues/dynamiccontent'),
'update'=>'#dynamic_content',
)
));
?>
<?php echo $form->error($model,'attributeid'); ?>
</div>
<div class="row">
<?php echo $form->hiddenField($model,'value'); ?>
</div>
<div id="dynamic_content"></div>
My controller has this:
public function actionDynamiccontent()
{
//CMS_Debug::output_array($data);
if (isset($_POST['AttributeValues']['attributeid']) && !empty($_POST['AttributeValues']['attributeid'])) {
$id = intval($_POST['AttributeValues']['attributeid']);
$myAttrib = Attributes::model()->findByPk($id)->getAttributes();
$params = array('field_type'=>$myAttrib,'data'=>$_POST['AttributeValues']);
$this->renderPartial("_ajaxContent",$params);
}
}
And finally my _ajaxContent has this code:
<div class="row">
<?php
//print_r($data);
switch ($field_type['type']) {
case "catalog":
$criteria = new CDbCriteria();
$criteria->condition = "showme=1";
$criteria->select = 'id,title';
$model1 = Products::model()->findAll($criteria);
//echo CHtml::textField('productid',$data['value']);
echo CHtml::dropDownList('productid',$data['value'],CHtml::listData($model1,'id','title'),array('multiple'=>'multiple', 'size'=>5));
break;
case "text":
echo CHtml::textField('productid',$data['value']);
break;
default:
echo "Default.";
}
?>
</div>
When I try to save the record, I get no value (since it’s saved on productid), so a little bit of jquery is required (on top of _form.php) :
<?php
$myJQuery = <<<EOF
$(document).ready(function() {
$('form').submit(function(){
var type = 'text';
$('#productid').each(function(i,t) {
type = t.type;
});
if (type == 'select-multiple') {
var multipleValues = $('#productid').val() || [];
var pid = multipleValues.join(',');
alert('Multiple Value PID:'+pid);
$("#AttributeValues_value").val(pid);
} else {
var pid = $('#productid').val();
alert('Single Value PID:'+pid);
$('#AttributeValues_value').val(pid);
}
return true;
});
});
EOF;
Yii::app()->clientScript->registerScript('validateScript',$myJQuery,CClientScript::POS_READY);
?>
Yet, here is where I get a bump, since I am saving the value to AttributeValue_value field (hidden), but when it goes to actionUpdate() or actionCreate(), it still shows my field empty.
So is there any way to get the $form->value inside the partial view or a way to save properly the missing field?
Thanks in advance.