20.Yii2.0框架多表关联一对多查询之hasMany
[TOC]
新手模式
用上次的查询结果,作为本次的查询条件
//关联查询
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 app\models;
use yii\db\ActiveRecord;
class Article extends ActiveRecord
{
}
新建mode层Category.php
<?php
namespace app\models;
use yii\db\ActiveRecord;
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:\xampp\htdocs\yii\helper\function.php:19:
array (size=1)
0 =>
object(app\models\Category)[78]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=2)
'cid' => string '2' (length=1)
'c_name' => string '娱乐' (length=6)
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=2)
'cid' => string '2' (length=1)
'c_name' => string '娱乐' (length=6)
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_relationsDependencies' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_eventWildcards' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
D:\xampp\htdocs\yii\helper\function.php:19:string 'app\models\Article' (length=18)
D:\xampp\htdocs\yii\helper\function.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/