Using external actions
In Yii, you can define controller actions as separate classes and then connect them to your controllers. This way, you can reuse some common functionality.
For example, you can move the backend for autocomplete fields to an action and save some time by not having to write it over and over again.
Another simple example that we will review is deleting a model.
Getting ready
Set up a new application using
yiic webapp.Create a database schema with the following script:
CREATE TABLE `post` ( `id` int(10) unsigned NOT NULL auto_increment, `created_on` int(11) unsigned NOT NULL, `title` varchar(255) NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `user` ( `id` int(10) unsigned NOT NULL auto_increment, `username` varchar(200) NOT NULL, `password` char(40) NOT NULL, PRIMARY KEY (`id`) );
Generate the
PostandUsermodels using Gii. Add some data to the tables.
How to do it...
Let's write
protected/controllers/PostController.php. It is a usual...