correlation model [1]

Source: Internet
Author: User

A Introduction to the Model
Correlation model, a total of three modes. One-on-one: One_to_one, including Has_one and belongs_to;
One-to-many: One_to_many, including Has_many and belongs_to; many-to-many: Many_to_many.
Use table relationships to understand these three modes:
One to one: User tables and identity cards, a user can only correspond to an identity card, and an identity card can only correspond to one
A user. This is one-to-one.
One-to-many: User table and Message table: A user can post N messages, and each message can only be by one user
Published. This is one-to-many. Even if multiple users publish the same message, but the ID is not the same, so, not
Can be understood as many-to-many.
Many-to-many: User tables and Role tables: a user can be a certified professional and an auditor. So the character table
The auditor can also be a small new role for crayons, or it can be a luffy role. This is many-to-many.

Two Associating operations

The user table and the ID Card Association, has_one (one-to-ones), indicates that a user has only one identity card.

Create two data tables first:

Think_card

Think_user

Weibo/home/controller/usercontroller. Insert code in class. php :

1<?PHP2 3 namespace Home\controller;4  UseThink\controller;5  UseHome\model\usermodel;6 7 classUsercontrollerextendsController {8      Public functionindex () {9         $user= D (' User ');Ten         $arr=$user->relation (true),Select (); One         Var_dump($arr); A     } -}

And then in Weibo/home/user/model/usermodel. insert code in class. PHP :

1<?PHP2 namespace Home\model;3  UseThink\model;4  UseThink\model\relationmodel;5 6 classUsermodelextendsRelationmodel {7     protected $_link=Array(8' Card ' =Array(9' Mapping_type ' =>self::has_one,Ten' Class_name ' = ' Card ', One' Mapping_name ' = ' Card ', A' Foreign_key ' = ' uid ', -' Mapping_fields ' = ' Code ', -' As_fields ' = ' Code ', the' Condition ' = ' id=1 ', -), -         );  -}

Has_one supports the following properties

Class_name The associated model class name, if not written, will automatically locate the related data table.
Mapping_name The name of the association map used to get the array name of the data.
Mapping_type Set the correlation mode, such as one-to-one self::has_one.
Foreign_key The name of the associated foreign key, which automatically corresponds to the ID of the current data table.
Mapping_fields The field that is associated with the query is queried by default.
As_fields The associated field is mapped to a field of a sibling.
Condition Associated conditions, additional queries are used.

In Weibo/home/user/model/usermodel. test the effect of each line of code in class. PHP:

1 protected $_link Array (2         ' Card ' = =array(3             ' Mapping_type ' =>self::has_one, 4             ' Class_name ' + ' Card ',5             ' mapping_name ' = ' carder ',6             ),7         );    

The effect is:

' class_name ' = ' Card ', this line can not be written, because the default will automatically locate the relevant data table,

But it cannot be written wrong, it must be the same as the name of the data table.

' mapping_name ' = ' carder ', then the name of the data table is changed to Carder , when nothing is shown,

is because no associated foreign key is set, plus ' foreign_key ' = ' uid ', after which the effect is obtained

At this point, you can get think_card and think_user data tables associated together.

Add ' mapping_fields ' = ' code ', and only get the code data in Think_card;

With ' as_fields ' + ' code ', the code data Representation in Think_card is the same as the data representation in Think_user:

Plus ' condition ' = ' id=1 ', after getting Think_card to get only id=1 data, just a few changes in the way SQL queries

Not in addition to the former is:

After the addition:

At this time, only id=1 data in the read data has code, the other code data is null,

Here is the belongs_to correlation model:

The Belongs_to association indicates that the current model belongs to another parent object, for example, each user belongs to a department

New Weibo/home/controller/cardcontroller. Class. PHP , the code is:

1<?PHP2 namespace Home\controller;3  UseThink\controller;4 5 classCardcontrollerextendsController {6      Public functionindex () {7         $card= D (' Card ');8         $arr=$card->relation (true),Select ();9         Var_dump($arr);Ten     } One}

New Weibo/home/user/model/cardmodel. Class. php , inserting code:

1<?PHP2 namespace Home\model;3  UseThink\model;4  UseThink\model\relationmodel;5 6 classCardmodelextendsRelationmodel {7     protected $_link=Array(8' User ' =Array(9' Mapping_type ' =>self::belongs_to,Ten' Foreign_key ' = ' uid ', One' Mapping_fields ' = ' user ', A' As_fields ' = ' user ', -), -     ); the  -}

' foreign_key ' = ' uid ', the associated UID is the structure in the card table, not the structure in the user table. The data for the UID in the card is: 1,2,4, the result is:

The red mark is the part that needs attention, if the ID is written here, the result of the data in the card table is:

The data obtained at this time is the user's 3rd name.

correlation model [1]

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.