One thing I impress of YII is there Active Record feature. Instead of declaring the sql command to query the data of two or more tables. In YII we can just create relation function in the model class to join the tables by assigning the foreign keys.
Yii Relational Active Record Approaches
In Yii there are two approaches mainly the lazy loading approach and the eager loading approach. Both approaches have been documented on Yii relational active record tutorial. But if you are lazy to read, this is how each approach is being use. Lazy loading approach is use when you are dealing with 1 record and eager loading approach comes in handy when there are more than 1 records you wish to access. This is made in this way to reduce the number of join which lead to inefficiency according to Yii document page.
1. Establishing DB Connection
The following application configuration shows an example:
1 2 3 4 5 6 7 8 9 10 | return array( 'components'=>array( 'db'=>array( 'class'=>'system.db.CDbConnection', 'connectionString'=>'sqlite:path/to/dbfile', // turn on schema caching to improve performance // 'schemaCachingDuration'=>3600, ), ), ); |
2.- Creating Record
To insert a new row into a database table, we create a new instance of the corresponding AR class, set its properties associated with the table columns, and call the save() method to finish the insertion.
1 2 3 4 5 | $post=new Post; $post->title='sample post'; $post->content='content for the sample post'; $post->create_time=time(); $post->save(); |
3. Reading Record
To read data in a database table, we call one of the find methods as follows.
1 2 3 4 5 6 7 8 | // find the first row satisfying the specified condition $post=Post::model()->find($condition,$params); // find the row with the specified primary key $post=Post::model()->findByPk($postID,$condition,$params); // find the row with the specified attribute values $post=Post::model()->findByAttributes($attributes,$condition,$params); // find the first row using the specified SQL statement $post=Post::model()->findBySql($sql,$params); |
4. Updating Record
After an AR instance is populated with column values, we can change them and save them back to the database table.
1 2 3 | $post=Post::model()->findByPk(10); $post->title='new post title'; $post->save(); // save the change to database |
5. Deleting Record
We can also delete a row of data if an AR instance has been populated with this row.
1 2 | $post=Post::model()->findByPk(10); // assuming there is a post whose ID is 10 $post->delete(); // delete the row from the database table |
Note, after deletion, the AR instance remains unchanged, but the corresponding row in the database table is already gone.
The following class-level methods are provided to delete rows without the need of loading them first:
1 2 3 4 | // delete the rows matching the specified condition Post::model()->deleteAll($condition,$params); // delete the rows matching the specified condition and primary key(s) Post::model()->deleteByPk($pk,$condition,$params); |
6. Data Validation
AR performs data validation automatically when save() is being invoked. The validation is based on the rules specified in the rules() method of the AR class. For more details about how to specify validation rules, refer to the Declaring Validation Rules section. Below is the typical workflow needed by saving a record:
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: Php Framework, Yii