yii2 with 的用法

1、在模型中先绑定

public function getCustomer()
{
//子查询
return $this->hasOne(CrmCustomer::className(), ['customer_id' => 'customer_id']); //关联表 => 当前模型主键
}

public function getBank()
{
//子查询
return $this->hasOne(CrmBank::className(), ['bank_id' => 'money_bank']); //关联表 => 当前模型主键
}

public function getPlatform()
{
//子查询
return $this->hasOne(CrmPlatform::className(), ['platform_id' => 'ad_platform_id']); //关联表 => 当前模型主键
}

public function getDepartment()
{
//子查询
return $this->hasOne(CrmDepartment::className(), ['department_id' => 'department_id']); //关联表 => 当前模型主键
}

public function getAdminuser()
{
//子查询
return $this->hasOne(AdminUser::className(), ['id' => 'admin_id']); //关联表 => 当前模型主键
}

public function getExt()
{
//子查询
return $this->hasOne(CrmKaihuExt::className(), ['kaihu_id' => 'id']); //关联表 => 当前模型主键
}

public function getAccount()
{
//子查询
return $this->hasMany(CrmAdAccount::className(), ['kaihu_id' => 'id']); //关联表 => 当前模型主键
}



2、这是一个kaihuMainSearch.php的search 模型查找数据,其实with可以用于任何地方,这里使用in查询,舍弃了leftjoin

<?php

namespace app\modules\crm\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\admin\models\RbacUserRole;
use app\modules\crm\services\CrmKaihuMainService;

/**
* CrmKaihuMainSearch represents the model behind the search form about `app\modules\crm\models\CrmKaihuMain`.
*/
class CrmKaihuMainSearch extends CrmKaihuMain
{

public $customer;
public $industry_id;
public $sub_industry;
public $platform_name;
public $operator;
public $admin_notes;
public $director_feedback;
public $expiry_time;
public $is_precharge;

/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'admin_id', 'ad_platform_id', 'customer_id', 'pay_time', 'ctime', 'mtime','department_id','kaihu_rebate'], 'integer'],
[['ad_link', 'tel', 'weixin', 'qq', 'cashback_ratio', 'who_pay', 'is_cashback', 'technician', 'payment_img', 'effect_feedback','contacts','money_bank','remarks'], 'safe'],
[['total_money', 'cashback_amount', 'link_fee'], 'number'],
[['sub_industry','customer','operator','admin_notes','director_feedback','expiry_time','is_precharge'],'safe'],
];
}

/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}

