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