Data Sheet
ID cat_name cat_pid
76 mobile phone, digital 0
84 Mobile Phone Accessories 76
86 Bluetooth Headset 84
Affiliation : Bluetooth headset = (previous level) mobile phone accessories = (previous level) mobile phone, digital ( top -level)
Correlation model
Namespace Admin\model;use think\model\relationmodel;class Categorymodel extends relationmodel{protected $_link = array (' Category ' = = Array (///table name ' Mapping_type ' =>self::belongs_to, //key.) This is the query parent and the Has_m Any is the query child ' mapping_name ' = ' _parent ',//Custom ' mapping_order ' = ' cat_sort ', ' par Ent_key ' = ' cat_pid ',//key. The name of the field associated with the parent);
Code in the controller:
Public Function Search () {
$category = D (' category '); The D method is used. To use the correlation model
$id =; At this time 86 this ID is the lowest level in the data table. $this->id = $category->where (' id= '. $id)->order (' Cat_sort ')->relation (True)->select (); This time the query is 86 of the previous level. That is, the id=84, mobile phone accessories $lastPid = $category,relationget(true); If you want to make an association query based on the completed query, use the Relationget () method
Dump ($LASTPID);
The $lastpid is the topmost data (id=76, mobile, digital). (only if the data table is a three-tier structure.) If it is a two-tier structure, then the $this->id is already at the top level, and you do not need to use the Relationget () method)
If you do this again, you can query to a higher parent, provided the data table is a four-tier structure, and so on.
$lastLastPid = $category->relationget (true);
}
Thinkphp the parent of a record in the correlation model (non-query children)