I have two related tables sales and products. a sale has many products.
VentasControler I have a controller, model of product sales.
I have also view sales
eh then followed step by step account as below:
Eh I have created the 1st class in my case StudentManager called ProductoManager that is hosted in the Controllers folder which will contain the same class StudentManager:
<?php
class ProductoManager extends TabularInputManager
{
protected $class='Producto';
public function getItems()
{
if (is_array($this->_items))
return ($this->_items);
else
return array(
'n0'=>new Producto(),
);
}
public function deleteOldItems($model, $itemsPk)
{
$criteria=new CDbCriteria;
$criteria->addNotInCondition('idproducto', $itemsPk);
$criteria->addCondition("idventa= {$model->primaryKey}");
Producto::model()->deleteAll($criteria);
}
public static function load($model)
{
$return= new ProductoManager();
foreach ($model->students as $item)
$return->_items[$item->primaryKey]=$item;
return $return;
}
public function setUnsafeAttribute($item, $model)
{
$item->idventa=$model->primaryKey;
}
}
then the VentaController which is my main driver so I modified the ActionCreate:
public function actionCreate()
{
$model=new Venta();
$productoManager=new ProductoManager();
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Venta']))
{
$model->attributes=$_POST['Venta'];
$productoManager->manage($_POST['Producto']);
if (!isset($_POST['noValidate']))
{
$valid=$model->validate();
$valid=$productoManager->validate($model) && $valid;
if($valid)
{
$model->save();
$productoManager->save($model);
$this->redirect(array('view','id'=>$model->idventa));
}
}
}
$this->render('create',array(
'model'=>$model,
'productoManager'=>$productoManager,
));
}
and _form this in views/venta/_form added this:
<div>
<h2>Productos:</h2>
<table>
<tr>
<th><?php echo Producto::model()->getAttributeLabel('descripcion')?></th>
<th><?php echo Producto::model()->getAttributeLabel('precio_con_igv')?></th>
<th><?php echo CHtml::link('add', '#', array('submit'=>'', 'params'=>array('Producto[command]'=>'add', 'noValidate'=>true)));?></th>
</tr>
<?php foreach($productoManager->items as $id=>$producto):?>
<?php $this->renderPartial('_formProducto', array('id'=>$id, 'model'=>$producto, 'form'=>$form));?>
<?php endforeach;?>
</table>
</div>
then I created a new form in views / sell / _formProducto
_formProducto call it contains the product code for the table
<tr>
<td>
<?php echo $form->textArea($model,"[$id]descripcion",array('size'=>50,'maxlength'=>255)); ?>
<?php echo $form->error($model,"title"); ?>
</td>
<td>
<?php echo $form->textArea($model,"[$id]precio_con_igv",array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,"description"); ?>
</td>
<td><?php echo CHtml::link(
'delete',
'#',
array(
'submit'=>'',
'params'=>array(
'Producto[command]'=>'delete',
'Producto[idproducto]'=>$id,
'noValidate'=>true)
));?>
</td>
</tr>
that’s all the error that I get to run is as follows
PHP Error
Description
Invalid argument supplied for foreach()
Source File
C:\xampp\htdocs\agobeta\protected\views\venta\_form.php(115)
<?php foreach($productoManager->items as $id=>$producto):?>
which are items?
I leave my attributes of the product and sales tables
Producto:
idproducto
descripcion
Venta
idventa
I hope your answers thanks thank you very much