Yii Framework2.0 Development Tutorial (6) Database Mysql--activerecord

Source: Internet
Author: User

The active record (activity log, AR) provides an object-oriented interface to access data in the database.

an AR class is associated with a data table , each AR object corresponds to a row in the table , and the properties of the object (that is, the AR attribute attribute) map to the corresponding column of the data row .

An Activity record (AR object) corresponds to a row of the data table, and the properties of the AR object map the corresponding column of the row.

You can manipulate data in a data table directly in an object-oriented way, so mom doesn't have to worry about writing native SQL statements.

Edit models/country.php

<?php namespace App\models; Use yii\db\activerecord;//you don't have to write any code inside. Just like now, Yii can guess the corresponding data table name based on the class name. Class Country extends activerecord{}

Or

<?php namespace App\models; Use yii\db\activerecord;//you don't have to write any code inside. Just like now, Yii can guess the corresponding data table name based on the class name. Class Country extends activerecord{//@return string returns the data table name associated with the AR class public static function TableName () {return ' Country ';}}

Edit views/zhyoulun/helloworld.php

<?php$country = new \app\models\country; $country->code = ' ZZ '; $country->name = ' Youlun '; $country Population = ' 1 '; $country->save ();? >

Visit Web page http://localhost/basic/web/index.php?r=zhyoulun/helloworld, you can see a record in the database


Querying data

Get all rows of the country table and sort by name $countries = Country::find ()->orderby (' name ')->all (); Echo ' <table border= ' 1 "> ' for ($i =0; $i <count ($countries); $i + +) {echo ' <tr> '; Echo ' <td> '. $countries [$i] [' Code ']. ' </td> '; Echo ' <td> '. $countries [$i] [' name ']. ' </td> '; Echo ' <td> '. $countries [$i] [' population ']. ' </td> '; Echo ' </tr> ';} Echo ' </table> ';

Retrieving clients with native SQL statements: $sql = ' SELECT * from customer '; $customers = Customer::findbysql ($sql)->all ();

Get data in an array form

Retrieve customer information as an array instead of an object: $customers = Customer::find ()    ->asarray ()    ->all ();//$customers Each element is a key-value array


Get data in bulk

Extract 10 customer information at a time foreach (Customer::find ()->batch () as $customers) {    //$customers is an array of 10 or fewer customer objects}//extract 10 guests at a time Customer::find ()->each () as $customer) {    //$customer is a "Customer" Object}// Greedy load Mode batch query foreach (Customer::find ()->with (' orders ')->each () as $customer) {}


Operational data (AR provides the following methods for inserting, updating, and deleting a row in the table associated with an AR object)

[[Yii\db\activerecord::save () |save ()]]
[[Yii\db\activerecord::insert () |insert ()]]
[[Yii\db\activerecord::update () |update ()]]
[[Yii\db\activerecord::d elete () |delete ()]

Insert record for new customer $customer = new Client (); $customer->name = ' James '; $customer->email = ' [email protected] '; $customer- >save ();  Equivalent to $customer->insert (); Update existing customer records $customer = Customer::find ($id); $customer->email = ' Bond @demo.com '; $customer->save ();  Equivalent to $customer->update (); Delete an existing customer record $customer = Customer::find ($id); $customer->delete ();

AR also provides a static method that can be applied to the entire table associated with an AR class. Be careful when using these methods, because they act on the whole table! For example, DeleteAll () will delete all the records in the table.

[[Yii\db\activerecord::updatecounters () |updatecounters ()]]
[[Yii\db\activerecord::updateall () |updateall ()]]
[[Yii\db\activerecord::updateallcounters () |updateallcounters ()]]
[[Yii\db\activerecord::d Eleteall () |deleteall ()]


Reference:

Http://www.yiichina.com/guide/2/db-active-record


Reprint Please specify source: http://blog.csdn.net/zhyoulun/article/details/40476471

Yii Framework2.0 Development Tutorial (6) Database Mysql--activerecord

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.