本着一个举例子的心态来写文章,努力的让文章写的更通俗易懂。

数据库表结构:
tableName:tags 标签表
731321E0-0D1E-419D-B7DB-6B12355FE320
tableName:images 图片表
F4A97D56-E45E-4428-BAFE-831BB8890531
tableName:imagesTags 图片标签关联表
869906E6-DEC1-4CF5-899A-0463074AB574
tableName:user 用户表
B23BE1D4-FB35-421B-AAB8-18697C08823E
表关系:
user(1) --->images(*)一对多
images(1)<----->(*)imagesTags(*)<--->tags(1)多对多
一对一单独举例吧

列出用户所有图片:

Controller代码:
$return = User::find()->with('images')->where(['source' => $_GET['userId']])->asArray()->all();
UserModels代码:
  public function getImages(){
        //关联关系 Images表的userId关联User表的source 注意不要反了
        return $this->hasMany(Images::className(), ['userId' => 'source']); 
    }
   var_dump($return);

最后我们看下打印出来的效果

array(1) {
  [0]=>
  array(5) {
    ["id"]=>
    string(4) "1767"
    ["name"]=>
    string(6) "大萌"
    ["avatar"]=>
    string(44) "http://img3.douban.com/icon/up79093814-3.jpg"
    ["source"]=>
    string(8) "79093814"
    ["images"]=>
    array(2) {
      [0]=>
      array(9) {
        ["id"]=>
        string(4) "9845"
        ["localPath"]=>
        string(57) "/www/songlin/douban/images/2015-12-01/144897995332650.jpg"
        ["yunPath"]=>
        string(36) "80c15f15gw1eykj57s6ibj20c80fot9n.jpg"
        ["iId"]=>
        string(8) "81830392"
        ["title"]=>
        string(15) "【晒】无聊"
        ["beautiful"]=>
        string(1) "0"
        ["ugly"]=>
        string(1) "0"
        ["userId"]=>
        string(8) "79093814"
        ["imgType"]=>
        string(4) "sina"
      }
      [1]=>
      array(9) {
        ["id"]=>
        string(4) "9890"
        ["localPath"]=>
        string(57) "/www/songlin/douban/images/2015-12-01/144898127333326.jpg"
        ["yunPath"]=>
        string(36) "80c15f15gw1eykkizbq08j20dw0hp768.jpg"
        ["iId"]=>
        string(8) "65112832"
        ["title"]=>
        string(13) "【晒wulq】"
        ["beautiful"]=>
        string(1) "0"
        ["ugly"]=>
        string(1) "0"
        ["userId"]=>
        string(8) "79093814"
        ["imgType"]=>
        string(4) "sina"
      }
    }
  }
}

返回的数组中多了个images节点,里面存的是images表中的多条记录。
看下页面中yii2 log纪录的sql

  3	09:52:22.040	0.4 ms	SELECT	SELECT * FROM `images` WHERE `userId`='79093814'
  4	09:52:22.039	0.3 ms	SELECT	SELECT * FROM `User` WHERE `source`='79093814'

从log能看出来 yii2表关联都是多条执行的

多对多关联
根据tag查询出图片
Controller:

    $count = ImagesTags::find()->where(['iTagsId' => $_GET['tag']])->with('images','tags')->asArray()->orderBy('iId desc')->count();
            $pages = new Pagination(['totalCount' =>$count, 'pageSize' => '16']);
            $pages->pageSizeParam = 'perpage';
    $images = ImagesTags::find()->offset($pages->offset)->limit($pages->limit)
                ->with('images','tags')->where(['iTagsId' =>  $_GET['tag']])->asArray()->orderBy('iId desc')->all();
  

Models:

     public function getImages(){
        return $this->hasOne(Images::className(), ['id' => 'iImageId']);
    }

    public function getTags(){
        return $this->hasOne(Tags::className(),['id'=>'iTagsId']);
    }

最后看下出来的纪录

   array(1) {
  [0]=>
  array(5) {
    ["iId"]=>
    string(3) "144"
    ["iImageId"]=>
    string(4) "9931"
    ["iTagsId"]=>
    string(1) "3"
    ["images"]=>
    array(9) {
      ["id"]=>
      string(4) "9931"
      ["localPath"]=>
      string(57) "/www/songlin/douban/images/2015-12-01/144898129229796.jpg"
      ["yunPath"]=>
      string(36) "80c15f15gw1eykkwcthvyj20dw0ij78b.jpg"
      ["iId"]=>
      string(8) "81813193"
      ["title"]=>
      string(21) "【晒】雾都孤儿"
      ["beautiful"]=>
      string(1) "0"
      ["ugly"]=>
      string(1) "0"
      ["userId"]=>
      string(1) "0"
      ["imgType"]=>
      string(4) "sina"
    }
    ["tags"]=>
    array(2) {
      ["id"]=>
      string(1) "3"
      ["tag"]=>
      string(9) "小牛仔"
    }
  }
}

多出了tags与images节点。
一对一的思路跟一对多是一个意思,改成hasOne即可

本文为松林原创,转载请注明出处,尊重他人成果。
http://www.songlin51.com/archives/1264.html