Yii多表联合查询操作详解

本文针对Yii多表联查进行汇总描述,供大家参考,具体内容如下

1、多表联查实现方法

有两种方式一种使用DAO写SQL语句实现,这种实现理解起来相对轻松,只要保证SQL语句不写错就行了。缺点也很明显,比较零散,而且不符合YII的推荐框架,最重要的缺点在于容易写错。

还有一种便是下面要说的使用YII自带的CActiveRecord实现多表联查

2、 整体框架

我们需要找到一个用户的好友关系,用户的信息放在用户表中,用户之间的关系放在关系表中,而关系的内容则放在关系类型表中。明显的我们只需要以关系表为主表联查其他两个表即可。我主要从代码的角度,分析下实现的过程。

3、CActiveRecord

我们首先需要对3张表建立相应的model,下面是关系表的代码

SocialRelation.php

<?php 

/**
 * This is the model class for table "{{social_relation}}".
 *
 * The followings are the available columns in table '{{social_relation}}':
 * @property integer $relation_id
 * @property integer $relation_type_id
 * @property integer $user_id
 * @property integer $another_user_id
 *
 * The followings are the available model relations:
 * @property SocialRelationType $relationType
 * @property AccessUser $user
 * @property AccessUser $anotherUser
 */
class SocialRelation extends CActiveRecord
{
  /**
   * Returns the static model of the specified AR class.
   * @param string $className active record class name.
   * @return SocialRelation the static model class
   */
  public static function model($className=__CLASS__)
  {
    return parent::model($className);
  } 

  /**
   * @return string the associated database table name
   */
  public function tableName()
  {
    return '{{social_relation}}';
  } 

  /**
   * @return array validation rules for model attributes.
   */
  public function rules()
  {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
      array('relation_type_id, user_id, another_user_id', 'numerical', 'integerOnly'=>true),
      // The following rule is used by search().
      // Please remove those attributes that should not be searched.
      array('relation_id, relation_type_id, user_id, another_user_id', 'safe', 'on'=>'search'),
    );
  } 

  /**
   * @return array relational rules.
   */
  public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
      'relationType' => array(self::BELONGS_TO, 'SocialRelationType', 'relation_type_id'),
      'user' => array(self::BELONGS_TO, 'AccessUser', 'user_id'),
      'anotherUser' => array(self::BELONGS_TO, 'AccessUser', 'another_user_id'),
    );
  } 

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels()
  {
    return array(
      'relation_id' => 'Relation',
      'relation_type_id' => 'Relation Type',
      'relation_type_name' => 'Relation Name',
      'user_id' => 'User ID',
      'user_name' => 'User Name',
      'another_user_id' => 'Another User',
      'another_user_name' => 'Another User Name',
    );
  } 

  /**
   * Retrieves a list of models based on the current search/filter conditions.
   * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
   */
  public function search()
  {
    // Warning: Please modify the following code to remove attributes that
    // should not be searched. 

    $criteria=new CDbCriteria; 

    $criteria->compare('relation_id',$this->relation_id);
    $criteria->compare('relation_type_id',$this->relation_type_id);
    $criteria->compare('user_id',$this->user_id);
    $criteria->compare('another_user_id',$this->another_user_id);
    $criteria->with=array(
      'relationType',
    ); 

    return new CActiveDataProvider($this, array(
      'criteria'=>$criteria,
    ));
  }
} 

为了描述方便我们约定 主表为A表(执行查询的那个表), 引用表为B表(外键所引用的表)
建议使用Gii自动生成模型,这样能够节省大量时间,为了测试方便,可以对主表生成CRUD,就是增删改查页面,其他的引用表只用生成model就行了。
1. model函数、tablename函数用于得到这个模型和得到数据库表基本信息。自动生成无需修改

2.rules函数,这个函数主要用于规定参数检验方式,注意即使有些参数不需要校验,也必须出现在rules中。不然模型将无法得到参数

3.relation函数,这个函数十分关键,用于定义表之间的关系,下面我将详细说明其中含义

'relationType' => array(self::BELONGS_TO, 'SocialRelationType', 'relation_type_id')  
 这句代码中结构如下
