thinkphp Multi-to-many correlation model (RPM)

Source: Internet
Author: User

Build a model first

123456789101112 <?php class UserModel extends RelationModel{     protected $_link=array(       "group"=>array(           "mapping_type"=>MANY_TO_MANY,           "foreign_key"=>"uid",//中间表的字段           "relation_foreign_key"=>"gid",//中间表的字段           "relation_table"=>"think_user_group"     )     ); }?>

Then create the database. Create three tables respectively: Think_user think_group Think_user_group

User table UserID, username field

Group table GroupID, email fields

User_group table uid, GID field

The UID in the Foreign_key is associated with the UserID field in the user table

The GID in Relation_foreign_key is associated with the GroupID field in group

And then create it in the controller.

12345678910 <?php// 本类由系统自动生成,仅供测试用途class IndexAction extends Action {    public function index(){        $db=D("user");        $list=$db->relation(true)->select();        echo "<pre>";        print_r($list);    }}

You can see all the relevant data in the data table is output!

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.