首页 > 代码库 > yii2_关联表

yii2_关联表

 1 我是这样做的
 2 1. searchModel里面:
 3         $query = Order::find()->from([‘o‘ => Order::tableName()]);
 4         $query->joinWith([‘dcUser0 u‘, ‘product0 p‘]);
 5 2. Order model里面
 6     public function getDcUser0()
 7     {
 8         return $this->hasOne(DcUser::className(), [‘uid‘ => ‘uid‘]);
 9     }
10 
11     public function getProduct0()
12     {
13         return $this->hasOne(Product::className(), [‘product_id‘ => ‘product_id‘])->viaTable(OrderToProduct::tableName(), [‘order_id‘ => ‘order_id‘]);
14     }
15 
16 
17 表前缀:
18 1. class OrderSearch extends Order
19 2. Order model里面:
20 public static function tableName()
21     {
22         return ‘{{%order}}‘;
23     }
24 
25 gii生成Order时候要选有表前缀
26 
27 关联做好后 gridview写法:
28             [
29                 ‘label‘ => ‘商品名‘,
30                 ‘attribute‘ => ‘product_name‘,
31                 ‘format‘ => ‘raw‘,
32                 ‘value‘ => ‘product0.name‘,
33                 ‘filter‘ => $searchModel->product_list,
34             ],
35 
36 list
37 
38     public static function getActiveDataProvider(){
39         return class_model::getActiveDataProvider(self::find(),5);
40     }
41 
42 
43     public function count($where_arg){
44       return  self::find()->where($where_arg)->count();
45     }
46 
47     public function all($where_arg){
48         return self::find()->where($where_arg)->all();
49     }

 

yii2_关联表