'VarName'=>array('RelationType', 'ClassName', 'ForeignKey', ...additional options)
VarName 是关系的名字,我们以后会用这个名字访问外键引用表的字段

RelationType是关系的类型,十分重要,如果设定错误会导致一些奇怪而且难以检查的错误,Yii一共提供了4种关系

BELONGS_TO(属于): 如果表 A 和 B 之间的关系是一对多,则 表 B 属于 表 A
HAS_MANY(有多个): 如果表 A 和 B 之间的关系是一对多,则 A 有多个 B
HAS_ONE(有一个): 这是 HAS_MANY 的一个特例,A 最多有一个 B
MANY_MANY: 这个对应于数据库中的 多对多关系
ClassName是引用表名,就是外键所引用的表的名字,也就是B表表名

ForeignKey是外键名,主要这里填写的是外键在主表中的名字,也就是外键在A表中的表名,切记不要填错了

如果B表中是双主键可以采用下列方式实现,从软件工程的角度不推荐这样的做法,每个表最好使用独立无意义主键,不然容易出现各种问题,而且不方便管理

'categories'=>array(self::MANY_MANY, 'Category',
        'tbl_post_category(post_id, category_id)'),

additional option 附加选项,很少用到
4 attributeLabels函数,这就是表属性的显示名称了,有点点像powerdesigner中code和name的关系前面一部分为数据库字段名,后面一部分为显示名称
5 search函数,用于生成表查询结果的函数,可以在此加一些限制条件,具体的使用方法就不在这里说明了,可以参考API中CDbCriteria的讲解。如果使用Gii生成那么不需要怎么修改。

同理我们生成,剩下的两个引用表

关系类型表:SocialRelationType.php

<?php 

/**
 * This is the model class for table "{{social_relation_type}}".
 *
 * The followings are the available columns in table '{{social_relation_type}}':
 * @property integer $relation_type_id
 * @property string $relation_type_name
 *
 * The followings are the available model relations:
 * @property SocialRelation[] $socialRelations
 */
class SocialRelationType extends CActiveRecord
{
  /**
   * Returns the static model of the specified AR class.
   * @param string $className active record class name.
   * @return SocialRelationType the static model class
   */
  public static function model($className=__CLASS__)
  {
    return parent::model($className);
  } 

  /**
   * @return string the associated database table name
   */
  public function tableName()
  {
    return '{{social_relation_type}}';
  } 

  /**
   * @return array validation rules for model attributes.
   */
  public function rules()
  {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
      array('relation_type_name', 'length', 'max'=>10),
      // The following rule is used by search().
      // Please remove those attributes that should not be searched.
      array('relation_type_id, relation_type_name', 'safe', 'on'=>'search'),
    );
  } 

  /**
   * @return array relational rules.
   */
  public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
      'socialRelations' => array(self::HAS_MANY, 'SocialRelation', 'relation_type_id'),
    );
  } 

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels()
  {
    return array(
      'relation_type_id' => 'Relation Type',
      'relation_type_name' => 'Relation Type Name',
    );
  } 

  /**
   * Retrieves a list of models based on the current search/filter conditions.
   * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
   */
  public function search()
  {
    // Warning: Please modify the following code to remove attributes that
    // should not be searched. 

    $criteria=new CDbCriteria; 

    $criteria->compare('relation_type_id',$this->relation_type_id);
    $criteria->compare('relation_type_name',$this->relation_type_name,true); 

    return new CActiveDataProvider($this, array(
      'criteria'=>$criteria,
    ));
  }
}

用户表:AccessUser.php

<?php 

/**
 * This is the model class for table "{{access_user}}".
 *
 * The followings are the available columns in table '{{access_user}}':
 * @property integer $id
 * @property string $name
 * @property string $password
 * @property string $lastlogin
 * @property string $salt
 * @property string $email
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property SocialRelation[] $socialRelations
 * @property SocialRelation[] $socialRelations1
 */
class AccessUser extends CActiveRecord
{
  /**
   * Returns the static model of the specified AR class.
   * @param string $className active record class name.
   * @return AccessUser the static model class
   */
  public static function model($className=__CLASS__)
  {
    return parent::model($className);
  } 

