Okay. My first project with yii. And my first question in the forum
I have a model ‘customer’ and model ‘products’. Sure, a customer can have many products. In ‘products’ are only product id and product description. In ‘customer’ I have a field also named ‘products’.The tables have no relations, I try to keep it pretty simple.
protected/views/customer/_form.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'customer-form',
'enableAjaxValidation'=>false,
));?>
<div class="row">
<?php echo $form->labelEx($model,'products'); ?>
<?php echo $form->ListBox($model,'products',CHtml::listData($pmodel,'product','description'),array('multiple'=>1)); ?>
<?php echo $form->error($model,'products'); ?>
</div>
So, I see the product description in the list box
The controller I have modified with:
public function actionCreate()
{
$model=new Customer;
if(isset($_POST['Customer']))
{
$model->attributes=$_POST['Customer'];
$m = implode(",", $model->products);
$model->products = '';
$model->products = $m;
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
This save the product id in the customer table.
Now I will update the customer. ListBox shows the available products from the product table but not the saved items for this customer.
How can I get it? Or how can we do better?
thanks