- Gii – Generating Module
- Gii – Generating Controller
- Gii – Creating a Model
- Yii - Gii
- Yii - Localization
- Yii - Authorization
- Yii - Authentication
- Yii - Error Handling
- Yii - Logging
- Yii - Aliases
- Yii - Fragment Caching
- Yii - Caching
- Yii - Testing
- Yii - Fields
- Yii - RESTful APIs in Action
- Yii - RESTful APIs
- Yii - Theming
- Yii - Database Migration
- Yii - Active Record
- Yii - Query Builder
- Yii - Data Access Objects
- Yii - Database Access
- Yii - Dependency Injection
- Yii - Configurations
- Yii - Creating a Behavior
- Yii - Behaviors
- Yii - Creating Event
- Yii - Events
- Yii - GridView Widget
- Yii - ListView Widget
- Yii - Data Widgets
- Yii - Data Providers
- Yii - Properties
- Yii - Sorting
- Yii - Pagination
- Yii - Formatting
- Yii - Files Upload
- Yii - Using Cookies
- Yii - Cookies
- Yii - Using Flash Data
- Yii - Sessions
- Yii - AJAX Validation
- Yii - Ad Hoc Validation
- Yii - Validation
- Yii - HTML Forms
- Yii - Rules of URL
- Yii - URL Routing
- Yii - URL Formats
- Yii - Responses
- Yii - HTTP Requests
- Yii - Creating Extensions
- Yii - Extensions
- Yii - Asset Conversion
- Yii - Assets
- Yii - Layouts
- Yii - Views
- Yii - Modules
- Yii - Widgets
- Yii - Models
- Yii - Using Actions
- Yii - Using Controllers
- Yii - Controllers
- Yii - Entry Scripts
- Yii - Application Structure
- Yii - Create Page
- Yii - Installation
- Yii - Overview
- Yii - Home
Yii Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Yii - Active Record
Active Record provides an object-oriented API for accessing data. An Active Record class is associated with a database table.
Yii provides the Active Record support for the following relational databases −
MySQL 4.1 or later
SQLite 2 and 3:
PostgreSQL 7.3 or later
Microsoft SQL Server 2008 or later
CUBRID 9.3 or later
Oracle
ElasticSearch
Sphinx
Additionally, the Active Record class supports the following NoSQL databases −
Redis 2.6.12 or later
MongoDB 1.3.0 or later
After declaring an Active Record class(MyUser model in our case) for a separate database table, you should follow these steps to query data from it −
Create a new query object, using the yiidbActiveRecord::find() method.
Build the query object.
Call a query method to retrieve data.
Step 1 − Modify the actionTestDb() method this way.
pubpc function actionTestDb() { // return a single user whose ID is 1 // SELECT * FROM `user` WHERE `id` = 1 $user = MyUser::find() ->where([ id => 1]) ->one(); var_dump($user); // return the number of users // SELECT COUNT(*) FROM `user` $users = MyUser::find() ->count(); var_dump($users); // return all users and order them by their IDs // SELECT * FROM `user` ORDER BY `id` $users = MyUser::find() ->orderBy( id ) ->all(); var_dump($users); }
The code given above shows how to use ActiveQuery to query data.
Step 2 − Go to http://localhost:8080/index.php?r=site/test-db, you will see the following output.
![Active Record](/yii/images/active_record.jpg)
Querying by primary key values or a set of column values is a common task, that is why Yii provides the following methods −
yiidbActiveRecord::findOne() − Returns a single Active Record instance
yidbActiveRecord::findAll() − Returns an array of Active Record instances
Example −
pubpc function actionTestDb() { // returns a single customer whose ID is 1 // SELECT * FROM `user` WHERE `id` = 1 $user = MyUser::findOne(1); var_dump($user); // returns customers whose ID is 1,2,3, or 4 // SELECT * FROM `user` WHERE `id` IN (1,2,3,4) $users = MyUser::findAll([1, 2, 3, 4]); var_dump($users); // returns a user whose ID is 5 // SELECT * FROM `user` WHERE `id` = 5 $user = MyUser::findOne([ id => 5 ]); var_dump($user); }
Save Data to Database
To save data to the database, you should call the yiidbActiveRecord::save() method.
Step 1 − Modify the actionTestDb() method this way.
pubpc function actionTestDb() { // insert a new row of data $user = new MyUser(); $user->name = MyCustomUser2 ; $user->email = mycustomuser@gmail.com ; $user->save(); var_dump($user->attributes); // update an existing row of data $user = MyUser::findOne([ name => MyCustomUser2 ]); $user->email = newemail@gmail.com ; $user->save(); var_dump($user->attributes); }
Step 2 − Go to http://localhost:8080/index.php?r=site/test-db, you will see the following output.
![Save Data to Database](/yii/images/save_data_database.jpg)
To delete a single row of data, you should −
Retrieve the Active Record instance
Call the yiidbActiveRecord::delete() method
Step 1 − Modify the actionTestDb() method this way.
pubpc function actionTestDb() { $user = MyUser::findOne(2); if($user->delete()) { echo "deleted"; } }
Step 2 − Type http://localhost:8080/index.php?r=site/test-db in the address bar of the web browser, you will see the following output.
![Delete Single Row Data](/yii/images/delete_single_row_data.jpg)
Step 3 − You can also call the yiidbActiveRecord::deleteAll() method to delete multiple rows of data, for example.
pubpc function actionTestDb() { MyUser::deleteAll( id >= 20 ); }Advertisements