Tabular Input: Render even if AR result is empty

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): ?>


<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>


<?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'





        foreach($items as $i=>$item)




            $valid=$item->validate() && $valid;


        if($valid)  // all items are valid

            // something here


    // displays the view to collect tabular input



What I don’t really understand is what this method is supposed to do: $this->getItemsToUpdate();?


You mean an update or insert view? You could check [font="Courier New"]count($items)[/font] and display the form accordingly, or redirect to the create method.

But according to the tutorial, you’re supposed to be in update mode, ie you do have items to update, which brings the second QA :)

It’s just a method to retrieve what you want to update. It can be anything. Remember, that guide page is about batch update, so it’s not the standard update of one single $id that is directly passed by an argument, but rather some collection of items depending on your needs.

If your need is to batch update a related model, I guess that method would be [font=“Courier New”]$modelA->modelBs[/font] :)

Otherwise, you may look here for an example

Thank you bennouna for pointing this out :)

You have been a great help - again :D

Thanks a bunch!!!