简体   繁体   English

phalcon 按 id 计数,以伏特为单位

[英]phalcon count by id in volt

I'm facing problem in Phalcon.我在 Phalcon 中遇到了问题。 in my blog i have a category table there have all list of category like "a,b,c,d,e,f,g,h,i" and in my blog table have a column name category.在我的博客中,我有一个类别表,其中包含所有类别列表,例如“a、b、c、d、e、f、g、h、i”,并且在我的博客表中有一个列名称类别。 in category column i'm inserting the id of category.在类别列中,我插入类别的 id。 now the problem is i want to count how many post have in each category.现在的问题是我想计算每个类别中有多少帖子。 i'm unable to get expected result.我无法得到预期的结果。 count result result shows me result [0].计数结果结果显示我的结果 [0]。 whats my fault?我的错是什么?

[Blog Controller] [博客控制器]

    $categories = Category::find();
    $this->view->setVar('category', $categories);

    $ab = Blogs::countBycategory($categories->id);
    $this->view->setVar('ccat',$ab);

[Index View] [索引视图]

    {% for categories in category %}
    <a title="{{categories.cname}}" href="blog/category/{{categories.cname}}" class="tags">
        {{ categories.cname }} <span>[ {{ccat}} ]</span></a>
    {% endfor %}

You would benefit from using Model Relationships as referenced here: https://docs.phalconphp.com/uk/latest/reference/models.html#relationships-between-models您将受益于使用此处引用的模型关系: https : //docs.phalconphp.com/uk/latest/reference/models.html#relationships-between-models

Essentially your category model could look like this:本质上,您的类别模型可能如下所示:

<?php

class Category extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->hasMany("id", "Blogs", "category_id", [
            "alias" => "articles"
        ]);
    }
}

Then to get a count of articles per category, do something like this:然后要计算每个类别的文章数量,请执行以下操作:

$categories = Category::find();
foreach($categories as $category) {
    echo $category->cname . " " . count($category->articles) . "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM