Book Image

Yii Project Blueprints

By : Charles R. Portwood ll
Book Image

Yii Project Blueprints

By: Charles R. Portwood ll

Overview of this book

Table of Contents (15 chapters)
Yii Project Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the locations model


To interact with our data, we need to create a model that once again references our new database table. Using the instructions outlined in Chapter 1, A Task-management Application, we enable the Gii module and create a new model called Location to interact with the locations table in our database.

Once created, we add a beforeSave() method to the generated file (protected/modules/Location.php) to automatically set the created and updated time:

public function beforeSave()
{
   if ($this->isNewRecord)
      $this->created = time();

   $this->updated = time();

   return parent::beforeSave();
}

Then, we modify the rules() method:

public function rules()
{
    return array(
        array('created, updated', 'numerical', 'integerOnly'=>true),
        array('name, lat, long, city, state', 'required'),
        array('title, data', 'safe'),
        array('name, lat, long, city, state, created, updated', 'safe', 'on'=>'search'),
    );
}