Blogger Information
Blog 16
fans 0
comment 0
visits 18175
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分享:关于TP5的一对一、一对多同时存在的关联查询
忧郁之子的博客
Original
2147 people have browsed it

看着手册摸索的,请高手帮完善;有些地方其实也不明白是什么意思。

表:member 下属字段: id, username, password
表:basic 下属字段:id, uid, city
表:photo 下属字段: id, uid, url
关系:member 是主表, 和basic是一对一关系; 和photo是一对多关系;

member 的MODEL:

//建立和basic的关联

public function basic()
    {
        return $this->hasOne('Basic','uid');//hasOne是一对一
    }

    //建立和photo的关联

    public function photo()
    {
        return $this->hasMany('photo','uid');//hasMany是一对多
    }

basic 的MODEL:

public function member()
    {
        return $this->belongsTo('Member');
    }

photo 的MODEL:

public function member()
    {
        return $this->belongsTo('Member');
    }

member 的controller:

public function list()
    {
        $member=new MemberModel();
         $list=$member->with('photo,basic')->find('15')->toArray();//查询一条ID为15的用户数据;toArray()是将结果转为数组。
        $print_r($list); 
    }


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!