  /**
   * @return string the associated database table name
   */
  public function tableName()
  {
    return '{{access_user}}';
  } 

  /**
   * @return array validation rules for model attributes.
   */
  public function rules()
  {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
      array('status', 'numerical', 'integerOnly'=>true),
      array('name, password, salt, email', 'length', 'max'=>255),
      array('lastlogin', 'safe'),
      // The following rule is used by search().
      // Please remove those attributes that should not be searched.
      array('id, name, password, lastlogin, salt, email, status', 'safe', 'on'=>'search'),
    );
  } 

  /**
   * @return array relational rules.
   */
  public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
      'user_name' => array(self::HAS_MANY, 'SocialRelation', 'user_id'),
      'anotherUser_name' => array(self::HAS_MANY, 'SocialRelation', 'another_user_id'),
    );
  } 

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels()
  {
    return array(
      'id' => 'ID',
      'name' => 'Name',
      'password' => 'Password',
      'lastlogin' => 'Lastlogin',
      'salt' => 'Salt',
      'email' => 'Email',
      'status' => 'Status',
    );
  } 

  /**
   * Retrieves a list of models based on the current search/filter conditions.
   * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
   */
  public function search()
  {
    // Warning: Please modify the following code to remove attributes that
    // should not be searched. 

    $criteria=new CDbCriteria; 

    $criteria->compare('id',$this->id);
    $criteria->compare('name',$this->name,true);
    $criteria->compare('password',$this->password,true);
    $criteria->compare('lastlogin',$this->lastlogin,true);
    $criteria->compare('salt',$this->salt,true);
    $criteria->compare('email',$this->email,true);
    $criteria->compare('status',$this->status); 

    return new CActiveDataProvider($this, array(
      'criteria'=>$criteria,
    ));
  }
}

4、Controller
三张表介绍完了后,下面就应当介绍Controller了,同样的我们使用Gii生成主表(A表)的CRUD后就能得到controller,我们只需要对其进行一些修改即可,代码如下

SocialRelationController.php

<?php 

class SocialRelationController extends Controller
{
  /**
   * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
   * using two-column layout. See 'protected/views/layouts/column2.php'.
   */
  public $layout='//layouts/column2'; 

  /**
   * @return array action filters
   */
  public function filters()
  {
    return array(
      'accessControl', // perform access control for CRUD operations
      'postOnly + delete', // we only allow deletion via POST request
    );
  } 

  /**
   * Specifies the access control rules.
   * This method is used by the 'accessControl' filter.
   * @return array access control rules
   */
  public function accessRules()
  {
    return array(
      array('allow', // allow all users to perform 'index' and 'view' actions
        'actions'=>array('index','view'),
        'users'=>array('*'),
      ),
      array('allow', // allow authenticated user to perform 'create' and 'update' actions
        'actions'=>array('create','update'),
        'users'=>array('@'),
      ),
      array('allow', // allow admin user to perform 'admin' and 'delete' actions
        'actions'=>array('admin','delete'),
        'users'=>array('admin'),
      ),
      array('deny', // deny all users
        'users'=>array('*'),
      ),
    );
  } 

  /**
   * Displays a particular model.
   * @param integer $id the ID of the model to be displayed
   */
  public function actionView($id)
  {
    $this->render('view',array(
      'model'=>$this->loadModel($id),
    ));
  } 

  /**
   * Creates a new model.
   * If creation is successful, the browser will be redirected to the 'view' page.
   */
  public function actionCreate()
  {
    $model=new SocialRelation; 

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model); 

    if(isset($_POST['SocialRelation']))
    {
      $model->attributes=$_POST['SocialRelation'];
      if($model->save())
        $this->redirect(array('view','id'=>$model->relation_id));
    } 

