Yii Framework Analysis Note 4:ar model and DB

Source: Internet
Author: User
Tags vars yii

In Yii, two types of models are implemented, namely the form model and the activity record. In the case of persisted data, Yii only realizes the activity record, for the complex data relation can use DAO which the framework provides to write the model itself, the ORM which corresponds to the integration doctrine has not studied carefully.

AR model
The model in the framework is the Cmodel subclass, Cmodel is mainly some validation and error handling, and implements an iterator and an array of access interfaces, and the activity record model Cactiverecord is the base class of the AR model.

Each AR class represents a single data table, and an AR instance represents a row in that table. AR is an object-relational mapping (ORM) design pattern that is responsible for data persistence. Encapsulates the database curd operation, and it is a domain model that encapsulates some of the business logic. An AR class includes a table schema, constraints, and curd
There are two ways to instantiate a model, either directly to new one, or by using the static method mode (), which differs from the former in that the code executed by the constructor is skipped (mainly to avoid multiple fetches of metadata), and the static method mode () is used to create an AR object through the steps:

[PHP]View Plaincopy
  1. $model:: Model ($class)
  2. $model =self::$_models[$className]=new $className (null); Create a mode instance
  3. New Cactiverecordmetadata ($model)//Create metadata class
  4. 1. Get the table pattern instance
  5. $model->getdbconnection () //Get database connection
  6. ->getschema ()//Get database schema
  7. ->gettable ($tableName)//Get table Shema
  8. ->loadtable ()//Get Table Object
  9. ->findcolumns ()//Get the Column objects in the table and the constraints in them
  10. ->findconstraints ()//Get Table constraint (code is actually a foreign key relationship)
  11. 2. Primary KEY Configuration
  12. 3. Add relationships (relationships include belongs_to, Has_one, Has_many, Many_many, STAT)
  13. $model->attachbehaviors ($model->behaviors ()); Add Behavior

There is a problem with the above process, and each time the model is created, queries for "show COLUMNS from $tableName" and "Show CREATE TABLE $tableName" are performed, which can affect performance in high concurrency. The configuration of schemacachingduration and Schemacacheid can be added to the database configuration in the master configuration file to cache the database schema, which is the time to set the cache, which is the ID of the cache component. However, the problem with caching is that modifying the table structure in the database does not take effect immediately, so it is necessary to write a script that refreshes the cache in a production environment.

DAO
The benefit of the AR model is to minimize the writing of SQL statements in development, and the AR model encapsulates both the state of the database record and the access method that persisted to the database, as well as the business logic, which is obvious in the applications where the relationship is not very complex, but if the database relationship is complex and the business logic is complex, It is necessary to put forward the operation of data separately, the DAO in Yii is the separation of business logic and data exchange, and also reduces the coupling of business and database.
In Yii, DAO is based on PDO, and the unified interface can access different database management systems. In the framework, the Cdbconnection, Cdbcommand, Cdbdatareader, and Cdbtransaction classes respectively control the amount of data connection, command, read, and transaction.

Yii also maps the database schema, where a database is an object, a table corresponds to an object, and a column corresponds to an object that encapsulates some of the actions of its corresponding schema.

Yii Framework Analysis Note 4:ar model and DB

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.