php yii多表查询

一个Company记录可以对应多个CompanyUser纪录
Company表:

[['id', 'nature_id', 'scale_id', 'pro_id', 'created_at', 'updated_at'], 'integer'],
[['id','company_name'], 'required'],
[['company_logo','company_desc','company_name','nature','scale','pro'], 'string']

public function getCompanyuser()
{
return $this->hasMany(CompanyUser::className(), ['company_id' => 'id']);
}


CompanyUser表
[['id', 'company_id', 'user_id'], 'integer']
关系

public function getCompany()
{
return $this->hasOne(Company::className(), ['id' => 'company_id']);
}
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}

CompanyController.php

function actionIndex() {
$user_id = \Yii::$app->user->id;
$models = Company::find()->innerJoinWith(['companyuser'])->where(['user_id'=>$user_id])->orderBy(['created_at' => SORT_DESC])->asArray()->all();
$data = [];
foreach ($models as $model) {
$data[] = $model;
}
return json_encode($data);
}
这样可以根据companyuser表的user_id设置条件查询

-----------------------------------------------------------------------------------

SellController.php
$models = BuyRecord::find()->innerJoinWith(['d'=>function($query) {$query->andWhere('tp=1');}])->with('u')->where(['user_id_sell' => $user_id, 'status'=>1,'data_id'=>$dataid])->orderBy(['created_at' => SORT_DESC])->asArray()->all();

BuyRecord.php

public function getU()
{
return $this->hasOne(U::className(), ['id' => 'user_id']);
}
public function getD()
{
return $this->hasOne(Data::className(), ['id' => 'data_id']);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544077.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ruby hash方法_Ruby中带有示例的Hash.rehash方法

ruby hash方法Hash.rehash方法 (Hash.rehash Method) In this article, we will study about Hash.rehash Method. The working of the method cant be assumed because of its quite a different name. Let us read its definition and understand its implementation with th…

面试突击63:distinct 和 group by有什么区别?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)在 MySQL 中,最常见的去重方法有两个:使用 distinct 或使用 group by,那它们有什…

从20s优化到500ms,我用了这三招

前言接口性能问题,对于从事后端开发的同学来说,是一个绕不开的话题。想要优化一个接口的性能,需要从多个方面着手。本文将接着接口性能优化这个话题,从实战的角度出发,聊聊我是如何优化一个慢查询接口的。上周我优化了…

camelcase_在Python中将字符串转换为camelCase

camelcaseHere, we are implementing a python program to convert a given string to camelCase. 在这里,我们正在实现一个python程序,将给定的字符串转换为camelCase。 Example of camelCase: camelCase的示例: String: "Hello worl…

面试拆解:系统上线后CPU急速飙升,该怎么排查?

上次面试官问了个问题:应用上线后Cpu使用率飙升如何排查?其实这是个很常见的问题,也非常简单,那既然如此我为什么还要写呢?因为上次回答的时候我忘记将线程PID转换成16进制的命令了。所以我决定再重温一遍这个问题&…

提高Python运行效率的六个窍门

Python性能优化的20条建议 http://segmentfault.com/a/1190000000666603优化算法时间复杂度 算法的时间复杂度对程序的执行效率影响最大,在Python中可以通过选择合适的数据结构来优化时间复杂度,如list和set查找某一个元素的时间复杂度分别是O(n)和O(1)。…

ruby hash方法_Hash.fetch()方法以及Ruby中的示例

ruby hash方法Hash.fetch()方法 (Hash.fetch() Method) In this article, we will study about Hash.fetch() Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method wi…

MySQL 死锁了,怎么办?

作者:小林coding提纲如下:正文有个业务主要逻辑就是新增订单、修改订单、查询订单等操作。然后因为订单是不能重复的,所以当时在新增订单的时候做了幂等性校验,做法就是在新增订单记录之前,先通过 select ... for upda…

lcfirst_PHP lcfirst()函数与示例

lcfirstPHP lcfirst()函数 (PHP lcfirst() function) lcfirst() function is a string function, it is used to convert first character to lowercase. It accepts string and returns string with first lowercase character. lcfirst()函数是一个字符串函数,用于…

在notepad++中运行python代码

#在notepad中运行python代码1、安装插件pyNPP, 2、允许插件pyNPP中的第一个和第二个选项即可,如果代码过少代码执行一闪而过,可能无法看到,可加入少量sleep时间即可 方法二:1、安装插件NppExec2、打开NppExec--Execute…

10 张图搞懂服务注册发现机制

在微服务架构或分布式环境下,服务注册与发现技术不可或缺,这也是程序员进阶之路必须要掌握的核心技术之一,本文通过图解的方式带领大家轻轻松松掌握。引入服务注册与发现组件的原因先来看一个问题,假如现在我们要做一个商城项目&a…

c# datetime._C#| DateTime.GetHashCode()方法与示例

c# datetime.DateTime.GetHashCode()方法 (DateTime.GetHashCode() Method) DateTime.GetHashCode() method is used get the 32-bit signed integer hash code of DateTime class object. DateTime.GetHashCode()方法用于获取DateTime类对象的32位带符号整数哈希码。 Syntax:…

ASP.NET 5 Beta8 已经发布

Microsoft ASP.NET and Web Tools 2015 (Beta8) http://www.microsoft.com/en-us/download/details.aspx?id49442 .net core 完成了98%,绝大部分类库完成了跨平台开发,已经基本可用,下一版本为RC,发布时间为12月,将可…

面试突击65:HTTPS有什么优点?说一下它的执行流程?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)说到 HTTPS 相信大部分人都是不陌生,因为目前我们使用的绝大数网站都是基于 HTTPS 的,比如以…

nanf flash校验_C ++中带有示例的nanf()函数

nanf flash校验C Nanf()函数 (C nanf() function) nanf() function is a library function of cmath header, it is used to get the NaN value of type float. It accepts an argument (which is an implementation-specific C String – to get NaN value we have to pass a…

Cell.reuseIdentifier 指什么

Cell.reuseIdentifier 指的是 默认为空,如果不定义,在执行 [_tableView registerNib:templateCellNib forCellReuseIdentifier:_templateCell.reuseIdentifier]; 时,提示 must pass a valid reuse identifier to -[UITableView registerNib:f…

缓存穿透、缓存雪崩、缓存击穿?

背景 在现代软件架构中,缓存的应用已经非常普及。缓存的使用在面试和实践中都是避不开的硬技能、硬知识,如果你说还不太熟悉缓存的使用,可能都不好意思说自己是程序员。这篇文章,带大家进一步学习在缓存使用中不得不考虑三个特殊场…

c语言 div ldiv_C ++中带有示例的ldiv()函数

c语言 div ldivC ldiv()函数 (C ldiv() function) ldiv() function is a library function of cstdlib header. It is used for integral division, it accepts two parameters (numerator and denominator) and returns a structure that contains the quot (quotient) and r…

网盘搜索

http://pan.java1234.com1、在http://baidu.com的搜索框中输入:site:http://pan.baidu.com 搜索词(回复中林涛同学建议第一条如果用谷歌搜site:百度网盘的话效果会好一点)2、壹搜 网盘搜索引擎3、盘易搜 盘易搜-百度网盘搜索4、BD盘搜索 百度…

如何防止订单重复支付?

大家好,我是磊哥,想必大家对在线支付都不陌生,今天和大家聊聊如何防止订单重复支付。看看订单支付流程我们来看看,电商订单支付的简要流程:订单钱包支付流程从下单/计算开始:下单/结算:这一步虽…