    $this->render('create',array(
      'model'=>$model,
    ));
  } 

  /**
   * Updates a particular model.
   * If update is successful, the browser will be redirected to the 'view' page.
   * @param integer $id the ID of the model to be updated
   */
  public function actionUpdate($id)
  {
    $model=$this->loadModel($id); 

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model); 

    if(isset($_POST['SocialRelation']))
    {
      $model->attributes=$_POST['SocialRelation'];
      if($model->save())
        $this->redirect(array('view','id'=>$model->relation_id));
    } 

    $this->render('update',array(
      'model'=>$model,
    ));
  } 

  /**
   * Deletes a particular model.
   * If deletion is successful, the browser will be redirected to the 'admin' page.
   * @param integer $id the ID of the model to be deleted
   */
  public function actionDelete($id)
  {
    $this->loadModel($id)->delete(); 

    // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
    if(!isset($_GET['ajax']))
      $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
  } 

  /**
   * Lists all models.
   */
  public function actionIndex()
  {
    if(Yii::app()->user->id != null){
      $dataProvider=new CActiveDataProvider(
        'SocialRelation',
        array('criteria'=>array('condition'=>'user_id='.Yii::app()->user->id,
      ))
      );
      $this->render('index',array(
        'dataProvider'=>$dataProvider,
      ));
    } 

  } 

  /**
   * Manages all models.
   */
  public function actionAdmin()
  {
    $model=new SocialRelation('search');
    $model->unsetAttributes(); // clear any default values
    if(isset($_GET['SocialRelation']))
      $model->attributes=$_GET['SocialRelation']; 

    $this->render('admin',array(
      'model'=>$model,
    ));
  } 

  /**
   * Returns the data model based on the primary key given in the GET variable.
   * If the data model is not found, an HTTP exception will be raised.
   * @param integer $id the ID of the model to be loaded
   * @return SocialRelation the loaded model
   * @throws CHttpException
   */
  public function loadModel($id)
  {
    $model=SocialRelation::model()->findByPk($id);
    if($model===null)
      throw new CHttpException(404,'The requested page does not exist.');
    return $model;
  } 

  /**
   * Performs the AJAX validation.
   * @param SocialRelation $model the model to be validated
   */
  protected function performAjaxValidation($model)
  {
    if(isset($_POST['ajax']) && $_POST['ajax']==='social-relation-form')
    {
      echo CActiveForm::validate($model);
      Yii::app()->end();
    }
  }
}

简单介绍下其中各个函数和变量
$layout 就是布局文件的位置了,布局文件如何使用,这里不做讨论

filters 定义过滤器,这里面水很深

accessRules 访问方式,就是那些用户能够访问到这个模块

array('allow', // allow all users to perform 'index' and 'view' actions
        'actions'=>array('index','view'),
        'users'=>array('*'),
      ),

allow 表示允许访问的规则如下,deny表示拒绝访问的规则如下。
action表示规定规则使用的动作

user表示规则适用的用户群组,*表示所有用户,@表示登录后的用户,admin表示管理员用户

actionXXX 各个action函数

这里值得注意的是 这个函数

public function actionIndex()
  {
    if(Yii::app()->user->id != null){
      $dataProvider=new CActiveDataProvider(
        'SocialRelation',
        array('criteria'=>array('condition'=>'user_id='.Yii::app()->user->id,
      ))
      );
      $this->render('index',array(
        'dataProvider'=>$dataProvider,
      ));
    } 

  }

其中我们可以在dataProvider中设置相应的查询条件,注意这里设置是对于主表(A表)进行的,用的字段名也是主表中的,因为我们要显示的是当前用户的好友,于是,这里我们使用Yii::app()->user->id取得当前用户的id 。

loadModel 用于装载模型,这里我们可以看到findByPk查询了数据库。

performAjaxValidation 用于Ajax验证。

5、视图View

index.php

<?php
/* @var $this SocialRelationController */
/* @var $dataProvider CActiveDataProvider */ 

$this->breadcrumbs=array(
  'Social Relations',
);
?> 

<h1>Social Relations</h1> 

<?php $this->widget('zii.widgets.CListView', array(
  'dataProvider'=>$dataProvider,
  'itemView'=>'_view',
)); ?>

我们使用一个 CListView控件进行显示,其中itemView为内容显示的具体表单,dataProvider这个是内容源,我们在controller中已经设定了。

_view.php

<?php
/* @var $this SocialRelationController */
/* @var $data SocialRelation */
?> 

