Hi, I followed the Tabular Input tutorial and it’s working great so far.
Whenever I get some results back from a simple AR query then I can update those records. Now I am a little stuck on the following part:
Let’s say I don’t get any rows back from the query, how would I still be able to render the part which is in the foreach loop at least one time so I could perform an insert (well, technically it will still be a save() in AR)?
<?php foreach($items as $i=>$item): ?>
<tr>
<td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
<td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>
</tr>
<?php endforeach; ?>
Where $items would be the result of my AR query.
Also, I have an additional question about the tutorial. Within the controller there is the function
public function actionBatchUpdate()
{
// retrieve items to be updated in a batch mode
// assuming each item is of model class 'Item'
$items=$this->getItemsToUpdate();
if(isset($_POST['Item']))
{
$valid=true;
foreach($items as $i=>$item)
{
if(isset($_POST['Item'][$i]))
$item->attributes=$_POST['Item'][$i];
$valid=$item->validate() && $valid;
}
if($valid) // all items are valid
// ...do something here
}
// displays the view to collect tabular input
$this->render('batchUpdate',array('items'=>$items));
}
What I don’t really understand is what this method is supposed to do: $this->getItemsToUpdate();?