thinkphp Correlation Model Note points

Source: Internet
Author: User

Here are examples of commodity and commodity categories

1, the table name is Goods_type the model name is Goodstypemodel, if the model name is not this, you have to define the protected $tableName = model corresponding to the main table name

2. Model class must inherit Relationmodel

3. Three kinds of related relationships

One-to-one links: One_to_one, including Has_one and belongs_to
One-to-many associations: One_to_many, including Has_many and belongs_to
Many-to-many associations: Many_to_many, which specifically defines a table to represent a many-to-many relationship between two tables

(1) One-to-one example: commodity and commodity attributes

Commodity Model class

Class Goodsmodel extends relationmodel{

Protected $fields = Array ();

Protected $_link = Array (

' Jhgoodsattribute ' = Array (
' Mapping_type ' = Has_one,
' Class_name ' = ' jhgoodsattribute ',
' Foreign_key ' = ' goods_id ',
),

);

}

Commodity attribute Model class

Class Jhgoodsattributemodel extends Relationmodel {

Protected $fields = Array (
' goods_id ', ' sale_price ', ' showname ', ' original_price ', ' suggest_price ', ' up_num ', ' volume ', ' issues_time ', ' Pagedetails ', ' shelves ', ' _pk ' = ' goods_id ', ' _autoinc ' and false
);
Protected $_link = Array (
' Goods ' = belongs_to,
);

}

(2) One-to-many model employees and permissions

Employee Model Classes

Class Staffmodel extends Relationmodel {

Protected $fields = Array (
' Identity ', ' name ', ' num ', ' pswd ', ' department_id ', ' job ', ' _pk ' = ' num ', ' _autoinc ' and false
);
Protected $_link = Array (
' authority ' = Array (
' Mapping_type ' = Has_many,
' Class_name ' = ' authority ',
' Mapping_name ' = ' authority ',
' Mapping_key ' = ' num ',
' Foreign_key ' = ' staff_num '
),

Permission class

Class Authoritymodel extends Relationmodel {

Protected $fields = Array (
' id ', ' staff_num ', ' authority_num ', ' _pk ' = ' id ', ' _autoinc ' + = True
);
Protected $_link = Array (
' Staff ' = belongs_to
);

}

(3) Many-to-many relationship goods and commodity categories

Product Category

Class Goodsmodel extends Relationmodel {

Protected $fields = Array (
' id ', ' name ', ' Code ', ' Details ', ' specification ', ' pack ', ' Weight ', ' volume ', ' unit ', ' remark ', ' expire_threshopld ', ' min _threshopld ', ' producer_id ', ' _pk ' = ' id ', ' _autoinc ' = ' = True '
);
Protected $_link = Array (
' Goodstype ' = Array (
' Mapping_type ' = Many_to_many,
' Class_name ' = ' goodstype ',
' Relation_foreign_key ' = ' goods_type_id ',
' relation_table ' = ' goods_type_relation '
),

}

Commodity category Model Class

Class Goodstypemodel extends Relationmodel {

Protected $fields = Array (
' id ', ' parent_id ', ' name ', ' _pk ' = ' id ', ' _autoinc ' = = True
);
Protected $_link = Array (
' Goods ' = Array (
' Mapping_type ' = Many_to_many,
' Class_name ' = ' Goods ',
' Relation_foreign_key ' = ' goods_id ',
' Foreign_key ' = ' goods_type_id ',
' relation_table ' = ' goods_type_relation '
),

}

thinkphp Correlation Model Note points

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.