博客列表 >yii连表查询

yii连表查询

dxp2tq的博客
dxp2tq的博客原创
2018年07月28日 10:16:211669浏览

A,B,C三表连表查询:A.bid=B.id,b.cid = c.id

A模型的relations中:

'packing_order'=>array(

self::BELONGS_TO, 

'B', 

['a.bid' => 'b.id'],

'with'=>'B模型中的relations_name'

),

B的模型relations中:

'relations_name'=>array(

self::BELONGS_TO,

 'C', 

['b.cid' => 'c.id']

),


***************************************************************************

public function relations(){ 

 // NOTE: you may need to adjust the relation name and the related  

// class name for the relations automatically generated below.  

return array(      

'packing_order'=>array(          

        self::BELONGS_TO,          

        'PackingOrder',          

        ['packing_order_id' => 'id'],          

        'select'=>'packing,supplier,number,code'      

        ), 

      ); 

}

************************************************************************

$criteria= new CDbCriteria();

$criteria->join = 'LEFT JOIN oa_packing_order as po ON po.id=t.packing_order_id LEFT JOIN oa_packing_material as pm ON po.packingid=pm.id';

$criteria->select = "t.*,pm.type,po.packing,po.supplier,po.number,po.code";

$criteria->addCondition("查询条件");

$criteria->order = $order = '排序方法';

$model = Reports::model()->findAll($criteria);

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议