Setting up an author automatically
Almost all applications that involve multiple content authors should have a way to track who created the content or who is the owner.
From this recipe, you will learn how to automate this by using a model. We assume that the application uses CUserIdentity to manage authorization and that Yii::app()->user->id returns integer user ID. We don't need to change the original post author if someone else edited it.
Getting ready
Create a new application by using
yiic webappas described in the official guide at the following URL:http://www.yiiframework.com/doc/guide/en/quickstart.first-app
Set up a database connection and create a table named
postas follows:DROP TABLE IF EXISTS `post`; CREATE TABLE IF NOT EXISTS `post` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `text` TEXT NOT NULL, `author_id` INT(10) UNSIGNED NOT NULL, PRIMARY KEY (`id`) );
Generate the
Postmodel using Gii.
How to do it...
Add the following method...