Creating reusable controller actions
Common actions, such as deleting the AR model by the primary key or getting data for AJAX autocomplete, could be moved into reusable controller actions and later attached to controllers as needed.
In this recipe we will create a reusable delete action that will delete the specified AR model by its primary key.
Getting ready
Create a fresh Yii application using
yiic webapp.Create a new database and configure it.
Execute the following SQL:
CREATE TABLE `post` ( `id` int(11) NOT NULL auto_increment, `text` text, `title` varchar(255) default NULL, PRIMARY KEY (`id`) ); CREATE TABLE `comment` ( `id` int(11) NOT NULL auto_increment, `text` text, PRIMARY KEY (`id`) );
Generate models for
postandcommentusing Gii.
How to do it...
Create
protected/extensions/actions/EDeleteAction.php:<?php class EDeleteAction extends CAction { public $modelName; public $redirectTo = array('index'); /** * Runs the action. * This method is invoked by the...