<div class="view"> 

  <b><?php echo CHtml::encode($data->getAttributeLabel('relation_id')); ?>:</b>
  <?php echo CHtml::link(CHtml::encode($data->relation_id), array('view', 'id'=>$data->relation_id)); ?>
  <br /> 

  <b><?php echo CHtml::encode($data->getAttributeLabel('relation_type_id')); ?>:</b>
  <?php echo CHtml::encode($data->relation_type_id); ?>
  <br /> 

  <b><?php echo CHtml::encode($data->getAttributeLabel('relation_type_name')); ?>:</b>
  <?php
    echo $data->relationType->relation_type_name;
  ?>
  <br /> 

  <b><?php echo CHtml::encode($data->getAttributeLabel('user_id')); ?>:</b>
  <?php echo CHtml::encode($data->user_id); ?>
  <br /> 

  <b><?php echo CHtml::encode($data->getAttributeLabel('user_name')); ?>:</b>
  <?php
    echo $data->user->name;
  ?>
  <br /> 

  <b><?php echo CHtml::encode($data->getAttributeLabel('another_user_id')); ?>:</b>
  <?php echo CHtml::encode($data->another_user_id); ?>
  <br /> 

  <b><?php echo CHtml::encode($data->getAttributeLabel('another_user_name')); ?>:</b>
  <?php
    echo $data->anotherUser->name;
  ?>
  <br /> 

</div>

主要都是类似的,我们看其中的一条

代码如下:

<b><?php echo CHtml::encode($data->getAttributeLabel('relation_type_name')); ?>:</b> 
<?php echo $data->relationType->relation_type_name; ?>

第一行为显示标签,在模型中我们设定的显示名就在这里体现出来
第二行为内容显示,这里的relationType是在模型中设置的关系名字,后面的relation_type_name是引用表的字段名(B表中的名字)

6、总结