/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params,$scenarioss,$isRootUser)
{
$adminId = \Yii::$app->adminUser->id;

$query = CrmKaihuMain::find();

if(!empty($params['CrmKaihuMainSearch']['is_precharge']) && !empty($params['CrmKaihuMainSearch']['expiry_time']) && $params['CrmKaihuMainSearch']['expiry_time'] == '正常' && !empty($params['CrmKaihuMainSearch']['operator'])){
$kahuextid = CrmKaihuExt::find()
->select('kaihu_id')
->where(['is_precharge'=>$params['CrmKaihuMainSearch']['is_precharge']])
->andWhere(['>','expiry_time',time()])
->orWhere(['=', 'expiry_time', 0])
->andWhere((['like','operator',$params['CrmKaihuMainSearch']['operator']]))
->column();
}elseif (!empty($params['CrmKaihuMainSearch']['is_precharge']) && !empty($params['CrmKaihuMainSearch']['expiry_time']) && $params['CrmKaihuMainSearch']['expiry_time'] == '已到期' && !empty($params['CrmKaihuMainSearch']['operator'])){
$kahuextid = CrmKaihuExt::find()
->select('kaihu_id')
->where(['is_precharge'=>$params['CrmKaihuMainSearch']['is_precharge']])
->andWhere(['<','expiry_time',time()])
->andWhere(['<>', 'expiry_time', 0])
->andWhere((['like','operator',$params['CrmKaihuMainSearch']['operator']]))
->column();
}elseif (!empty($params['CrmKaihuMainSearch']['is_precharge']) && !empty($params['CrmKaihuMainSearch']['operator'])){
$kahuextid = CrmKaihuExt::find()
->select('kaihu_id')
->where(['is_precharge'=>$params['CrmKaihuMainSearch']['is_precharge']])
->andWhere((['like','operator',$params['CrmKaihuMainSearch']['operator']]))
->column();
}elseif (!empty($params['CrmKaihuMainSearch']['expiry_time']) && $params['CrmKaihuMainSearch']['expiry_time'] == '正常' && !empty($params['CrmKaihuMainSearch']['operator'])){
$kahuextid = CrmKaihuExt::find()
->select('kaihu_id')
->where(['>','expiry_time',time()])
->orWhere(['=', 'expiry_time', 0])
->andWhere((['like','operator',$params['CrmKaihuMainSearch']['operator']]))
->column();
}elseif (!empty($params['CrmKaihuMainSearch']['expiry_time']) && $params['CrmKaihuMainSearch']['expiry_time'] == '已到期' && !empty($params['CrmKaihuMainSearch']['operator'])){
$kahuextid = CrmKaihuExt::find()
->select('kaihu_id')
->where(['<','expiry_time',time()])
->andWhere(['<>', 'expiry_time', 0])
->andWhere((['like','operator',$params['CrmKaihuMainSearch']['operator']]))
->column();
}elseif (!empty($params['CrmKaihuMainSearch']['is_precharge'])){
$kahuextid = CrmKaihuExt::find()
->select('kaihu_id')
->where(['is_precharge'=>$params['CrmKaihuMainSearch']['is_precharge']])
->column();
}elseif (!empty($params['CrmKaihuMainSearch']['expiry_time']) && $params['CrmKaihuMainSearch']['expiry_time'] == '正常'){
$kahuextid = CrmKaihuExt::find()
->select('kaihu_id')
->where(['>','expiry_time',time()])
->orWhere(['=', 'expiry_time', 0])
->column();
}elseif (!empty($params['CrmKaihuMainSearch']['expiry_time']) && $params['CrmKaihuMainSearch']['expiry_time'] == '已到期'){
$kahuextid = CrmKaihuExt::find()
->select('kaihu_id')
->where(['<','expiry_time',time()])
->andWhere(['<>', 'expiry_time', 0])
->column();
}elseif (!empty($params['CrmKaihuMainSearch']['operator'])){
$kahuextid = CrmKaihuExt::find()
->select('kaihu_id')
->where((['like','operator',$params['CrmKaihuMainSearch']['operator']]))
->column();
}
if($scenarioss){
$kaihuIdArr = CrmKaihuMainService::getAdminKaihuIdsMap($adminId,$scenarioss,$isRootUser);
if(empty($kaihuIdArr)){
$kaihuIdArr = [-1];
}
}else{
$kaihuIdArr = CrmKaihuLog::getRootSendKaihuId($adminId,$isRootUser);
if(empty($kaihuIdArr)){
$kaihuIdArr = [-1];
}
}

if(isset($kahuextid)){
$kaihuIdArr = array_intersect($kahuextid,$kaihuIdArr);
}
$query->Where(['in','id', $kaihuIdArr]);

if(!empty($params['CrmKaihuMainSearch']['ctime'])){
$params['ctime'] = explode('/',$params['CrmKaihuMainSearch']['ctime']);
$params['start'] = strtotime($params['ctime'][0]);
$params['end'] = strtotime($params['ctime'][1])+86400;
$query->andWhere(['>=', 'pay_time', $params['start']]);
$query->andWhere(['<=', 'pay_time', $params['end']]);
}

if(!empty($params['CrmKaihuMainSearch']['pay_time'])){
$timestamp0 = strtotime($params['CrmKaihuMainSearch']['pay_time']);
$timestamp24 = strtotime($params['CrmKaihuMainSearch']['pay_time']) + 86400;
$query->andWhere(['>=', 'pay_time', $timestamp0]);
$query->andWhere(['<', 'pay_time', $timestamp24]);
}

//这个查询前,要在kaihuMain模型做绑定
$query->with(['customer','platform','ext','bank','adminuser','department']);
$query->select('*');
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10
],
'sort' => [
'defaultOrder' => [
'id' => SORT_DESC,
]
],
]);

$this->load($params);

if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}

// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'admin_id' => $this->admin_id,
'ad_platform_id' => $this->ad_platform_id,
'customer_id' => $this->customer_id,
'total_money' => $this->total_money,
'pay_time' => $this->pay_time,
'cashback_ratio' => $this->cashback_ratio,
'cashback_amount' => $this->cashback_amount,
'link_fee' => $this->link_fee,
'ctime' => $this->ctime,
'mtime' => $this->mtime,

]);

$query->andFilterWhere(['like', 'ad_link', $this->ad_link])
->andFilterWhere(['like', 'tel', $this->tel])
->andFilterWhere(['like', 'weixin', $this->weixin])
->andFilterWhere(['like', 'qq', $this->qq])
->andFilterWhere(['like', 'who_pay', $this->who_pay])
->andFilterWhere(['like', 'is_cashback', $this->is_cashback])
->andFilterWhere(['like', 'technician', $this->technician])
->andFilterWhere(['like', 'payment_img', $this->payment_img])
->andFilterWhere(['like', 'effect_feedback', $this->effect_feedback])
->andFilterWhere(['like', 'contacts', $this->contacts])
->andFilterWhere(['like', 'money_bank', $this->money_bank]);
if(!empty($params['CrmKaihuMainSearch']['platform_name'])){
$query->andFilterWhere(['like', 'ad_platform_id', $params['CrmKaihuMainSearch']['platform_name']]);
}
if(!empty($params['CrmKaihuMainSearch']['department_id'])){
$query->andFilterWhere(['like', 'department_id', $params['CrmKaihuMainSearch']['department_id']]);
}
if(!empty($params['CrmKaihuMainSearch']['customer'])){
$query->andFilterWhere(['like', 'customer_id', $params['CrmKaihuMainSearch']['customer']]);
}
if(!empty($params['CrmKaihuMainSearch']['id'])){
$query->andFilterWhere(['like', 'id', $params['CrmKaihuMainSearch']['id']]);
}
return $dataProvider;
}
}

3、介绍点with的高级用法 ,多表with叫搜索条件
var $condition = 0;
//评论数对比条件
$user= user::find()->with(['ext','articles' => function
($query) use($condition) { $query->where(['>','comment',$condition]); }
])->all();

猜你喜欢

转载自www.cnblogs.com/jjchi/p/9445701.html
今日推荐