Delete action in CRUD example

As i do my first steps in Yii, some questions come up. So please forgive me if some things might be obvious.

I've created some models and CRUD operations for db tables. Now i've seen, that in the admin view this construct is used:

      <?php echo CHtml::linkButton('Delete',array(


          'submit'=>'',


          'params'=>array('command'=>'delete','id'=>$model->id),


          'confirm'=>"Are you sure to delete #{$model->id}?")); ?>


From my understanding that's the reason, why we need the additional method processAdminCommand() in the Controller. In that method, authorization has to be checked manually, because it's not a "standard action".

Now i wonder, why not use a construct like this:

      <?php echo CHtml::linkButton('Delete',array(


          'submit'=>$this->createUrl('delete',array('id'=>$model->id)),


          'confirm'=>"Are you sure to delete #{$model->id}?")); ?>


That way the actionDelete() would be used and the filter would handle authorization automatically. No need for processAdminCommand(). What am i missing?

Using "processAdminCommand" is like simulating the postback operation in Prado. After this command is executed, the page still remains where it is (including sorting and pagination parameters).

Using your approach, we would need to remember those parameters.

I see. It just feels like a little break in the pure action concept right in the beginning: Calling actionAdmin() when actually i want actionDelete() to be performed.

Since actions and views are independent of each other, IMO it would feel more "correct" to call actionDelete and decide there, which view to render after the delete has happened. This is already possible now and that's really great. But we don't have information about the state of the view that initiated the action (at least if we don't send them with the action request).

I'm not sure if you get my point, but i wonder if something like this can be realized with Yii.

To put it another way: I'd like to have one actionDelete() that can be used from any view and that knows about that view and its state, and decides if it returns to that view after the action or renders any other necessary view.

Makes sense?

Yes, it is possible to keep state in Yii using CHtml::statefulForm. Check the hangman demo to see how states are being kept.

I agree actionDelete is more natural for this case. But if you take a look at the login portlet (extension), you will see using action is not always good because it simply loses state information unless we want to take the trouble to keep the state.

Thanks Qiang, i'll study that code.