Yii2常用ActiveRecord用法

时间:2023-01-31 20:26:13

1.多表连表查询与对象关联查询

public function getWmsCheck(){
        return $this->hasOne(\core\models\WmsCheck::className(), ['wms_check_code'=>'wms_check_code']);
    }
$wmsCheckDetailModels = \core\models\WmsCheckDetail::find()->where(['and',
            ['{{%wms_check_detail}}.common_producer_info_name' => '邵东汉广多特中药材初加工有限公司'],
            ['{{%wms_check_detail}}.common_producer_herb_type'=>\core\models\WmsCheck::HERB_TYPE_PRODUCT],
            ['{{%wms_check_detail}}.common_producer_herb_info_name'=>'玉竹'],
            ['in', '{{%wms_check_detail}}.common_producer_herb_grade_info_name', ['三级统、 下脚料', '皮料']]
        ])->joinWith([
            'wmsCheck'=>function($query){
                $query->andWhere([
                    '{{%wms_check}}.wms_check_end_at'=>strtotime('2018-06-01'),
                    ]);
            },
            ])->all();