先看数据表中的关系是怎样的:

customer表中的关系如下:

order中的表为:

先在customer中获得order的数据,并与之关联,在 helloController.php 中,代码如下

 <?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Customer;
use app\models\Order; class HelloController extends Controller {
public function actionIndex()
{
$customer = Customer::find()->where(['name'=>'zhangsan'])->one();
// $orders=$customer->getOrders();//getOrders()是在cunstomer.php中的函数,与下面代码效果一致
$orders = $customer->orders;//只用orders而不声明getOrders()是因为程序会自动使用__get()方法,调用Customer.php中的getOrders()方法。
print_r($orders);
}
}

在 app\models\Customer.php 中的代码为:

 <?php
namespace app\models; use yii\db\ActiveRecord;
class Customer extends ActiveRecord{
public function getOrders(){
$orders = $this->hasMany(Order::className(),['customer_id'=>'id'])->asArray();//Order::className()等同于'app\madels\Order'是使用Order.php下面的方法,其中'customer_id'='id'中的id是customer表中id,而不是order表中的id
return $orders;
}
}

在 app\models\Order.php 中的代码为:

 <?php
namespace app\models; use yii\db\ActiveRecord;
class Order extends ActiveRecord{ }

程序打印出的效果如图:

然后在order中获得customer中的数据,再与之关联。

helloController中的代码为:

 <?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Customer;
use app\models\Order; class HelloController extends Controller {
public function actionIndex()
{
$order = Order::find()->where(['id'=>1])->one();
$customer = $order->customer;
print_r($customer);
}
}

order.php中的代码:

 <?php
namespace app\models; use yii\db\ActiveRecord;
class Order extends ActiveRecord{
public function getCustomer() {
$customer= $this->hasOne(Customer::className(),['id'=>'customer_id'])->asArray();
return $customer;
}
}

然后在customer.php中的代码为:

 <?php
namespace app\models; use yii\db\ActiveRecord;
class Customer extends ActiveRecord{ }

效果图为:

YII的关联查询的更多相关文章

  1. yii 之数据库关联查询

    <?php namespace app\controllers; use yii\web\Controller; use app\models\Customer; class CustomerC ...

  2. YII关联查询

    原文链接:http://keshion.iteye.com/blog/1607994 一.多表关联的配置 在我们使用 AR 执行关联查询之前,我们需要让 AR 知道一个 AR 类是怎样关联到另一个的. ...

  3. Yii关联查询(转载)

    原文链接:http://keshion.iteye.com/blog/1607994 一.多表关联的配置 在我们使用 AR 执行关联查询之前,我们需要让 AR 知道一个 AR 类是怎样关联到另一个的. ...

  4. yii2中关联查询

    yii2 ActiveRecord多表关联以及多表关联搜索的实现 一个老生常谈的问题.最近通过群里的反馈,觉得很多人还是没有去理解这个问题.今天把这个问题讲明白了,看看yii2 ActiveRecor ...

  5. Yii2中多表关联查询

    准备条件: 1.首先准备两张表: customer(用户表)(id, name) order(订单表)(id, customer_id, price) customer 表和 order 表之间是一对 ...

  6. Yii Ar model 查询

    Ar model 查询 参照表: CREATE TABLE tbl_user ( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, username VA ...

  7. JDBC MySQL 多表关联查询查询

    public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...

  8. MYSQL基础操作之数据约束与关联查询

    一.MYSQL约束 1.默认值约束,当字段没有插入值的时候,mysql自动给该字段分配默认值. 默认值的字段允许为空. 对默认值字段也可以插入null. CREATE TABLE STUDENT( I ...

  9. C#代码中实现两个表(DataTable)的关联查询(JOIN)

    之前通常都是使用SQL直接从数据库中取出表1和表2关联查询后的数据,只需要用一个JOIN就可以了,非常方便.近日遇到一种情况,两个表中的数据已经取到代码中,需要在代码中将这两个表关联起来,并得到它们横 ...

随机推荐

  1. ASP+Access UTF-8 网页乱码问题解决办法

    用ACCESS数据库和ASP做网站时用UTF-8编码有时会出现乱码,再者网页出错或者刷新页面后就是乱码,如果数据库取值乱码在开头加上<%@LANGUAGE="VBSCRIPT" ...

  2. webservice cxf error:类的两个属性具有相同名称 "password"

    execption detail: Caused by: javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.Servic ...

  3. 【转载】PostgreSQL分区表(Table Partitioning)应用

    博客地址--点击

  4. CI框架分页类

    分页类1.分页类参数说明 'base_url' => 指向你的分页所在的控制器类/方法的完整的 URL, 'total_rows' => 数据的总行数, 'per_page' => ...

  5. C/C++获取数组的长度

    C.C++中没有提供 直接获取数组长度的函数,对于存放字符串的字符数组提供了一个strlen函数获取长度,那么对于其他类型的数组如何获取他们的长度呢?其中一种方法是使 用sizeof(array) / ...

  6. [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...

  7. 【20160924】GOCVHelper MFC增强算法(3)

        //获得当前目录路径     static CString GetLocalPath(){         CString csCfgFilePath;         GetModuleFi ...

  8. Linux 文件操作总结

    http://blog.163.com/he_junwei/blog/static/19793764620152592737741/ ioctl?? lseek?? 文件是linux中的一个重要概念. ...

  9. android post请求

    参考文章:http://blog.csdn.net/lotusyangjun/article/details/22292445 http://blog.csdn.net/withiter/articl ...

  10. phpcms 04

    首页index.html 首页头条推荐 <div class="col-left"> <div class="news-hot"> &l ...