PHP Development Framework Yii Framework Tutorial (26) Database-active Record Example

Source: Internet
Author: User
Using Java or. Net Write database applications, many people have used hibernate (or nhibernate) to greatly simplify database programming, and object to read and write database (ORM), Yii provides the active Record (AR) is also a popular Object-Relational mapping (ORM) technology. Each AR class represents a data table (or view), the columns of the data table (or view) are reflected in the AR class as attributes of the class, and an AR instance represents a row in the table. Common CRUD operations are implemented as an AR method. Therefore, we can access the data in a more object-oriented way.

This modifies the Yii Framework development Tutorial (24) Database-dao example to see how the Employee table is read using the active record.

To access a data table, we first need to define an AR class with an integrated Cactiverecord. Each AR class represents a single data table, and an AR instance represents a row in that table.

Since the AR class is often referenced in multiple places, we can import the entire directory containing the AR class instead of importing it individually. For example, if all of our AR class files are in the Protected/models directory, we can configure the application as follows:

' Import ' =>array (' application.models.* ',),

This example defines the employee class as follows:

Class Employee extends cactiverecord{public static function model ($className =__class__) {return Parent::model ($ ClassName);} Public Function TableName () {return ' Employee ';}}

The values of columns in a data table row can be accessed as properties of the corresponding AR instance. For example $employee->employeeid can access the employee's EmployeeId field.

This example simply reads the employee table and modifies the Sitecontroller Indexaction method:

Public Functionactionindex () {$model = Employee::model ()->findall (); $this->render (' index ', array (' model ' = $model,));}

As long as one line of code Employee::model ()->findall () is implemented to read the database table and assign the value function, to see the corresponding display record code:

{echo ' EmployeeId: '. $employee->employeeid. '; Echo ' first Name: '. $employee->firstname. '; Echo ' last Name: '. $employee->lastname. '; Echo ' Title: '. $employee->title. '; Echo ' Address: '. $employee->address. '; Echo ' Email: '. $employee->email. "Echo '----------------------';}? >

You can see that using AR allows you to access a field value directly from the field name of a database table (case sensitive) without having to define the class employee, which greatly simplifies the code.

In the previous introduction of model, said Cmodel has two sub-classes, one is Formmodel, the other is Cactiverecord, Cactiverecord defines the database access crud methods, such as

Create a record

To insert a new row into the datasheet, we create an instance of the corresponding AR class, set its properties related to the column of the table, and then call the Save () method to complete the insertion

$employee =new employee, $employee->firstname= ' James ', $employee->lastname= ' Shen ';.. $employee->save ();

If the table's primary key is self-increasing, after the insert is complete, the AR instance will contain an updated primary key. If a column is defined with a static default value (for example, a string, a number) in the table structure.

Reading Records

To read data from a data table, we can call one of the Find series methods in the following ways

Finds the first row in the result that satisfies the specified criteria
$post =post::model ()->find ($condition, $params);
Finds the row with the specified primary key value
$post =post::model ()->findbypk ($postID, $condition, $params);
Finds the row with the specified property value
$post =post::model ()->findbyattributes ($attributes, $condition, $params);
Finds the first row in the result with the specified SQL statement
$post =post::model ()->findbysql ($sql, $params); as shown above, we call the Find method through Post::model (). Keep in mind that the static method model () is required for each AR class. This method returns an AR instance in the object context that is used to access a class-level method (something similar to a static class method).

If the Find method finds a row that satisfies the query criteria, it returns a Post instance whose properties contain the values of the corresponding columns in the row of the data table. Then we can read the loaded values like the properties of a normal object, such as Echo $post->title;.

If you do not find anything in the database using the given query criteria, the Find method returns NULL.

When we call find, we use $condition and $params to specify the query criteria. Here $condition can be a where string in an SQL statement, $params a parameter array where the value should be bound to a placeholder in the $condation.

Update record

After the AR instance fills in the values of the columns, we can change them and save them back to the data table.

$post =post::model ()->findbypk, $post->title= ' new post title '; $post->save (); Save changes to database Delete record

If an AR instance is populated with a row of data, we can also delete this row of data

$post =post::model ()->FINDBYPK (10); Suppose there is a post whose ID is 10$post->delete (); Delete this line from the data table note that after deletion, the AR instance remains the same, but the corresponding row in the datasheet is gone.

Others can be found in the Yii Chinese document (http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.ar), which is not repeated here.

This example shows the result:

The above is the PHP Development Framework Yii Framework Tutorial (26) Database-active Record sample content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.