Table Association (continued)

Source: Internet
Author: User

How to define table Association?

$ Belonsto

 

Define subordinate Association

1. Foreign keys are placed in the master table.
2. Records of the associated table are not automatically updated during storage.
3. Records of the joined table will not be updated during deletion.

Example:
Multiple articles belong to one topic.

Format 1:

* Class Articles
*{
* Var $ belonsto = array (
* Array (
* 'Tableclass' => 'columns ',
* 'Foreignkey' => 'column _ id ',
* 'Ingingname' => 'column'
*),
*);
*}

In the preceding format, foreignKey indicates the fields used in the current table to store primary key references to the associated table. ��
MappingName indicates the name of the table to be joined in the returned results of the primary table. ��
If the mappingName parameter is not provided, tableClass is used.

Format 2:

* Class Articles
*{
* Var $ belonsto = 'columns ';
*}

The second is a simplified method. The foreignKey field name will be the same as the primary key field name of the associated table.

======================================

$ HasMany

Define one-to-multiple associations

1. The external key is saved in the associated table.
2. Records of the associated table are automatically updated during storage.
3. Associate Records are automatically deleted when the primary table record is deleted.

Example:
Each user has multiple orders.

Format 1:

* Class Users
*{
* Var $ hasparameters = array (
* Array (
* 'Tableclass' => 'Orders ',
* 'Foreignkey' => 'user _ id ',
* 'Mappingname' => 'Orders ',
*),
*);
*}

In a one-to-multiple relationship, the current table does not contain a primary key reference to the associated table. ��
Instead, the primary key reference for the current table is saved in the associated table.
In the preceding format, foreignKey indicates the fields used in the joined table to store primary key references to the current table. ��
MappingName indicates the name of the table to be joined in the returned results of the primary table. ��
If the mappingname parameter is not provided, tableclass is used.

Format 2:

* Class users
*{
* Var $ hasenders = 'Orders ';
*}

In the simplified writing method, the foreignkey field name will be the same as the primary key field name of the current table.

======================================

$ Hasone

Define a one-to-one Association
1. Foreign keys are placed in the associated table;
2. If an associated object exists during storage, records of the associated table will be automatically created or updated;
3. Associate Records are automatically deleted when the primary table record is deleted.

Example:
The current table is users, which is used to store user accounts. Each user account has only one profile record.

Format 1:

* Class users
*{
* Var $ hasone = array (
* Array (
* 'Tableclass' => 'profiles ',
* 'Foreignkey' => 'profile _ id ',
* 'Ingingname' => 'profile ',
*),
*);
*}

In the preceding format, foreignKey indicates the fields used in the joined table to store primary key references to the primary table. MappingName indicates the name of the table to be joined in the returned results of the primary table. If the mappingName parameter is not provided, tableClass is used.

Format 2:

* Class Users
*{
* Var $ hasOne = 'profiles ';
*}

The second is a simplified method. The foreignKey field name is the same as the primary key field name of the primary table.

======================================

$ Manytoyun
��
Define multiple-to-multiple associations

1. The external key is saved in the intermediate table;
2. The intermediate table is automatically updated during storage.
3. When the primary table record is deleted, the related records of the intermediate table are automatically deleted.

Example:
Each member can have multiple roles, and each role can be assigned to multiple members.

Format 1:

* Class Members
*{
* Var $ manyToMany = array (
* Array (
* 'Tableclass' => 'roles ',
* 'Jointable' => 'Member _ roles ',
* 'Foreignkey' => 'Member _ id ',
* 'Assocforeignkey' => 'Role _ id ',
* 'Ingingname' => 'roles ',
*),
*);
*}

In the many-to-many relationship, the current table does not contain a primary key reference to the associated table. ��
Instead, it stores the primary key reference for the current table and associated table in an intermediate table.
In the preceding format, joinTable indicates the name of the intermediate table. ForeignKey and
AssocforeignKey indicates the fields used in the intermediate table to store
Reference the primary key field of the associated table.

MappingName indicates the name of the table to be joined in the returned results of the primary table. ��
If the mappingName parameter is not provided, tableClass is used.

Format 2:

* Class Members
*{
* Var $ manytoloud = 'roles ';
*}

In the simplified writing method, the foreignKey field name will be the same as the primary key field name of the current table.
The assocforeignKey field name is the same as the primary key field name of the associated data table.
The name of the intermediate table is calculated using the FLEA_Db_TableDataGateway: getMidtableName () method.

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.