石头超人 2017-05-23 16:28:14 5246次浏览 4条评论 3 1 0

首先明白用 AR 模式操作数据库,即一个表得得建一个 model 层

举个简单的例子,文章(art)和分类表(type),model层建art.php 和type.php
代码如下:

<?php
/**
 * @author : Stone
 * User: Administrator
 * Date: 2017/5/23
 * Time: 11:29
 */

namespace frontend\models;
use yii\db\ActiveRecord;

class Type extends ActiveRecord
{

    public static function tableName()
    {
        return 'type';
    }
}

art.php同上

其次分析两表的关系 一个分类下有多个文章,一篇文章只能属于一个分类
分类1 ----->文章1,文章2....

分类2 ------>文章3,文章4....

比如我要查分类为美容下的文章 【一对多】

$type = Type::find()->where(['type'=>'美容'])->one();
$desc = $type->hasMany(Desc::className(),['type_id'=>'t_id'])->asArray()->all();

结果即

Array
(
    [0] => Array
        (
            [id] => 2
            [title] => 女子剪发被迷,割掉器官
            [type_id] => 2
            [content] => 123请问
        )

    [1] => Array
        (
            [id] => 10
            [title] => 大保健
            [type_id] => 2
            [content] => 好大的保健
        )
....

hasMany() 是因为一个分类下有多个文章 如果是根据文章查即为 hasOne(),下面会有讲述
type_id 为文章表的分类 t_id 为分类ID

也可以将 $type->hasMany(Desc::className(),['type_id'=>'t_id'])->asArray()->all() 写入model层
(根据分类产查文章,所以写到分类的model里,即type.php)
代码如下

<?php
/**
 * @author : Stone
 * User: Administrator
 * Date: 2017/5/23
 * Time: 11:29
 */

namespace frontend\models;
use yii\db\ActiveRecord;

class Type extends ActiveRecord
{
    public static function tableName()
    {
        return 'type';
    }
    public function getDesc()
    {
        $desc = $this->hasMany(Desc::className(),['type_id'=>'t_id'])->asArray()->all();
        return $desc;
    }
}

这样,根据分类查文章就出来了.....
接下来为根据文章查分类 (多对一)
代码:

$art = Desc::find()->where(['title'=>'血友病吧被卖时间'])->one();
$type = $art->hasOne(Type::className(),['t_id'=>'type_id'])->asArray()->one();

hsaOne 上面讲到一篇文章只属于一个分类
查询出的结果为

Array ( [t_id] => 1 [type] => 医疗 ) 

这样就根据标题查出来了

最后是两表查询直接展示数据

$desc = Desc::find()->innerJoinWith('type')->orderBy('id asc')->asArray()->all();
// 增:
$info = yii::$app->request->post();   //接到的值
$type = new Type();                   //实例化type model
$type->type = $info['type'];          //
$res = $type->save();                 //添加

// 删:
$id   = yii::$app->request->get('id');
$desc = Desc::findOne($id);
$res  = $desc->delete();
if($res)
{
    return $this->redirect('?r=five/index');
}
        
// 改:	
public function actionUp()
{
    if($_POST)
    {
        $info          = yii::$app->request->post();
        $desc          = new Desc();
        $id = $info['Ceshi']['id'];
        $ss = Desc::findOne($id);
        $ss->title   = $info['Ceshi']['title'];
        $ss->type_id = $info['Ceshi']['type'];
        $ss->content = $info['Ceshi']['content'];
        $res = $ss->save();
        if($res)
        {
            return $this->redirect('?r=five/index');
        }

    }
    else
    {
        $id = yii::$app->request->get('id');
        $model = new Ceshi();
        $desc = Desc::find()->where(['id'=>$id])->asArray()->one();
        $type = Type::find()->asArray()->all();
        $data = array();
        foreach($type as $k=>$v)
        {
            $data[$v['t_id']] = $v['type'];
        }
        return $this->render('up',['model'=>$model,'desc'=>$desc,'data'=>$data]);
    }
}
    
    
    
// 展示(表单小部件)
$model = new Ceshi();    //实例化model
$type = Type::find()->asArray()->all();     //实例化并查询type表
$data = array();
foreach($type as $k=>$v)
{
    $data[$v['t_id']] = $v['type'];
}
// 下拉框数据显示
return $this->render('art_list',['model'=>$model,'data'=>$data]);
觉得很赞
您需要登录后才可以评论。登录 | 立即注册