我在一个模型中有很多关系。我想使用过滤器和导出网格的搜索模型显示在网格 View 中收到的订单、产品和订单的总数,但我无法弄清楚如何通过排序获得计数。我有以下关系。
public function getProducts()
{
return $this->hasMany(Product::className(), ['user_id' => 'user_id']);
}
public function getShopImages(){
return $this->hasMany(ShopImage::className(), ['user_id' => 'user_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getOrders()
{
// Customer has_many Order via Order.customer_id -> id
return $this->hasMany(Order::className(), ['user_id' => 'user_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getOrderrs()
{
// Customer has_many Order via Order.customer_id -> id
return $this->hasMany(Order::className(), ['merchant_id' => 'user_id']);
}
我需要对每一个进行计数。任何想法,怎么做?
最佳答案
请查看以下链接。是帮你。
一种。 https://github.com/yiisoft/yii2/issues/2179
湾http://www.yiiframework.com/forum/index.php/topic/62772-how-to-get-count-in-relation-table-in-yii2-activerecord/
public function getProducts(){
return $this->hasMany(Product::className(), ['user_id' => 'user_id'])->count();}
关于php - 如何从网格 Yii2 搜索模型中的许多关系中获取计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41198449/