Yii Framework Forum: Delete action in CRUD example - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Delete action in CRUD example Rate Topic: -----

#1 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 10 December 2008 - 09:27 AM

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

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 10 December 2008 - 10:07 AM

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

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 10 December 2008 - 01:34 PM

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

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 10 December 2008 - 01:38 PM

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

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 10 December 2008 - 01:47 PM

Thanks Qiang, i'll study that code.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users