通过上面的步骤,我们就实现了整个联合查询功能,效果图如下所示:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Yii框架参数化查询中IN查询只能查询一个的解决方法

    本文实例讲述了Yii框架参数化查询中IN查询只能查询一个的解决方法.分享给大家供大家参考,具体如下: 在yii框架中使用参数化进行IN查询时,结果不如所愿 $sql =<<<SQL SELECT id FROM tb WHERE id IN(:ids) SQL; $db = GeneralService::getSlaveDB(); $result = $db->createCommand($sql)->query([':ids' => '1013,1015,1017'

  • Yii2增删改查之查询 where参数详细介绍

    概述 由于官方手册关于where的介绍比较少,所以想自己整理一下,以便大家的学习和自己回头查询.本篇文章会详细介绍and.or.between.in.like在where方法中的使用方法和举例. and // 我们要查询id大于1并且小于3的数据 $userInfo = User::find()->where(['and' , 'id > 1' , 'id < 3'])->all(); // 或者用以下方式,更为安全 $userInfo = User::find()->whe

  • Yii2实现跨mysql数据库关联查询排序功能代码

    背景:在一个mysql服务器上(注意:两个数据库必须在同一个mysql服务器上)有两个数据库: memory (存储常规数据表) 中有一个 user 表(记录用户信息) memory_stat (存储统计数据表) 中有一个 user_stat (记录用户统计数据) 现在在 user 表生成的 GridView 列表中展示 user_stat 中的统计数据 只需要在User的model类中添加关联 public function getStat() { return $this->hasOne(U

  • 详解YII关联查询

    一.多表关联的配置 在我们使用 AR 执行关联查询之前,我们需要让 AR 知道一个 AR 类是怎样关联到另一个的. 两个 AR 类之间的关系直接通过 AR 类所代表的数据表之间的关系相关联. 从数据库的角度来说,表 A 和 B 之间有三种关系:一对多(one-to-many,例如 tbl_user 和 tbl_post),一对一( one-to-one 例如 tbl_user 和 tbl_profile)和 多对多(many-to-many 例如 tbl_category 和 tbl_post)

  • Yii基于数组和对象的Model查询技巧实例详解

    本文实例讲述了Yii基于数组和对象的Model查询技巧.分享给大家供大家参考,具体如下: 对于一个Model Post 有如下的4中查询方法,返回对象或者对象数组. //查找满足指定条件的结果中的第一行 find the first row satisfying the specified condition $post=Post::model()->find($condition,$params); //查找具有指定主键值的那一行 find the row with the specified

  • Yii2中关联查询简单用法示例

    本文实例讲述了Yii2中关联查询用法.分享给大家供大家参考,具体如下: 有两张表,post和category,post.cate_id对应category.id 使用Gii上升这两张表的model 然后post的model中有如下代码 public function getCate() { return $this->hasOne(Category::className(), ['id' => 'cate_id']); } 在post这个model最下面在添加如下方法即可获取关联表内容 pub

  • Yii框架关联查询with用法分析

    本文实例分析了Yii框架关联查询with用法.分享给大家供大家参考.具体方法如下: Yii框架关联查询与mysql中的关联查询会有什么区别呢?这里小编就与各位来一起来看看吧. Yii的关联查询确实是一个方便的东西,网上的资料也很多,但是大部分都是Ctrl+c,Ctrl+v,有些东西一直没有人出来详细的写篇文章说明一下,在参考了网上很多资源以后,加上自己的的一些理解,写下了这篇文章,给广大初学者朋友们提供一点个人见解. YII 支持四种类型的关系: BELONGS_TO(属于): 如果表 A 和

  • Yii的CDbCriteria查询条件用法实例

    本文实例总结了一些Yii的CDbCriteria查询条件用法,分享给大家供大家参考.具体分析如下: 这里就是Yii中使用CDbCriteria方法来进行查询的各种条件: 复制代码 代码如下: $criteria = new CDbCriteria; $criteria->addCondition("MACID=464"); //查询条件,即where id = 1 $criteria->addInCondition('id', array(1,2,3,4,5)); //代表

  • YII2数据库查询实践

    初探yii2框架,对增删改查,关联查询等数据库基本操作的简单实践. 数据库配置. /config/db.php 进行数据库配置 实践过程中有个test库->test表->两条记录如下 mysql> select * from test; +----+--------+ | id | name | +----+--------+ | 1 | zhuai | | 2 | heng | +----+--------+ 18 rows in set (0.00 sec) sql 查询方式 yii

  • Yii查询生成器(Query Builder)用法实例教程

    本文为yii官网英文文档的翻译版本,主要介绍了Yii查询生成器(Query Builder)的用法.分享给大家供大家参考之用.具体如下: 首先,Yii的查询生成器提供了用面向对象的方式写SQL语句.它允许开发人员使用类的方法和属性来指定一个SQL语句的各个部分.然后,组装成一个有效的SQL语句,可以通过调用DAO数据访问对象的描述方法为进一步执行.以下显示了一个典型的使用查询生成器建立一个select语句: $user = Yii::app()->db->createCommand() -&g

  • Yii中的relations数据关联查询及统计功能用法详解

    本文实例讲述了Yii中的relations数据关联查询及统计功能用法.分享给大家供大家参考,具体如下: 关联查询,Yii 也支持所谓的统计查询(或聚合查询). 它指的是检索关联对象的聚合信息,例如每个 post 的评论的数量,每个产品的平均等级等. 统计查询只被 HAS_MANY(例如,一个 post 有很多评论) 或 MANY_MANY (例如,一个 post 属于很多分类和一个 category 有很多 post) 关联对象执行. 执行统计查询非常类似于之前描述的关联查询.我们首先需要在 C

  • Yii2中使用join、joinwith多表关联查询

    表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer (id customer_name) 订单表Order (id order_name customer_id book_id) 图书表 (id book_name author_id) 作者表 (id author_name) 模型定义 下面是这4个个模型的定义,只写出其中的关联 Customer class Customer extends \yii\db\ActiveRecord { // 这是获取客户的订单,由上面我们

  • yii数据库的查询方法

    本文实例讲述了yii数据库的查询方法.分享给大家供大家参考,具体如下: 这里介绍两种查询方法.一种是直接查询,一种是使用借助criteria实现查询. 复制代码 代码如下: $user=User::model(); 1. 直接查询: $arr=array( "select"=>"username,password,email", //要查询的字段 "condition"=>"username like '%6'",

随机推荐