【问题标题】:Phalcon count in volt伏尔康计数
【发布时间】:2016-04-10 10:29:13
【问题描述】:

我在 phalcon volt 中有一个计数问题。我有一个名为category 的表,其中有两列idcname,还有一个表博客,还有一列category。我想显示每个类别中有多少帖子。
当我将帖子插入博客表时,在类别列中我插入其类别id。首先,我只是检索所有类别的列表,如下所示:

[controller]
$categories = Category::find();
$this->view->setVar('category', $categories);
$cx = Blogs::find();
$this->view->setVar('cates',$cx);

[Volt]
{% for categories in category %}
<a href="blog/category/{{categories.cname}}" class="tags">{{ categories.cname }} 
<span>[ 
{% for cx in cates %}
    {%if cx.category === categories.id %}
        <?php echo(count($cx->category)); ?>
    {% endif %}
{% endfor %}
]</span></a>
{% endfor %}

它的呈现像“1 1 1”或“1 1”或“1”,但它应该呈现像“3”或“2”或“1”我错了什么?

我也尝试过这样但没有得到预期的输出:

{% for categories in category %}
<a href="blog/category/{{categories.cname}}" class="tags">{{ categories.cname }} 
<span>[ 
{% for cx in cates %}

{%if cx.category === categories.id %}
{% if loop.first %} {{ loop.length }} {% endif %}

{% endif %}

{% endfor %}
]</span></a>
{% endfor %}

【问题讨论】:

  • 很难效仿你的榜样..

标签: count categories phalcon volt


【解决方案1】:

您是否在 Phalcon 中定义了模型之间的关系? 如果是这样,您可以使用内置命令查询每个类别的帖子总数

文档中的示例:

您还可以使用“count”前缀返回一个整数,表示相关记录的计数:

$robot = Robots::findFirst(2);
echo "The robot has ", $robot->countRobotsParts(), " parts\n";

我对 Volt 模板没有太多经验,但我想它会是这样的:

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

参考:https://docs.phalconphp.com/en/latest/reference/models.html#taking-advantage-of-relationships

更新 - 模型关系

[型号:类别]

public function initialize()
{
    // id => primary key name of the Category table
    // Blogs => name of the table you want to create a relationship with
    // category => name of the foreign key column in your relationship table
    $this->hasMany('id', 'Blogs', 'category');
}

[模型:博客]

public function initialize()
{
    // category => blog column name which refers to the ID in the Category table
    // Category => name of the Category table
    // id => name of the primary key column in the Category table
    $this->belongsTo('category', 'Category', 'id');
}

【讨论】:

  • 先生,我无法正确理解它,您可以提供一个工作代码作为我的示例代码吗?这里可能出了点问题,我试图从我的博客表中渲染所有内容并想要计算与类别表 id 列匹配的类别列。还面临以下错误:PhalconException:: 模型“类别”上不存在方法“初始化”
  • 删除parent::initialize(); 行。那它有用吗?
【解决方案2】:

不,先生,它不工作。但我只是这样解决了我的问题:

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

[volt]

{% for categories in category %}
<a href="blog/category/{{categories.cname}}" class="tags">{{ categories.cname }} 
<span>[ 
<?php 
$catcount = $this->modelsManager->executeQuery("SELECT Blogs.category FROM Blogs WHERE Blogs.category = $categories->id");echo(count($catcount));
?>
]</span></a>
{% endfor %}

现在它按预期工作。在这里我不做任何关系离子模型。可以吗,先生。请!谢谢

【讨论】:

  • 你并没有真正使用 Phalcon 的力量。另外,您应该避免在 volt 中使用该查询,这对您的 MVC 结构不利。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
相关资源
最近更新 更多