Thinkphp the parent of a record in the correlation model (non-query children)

Source: Internet
Author: User

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)

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.