Creating model behaviors
There are many similar solutions in today's web applications. Leading products, such as Google's Gmail, are defining nice UI patterns. One of these is soft delete . Instead of a permanent deletion with tons of confirmations, Gmail allows the user to immediately mark messages as deleted and then easily undo it. The same behavior can be applied to any object, such as blog posts, comments, and so on.
Let's create a behavior that will allow marking models as deleted, restoring models, and selecting not yet deleted models, deleted models, and all models. In this recipe we'll follow a test-driven development approach to plan the behavior and test if the implementation is correct.
Getting ready
Carry out the following steps:
Create a database and add a
posttable to your database:CREATE TABLE `post` ( `id` int(11) NOT NULL auto_increment, `text` text, `title` varchar(255) default NULL, `is_deleted` tinyint(1) NOT NULL default '0', PRIMARY KEY (`id`) )
Configure Yii...