Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Yii2 如何获得每个类别的订单数量?_Yii2 - Fatal编程技术网

Yii2 如何获得每个类别的订单数量?

Yii2 如何获得每个类别的订单数量?,yii2,Yii2,我有两张桌子 category (id, name, description, keywords, visibility) orders (id, category_id, price_id, name, telephone, date, processed) 模型类别 public static function tableName() { return 'category'; } public function getOrders() { return $this-&g

我有两张桌子

category (id, name, description, keywords, visibility) 
orders (id, category_id, price_id, name, telephone, date, processed) 
模型类别

public static function tableName()
{
    return 'category';
}
public function getOrders()
{
    return $this->hasMany(Orders::className(), ['category_id' => 'id']);
}

function getPrices()
{
    return $this->hasMany(Price::className(), ['category_id' => 'id']);
}
和模型订单

public static function tableName()
{
    return 'orders';
}
public function getCategory()
{
    return $this->hasOne(Category::className(), ['id' => 'category_id']);
}


public function getPrice()
{
    return $this->hasOne(Price::className(), ['id' => 'price_id']);
}
我想在显示用于统计的类别时显示每个类别的订单数量。怎么做

树木建造守则

<?php  foreach ($cat as $item) :?>
                                        <ul class="treeview">
                                            <?php if ($item['id'] !=  17): ?>
                                                <li><a href="#"><?=$item['name']?> - 3</a>
                                                    <ul>
                                                        <?php  foreach ($price as $itemok) :?>
                                                            <?php if ($item['id'] ==  $itemok['category_id']): ?>
                                                                <li><a href="#"><?=$itemok['name']?></a></li>
                                                            <?php endif; ?>
                                                        <?php endforeach; ?>
                                                    </ul>
                                                </li>
                                            <?php endif; ?>
                                        </ul>
                                    <?php endforeach; ?>

有多种方法可以做到这一点,请参见下面的示例

$categories = Category::find()->all();

foreach($categories as $category){
    echo count($category->orders);
    echo $category->getOrders()->count();
}
或者在模型类别内创建一个单独的方法

public function getOrderCount(){
    return $this->getOrders()->count();
}
然后像这样使用它

$category->orderCount;

我不知道如何在注释中插入代码。你应该通过在下面添加答案将代码添加到问题中,看看是否有帮助。我添加了用于构建类别树和服务树的代码。我在下面添加了答案。你需要迭代活动记录对象,而不是数组请参见下面的$category->orderCount;我知道有必要向模型中添加属性?由于getOrderCount是您要调用的方法,只需将函数getOrderCount添加到您的模型中,并使用Category::find->all从categories模型@evgeniDuniak获取记录