• 20.Yii2.0框架多表关联一对多查询之hasMany


    目录

    新手模式

    用上次的查询结果,作为本次的查询条件

        //关联查询
        public function actionRelatesearch(){
            //新手模式
            $category = Category::findOne(2); //查分类表里的数据 abj
            $categoryarr = $category->attributes; //获取某一行的分类结果里的对象属性,array
            dump($categoryarr);
    
    
            //查文章里的cid等于2的 拿上面的结果 $categoryarr['cid']=2 作为本次查询的条件
            $article = Article::find()->asArray()->where(['cid'=>$categoryarr['cid']])->all();
            dump($article); //返回的是二维数组arr
        }
    
    

    结果:

    Array
    (
        [cid] => 2
        [c_name] => 娱乐
    )
    
    Array
    (
        [0] => Array
            (
                [article_id] => 6
                [article_title] => php的发展前景怎样
                [num] => 8
                [cid] => 2
            )
    
        [1] => Array
            (
                [article_id] => 7
                [article_title] => 67岁宋春丽与聂远出新戏,满头花发让人心疼,面容慈祥豪爽依旧!
                [num] => 12
                [cid] => 2
            )
    
        [2] => Array
            (
                [article_id] => 8
                [article_title] => 知微又涨粉啦!《天盛长歌》小衣衣、楚王和金狮王子你会选谁?
                [num] => 18
                [cid] => 2
            )
    
        [3] => Array
            (
                [article_id] => 9
                [article_title] => 刘亦菲穿旗袍带仙气,赵丽颖穿旗袍优雅,她穿旗袍最有民国范
                [num] => 14
                [cid] => 2
            )
    
    )
    

    hasMany关联模式查询

    思路
    1.用category表里查一行,查出某一个分类下的所有字段 结果是一维数组下的值是obj
    2.查出来的结果,用hasMany()去关联查询article表里的关联字段cate_id查询出属于该分类下的所有数据,用all()方法查多行

    新建mode层Article.php

    <?php
    namespace appmodels;
    
    use yiidbActiveRecord;
    
    class Article extends ActiveRecord
    {
    
    }
    

    新建mode层Category.php

    <?php
    namespace appmodels;
    
    use yiidbActiveRecord;
    
    class Category extends ActiveRecord
    {
    
    }
    

    新建控制器HomeController.php

        public function actionRelatesearch(){
            //关联查询
            //查询方法一(查一行) 一维数组下的值是obj
    //        $category = Category::findOne(1); //查分类表里的数据 abj
    ////        vp($category);
    //        $articles = $category->hasMany(Article::className(),['cate_id'=>'cid'])->asArray()->all();
    //        dd($articles);
    
            //查询方法二(查多行,结果需要提出一行来使用):
            //查分类表里的数据要是一维数组下的值是obj 不能用asArray()转为数组,不然hasMany会报错
            $category = Category::find()->where(['cid'=>2])->all();
            vp($category);
            //关联查询 用hasMany方法前提要满足二个条件
            //1.如果查出来是二维数组需要用角标提成一维数组
            //2.主表category 表查出来的结果要是一维数组下的值是obj 不能用asArray转成数组
            //第一个参数是string article的命名空间app/models/Article == Article::className()
            vp(Article::className()); //打印结果app/models/Article
            //第二个参数是array key是article表里的关联字段 value是category里的主键
            $articles = $category[0]->hasMany(Article::className(),['cate_id'=>'cid'])->asArray()->all();
            dd($articles);
        }
    
    

    打印结果

    D:xampphtdocsyiihelperfunction.php:19:
    array (size=1)
      0 => 
        object(appmodelsCategory)[78]
          private '_attributes' (yiidbBaseActiveRecord) => 
            array (size=2)
              'cid' => string '2' (length=1)
              'c_name' => string '娱乐' (length=6)
          private '_oldAttributes' (yiidbBaseActiveRecord) => 
            array (size=2)
              'cid' => string '2' (length=1)
              'c_name' => string '娱乐' (length=6)
          private '_related' (yiidbBaseActiveRecord) => 
            array (size=0)
              empty
          private '_relationsDependencies' (yiidbBaseActiveRecord) => 
            array (size=0)
              empty
          private '_errors' (yiiaseModel) => null
          private '_validators' (yiiaseModel) => null
          private '_scenario' (yiiaseModel) => string 'default' (length=7)
          private '_events' (yiiaseComponent) => 
            array (size=0)
              empty
          private '_eventWildcards' (yiiaseComponent) => 
            array (size=0)
              empty
          private '_behaviors' (yiiaseComponent) => 
            array (size=0)
              empty
    
    D:xampphtdocsyiihelperfunction.php:19:string 'appmodelsArticle' (length=18)
    
    D:xampphtdocsyiihelperfunction.php:26:
    array (size=4)
      0 => 
        array (size=4)
          'article_id' => string '6' (length=1)
          'article_title' => string 'php的发展前景怎样' (length=24)
          'num' => string '8' (length=1)
          'cate_id' => string '2' (length=1)
      1 => 
        array (size=4)
          'article_id' => string '7' (length=1)
          'article_title' => string '67岁宋春丽与聂远出新戏,满头花发让人心疼,面容慈祥豪爽依旧!' (length=89)
          'num' => string '12' (length=2)
          'cate_id' => string '2' (length=1)
      2 => 
        array (size=4)
          'article_id' => string '8' (length=1)
          'article_title' => string '知微又涨粉啦!《天盛长歌》小衣衣、楚王和金狮王子你会选谁?' (length=87)
          'num' => string '18' (length=2)
          'cate_id' => string '2' (length=1)
      3 => 
        array (size=4)
          'article_id' => string '9' (length=1)
          'article_title' => string '刘亦菲穿旗袍带仙气,赵丽颖穿旗袍优雅,她穿旗袍最有民国范' (length=84)
          'num' => string '14' (length=2)
          'cate_id' => string '2' (length=1)
    
    
    
    [Haima的博客] http://www.cnblogs.com/haima/
  • 相关阅读:
    Django CBV和FBV
    ArcGIS案例学习笔记2_1_山顶点提取最大值提取
    ArcGIS案例学习笔记2_1_学校选址适宜性分析
    ArcGIS案例学习笔记_3_2_CAD数据导入建库
    ArcGIS案例学习笔记3_1_地理配准案例_目视找点
    ArcGIS案例学习笔记3_1_地理配准案例_图面控制点
    ArcGIS案例学习笔记3_1_ArcMap编辑练习
    ArcGIS案例学习笔记4_2_城乡规划容积率计算和建筑景观三维动画
    ArcGIS案例学习笔记4_2_水文分析批处理地理建模
    ArcGIS案例学习笔记4_1_矢量校正
  • 原文地址:https://www.cnblogs.com/haima/p/9595934.html
Copyright © 2020-2023  润新知