Configuring relations for processing multi-Table associations in yii ar

Source: Internet
Author: User

The relational active record official document states:
The association between the two tables is based on the foreign key. However, although this foreign key Association is useful in data error tolerance, it is performance impaired, generally, foreign keys are not defined.
In this case, what is the basis for their association?
There are two tables A and B. Both tables A and B have a field filedx, and the field filedx is also an associated field of A and B. In this case, you can directly
Public Function relations ()
{
// Note: you may need to adjust the relation name and the related
// Class name for the relations automatically generated below.
Return array (
'Varname' => array ('relationtype', 'classname', 'ledx',... additional options)
);
}

In this way, the relations method is used.

In another case, although two tables A and B are associated, the joined field is not the same name. For example, in table A, the field name associated with table B is afieldb, if the field name associated with Table A in Table B is bfielda, you cannot directly use the preceding method to perform join query. You can change it to the following:
Return array (
'Varname' => array ('relationtype', 'classname', '', 'on' => 't. afieldb = varname. bfielda',... additional options)
);
Explanations:
The first three elements of array cannot be changed. Their meaning corresponds to their order. The first element must be of the correlated type, the second element must be the class name of the model associated with the table, and the third element must be an associated field. Except for three elements, other elements have little to do with the position. However, the key of the element must be specified. In this example, 'on' is the key name. The third element here is null, so there are two single quotes, the data key values of 'on' next to them specify their associations, T. afieldb, indicating the main table field afieldb, varname. bfielda indicates the bfielda field of the joined table. By the way, in all relations, the primary table has an alias, and this alias is t, and the joined table also has an alias, this alias is the varname defined, for example:
'Shops' => array (SELF: has_one, 'subjectshops', '', 'on' => 't. Sid = shops. id '),
The alias of the joined table is shops.

In addition to this 'on' element, you can specify additional options when declaring a link, which is summarized as follows:
Select: List of columns to select (select) in the associated ar class. The default value is '*', that is, all columns are selected. The column name in this option should be clear.

Condition: Where condition. The default value is null. The column name in this option should be clear.

Params: the parameter to bind to the generated SQL statement. The value should be assigned as an array by name-value. This option is valid from version 1.0.3.

On: The on statement. The conditions specified here will be appended to the join condition through the and operator. The column name in this option should be clear. This option is not applied to the many_many relation. This option is valid from version 1.0.2.

Order: The order by statement. The default value is null. The column name in this option should be clear.

With: A list of child related objects that shoshould be loaded together with this object. Be aware that using this option inappropriately may form an infinite relation loop.

Jointype: type of join for this relationship. It defaults to left Outer Join.

Alias: the alias for the table associated with this relationship. This option has been available since version 1.0.1. It defaults to null, meaning the table alias is the same as the relation name.

Together: whether the table associated with this relationship shocould be forced to join together with the primary table and other tables. this option is only meaningful for has_logs and many_many relations. if this option is set false, the table associated with the has_associated or many_relative relation will be joined with the primary table in a separate SQL query, which may improve the overall query performance since less duplicated data is returned. if this option is set true, the associated table will always be joined with the primary table in a single SQL query, even if the primary table is paginated. if this option is not set, the associated table will be joined with the primary table in a single SQL query only when the primary table is not paginated. for more details, see the section "relational query performance ". this option has been available since version 1.0.3.

Join: the extra join clause. It defaults to empty. This option has been available since Version 1.1.3.

Group: the Group by clause. It defaults to empty. column names referenced in this option shocould be disambiguated.

Having: The having clause. It defaults to empty. column names referenced in this option shocould be disambiguated. Note: option has been available since version 1.0.1.

Index: the name of the column whose values shocould be used as keys of the array that stores related objects. without setting this option, an Related Object array wocould use zero-based integer index. this option can only be set for has_logs and many_many relations. this option has been available since version 1.0.7.

In addition, the following options are available for certain relationships during lazy loading:

Limit: Limit of the rows to be selected. This option does not apply to belongs_to relation.

Offset: offset of the rows to be selected. This option does not apply to belongs_to relation.

 

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.