yii2表关联

1.两张表关联,以“商品表关联品牌表”为例

控制器中:

$goods_model=new Goods();

$goods_info=$goods_model::find()->joinWith('brand')->all();

商品模型中:

//关联品牌表
    public function getbrand(){
        return $this->hasMany(Brand::className(), ['brand_id' => 'goods_brand_id']);
    }

2.三张表关联,以“商品表关联品牌表和分类表”为例

控制器中:

$goods_model=new Goods();

$goods_info=$goods_model::find()->joinWith('brand','category')->orderBy('goods.goods_id desc');

商品模型中:

//关联品牌表

public function getbrand(){

return $this->hasMany(Brand::className(),['brand_id'=>'goods_brand_id']);

}

//关联类别表

public function getcategory(){

return $this->hasMany(Category::className(),['category_id'=>'goods_category_id']);

}

yii2表关联实例的更多相关文章

  1. Yii2中多表关联查询(join、joinwith)

    我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order      (id  order_name ...

  2. Yii2.0中文开发向导——Yii2中多表关联查询(join、joinwith)(转)

    我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order          (id  order_ ...

  3. Yii2.0中文开发向导——Yii2中多表关联查询(join、joinwith)

    我们用实例来说明这一部分 表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order          (id  order_ ...

  4. yii2 ActiveRecord多表关联以及多表关联搜索的实现

    作者:白狼 出处:http://www.manks.top/yii2_many_ar_relation_search.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明 ...

  5. [moka同学笔记]Yii2中多表关联查询(join、joinwith) (摘录)

    表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order          (id  order_name       cu ...

  6. RDIFramework.NET 中多表关联查询分页实例

    RDIFramework.NET 中多表关联查询分页实例 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,给用户和开发者最佳的.Net框架部署方案.该框架以SOA范式作为 ...

  7. Yii2中多表关联查询(hasOne、hasMany、join、joinwith)

    表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order      (id  order_name  customer_id ...

  8. Yii2中多表关联查询(with、join、joinwith)

    表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order         (id  order_name   custome ...

  9. 后盾网lavarel视频项目---lavarel多表关联一对多操作实例

    后盾网lavarel视频项目---lavarel多表关联一对多操作实例 一.总结 一句话总结: 1.一对多中多那个部分的数据前端通过json弄到服务器 2.所有通过一操作多的时候,都要用上模型中定义的 ...

随机推荐

  1. JavaScript实现在页面上的文本框中输入小写字母自动变为大写字母

    <script language="javascript" type="text/javascript"> $(function () { $(&q ...

  2. MVC强类型和弱类型的区别

    1 强类型的处理 首先必须要有一个对象的实体类,UserINfo就是一个实体类,如下: public class UserInfo() { public  int Id{set;get;} publi ...

  3. 个人常用iOS第三方库以及XCode插件介绍

    第三方库 CocoaPod CocoaPod并不是iOS上的第三方库 而是大名鼎鼎的第三方库的管理工具 在CocoaPod没有出现之前 第三方库的管理是非常痛苦的 尤其是一些大型的库(比如nimbus ...

  4. Oracle一个实例配置多个监听

    要想给一个Oracle实例配置多个监听,首先要定义多个监听器,因为是多个监听,势必会有一些监听端口不是1521. 现在服务端的listener.ora文件中定义如下监听器: LISTENER = (D ...

  5. Oracle利用external table 查看trace文件

    1. 用下面的语句找到trace文件的路径 select * from v$diag_info where name='Default Trace File'; 2. 创建一个directory用来加 ...

  6. Python开发【第二章】:Python的数据类型

    基本数据类型 一.整型 如: 18.73.84 整型具备如下功能: class int(object): """ int(x=0) -> int or long i ...

  7. Android 关于ExpandableListView去掉里头的分割线

    关于ExpandableListView去掉里面的分割线关于ExpandableListView,自己写了个类继承自BaseExpandableListAdaptergroups,childs都弄好了 ...

  8. php 读取webservice接口

    开启 extension = php_soap.dll extension = php_curl.dll extension = php_openssl.dll <script src=&quo ...

  9. Could not find class XXX referenced from method XXX.<YYY>

    Since some ADT-Version you have to set which libraries / projects should be exported too. Project-Pr ...

  10. ThinkPHP 自动验证与自动填充无效可能的原因(转)

    自动验证与自动填充是在使用ThinkPHP时经常用到的功能,但偶尔会遇到自动验证与自动填充无效的情况,本文就ThinkPHP 自动验证与自动填充无效可能的原因做一些分析. create() Think ...