Tag Archives: Active Record

Yii2 Data Manipulation in Database

Data Manipulation is quite easy to do in Yii2 and most of the methods are the same as in Yii were but with slight variation in syntax.

Following are the example of various Active Record methods:

To insert a new customer record:

To update an existing customer record:

To update an existing customer record:

To delete an existing customer record:

To delete several customers:

To increment the age of ALL customers by 1:

Happy Coding!

Yii2 Active Record Querying from Database

Lets say we have a table named ‘Customer’ and following are the few examples of retrieving data from database.

Retrieve all records from ‘Customer’ table:

To return customers whose ID is 1, 2 or 3:

To return a single customer whose ID is 1:

To retrieve all *active* customers and order them by their ID:

To return a single customer whose ID is 1:

To index the result by customer IDs:

To return customers in terms of arrays rather than Customer objects:

Happy Coding!