Phalcon 计数(伏特)
Phalcon count in volt
我在 phalcon 伏特中遇到了计数问题。我有一个名为 category
的 table 并且有两列 id
和 cname
,还有一个 table 博客和一列 [=12] =].我想显示每个类别中有多少 post。
当我将 post 插入博客 table 时,我在类别列中插入了它的类别 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 %}
您是否在 Phalcon 中定义了模型之间的关系?
如果是这样,您可以使用内置命令查询每个类别的帖子总数
文档中的示例:
You can also use “count” prefix to return an integer denoting the count of the related records:
$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');
}
不,先生,它不起作用。但我只是这样解决了我的问题:
[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 %}
现在它按预期工作了。在这里,我不与 model.Is 建立任何关系,好的,先生。请!谢谢
我在 phalcon 伏特中遇到了计数问题。我有一个名为 category
的 table 并且有两列 id
和 cname
,还有一个 table 博客和一列 [=12] =].我想显示每个类别中有多少 post。
当我将 post 插入博客 table 时,我在类别列中插入了它的类别 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 %}
您是否在 Phalcon 中定义了模型之间的关系? 如果是这样,您可以使用内置命令查询每个类别的帖子总数
文档中的示例:
You can also use “count” prefix to return an integer denoting the count of the related records:
$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');
}
不,先生,它不起作用。但我只是这样解决了我的问题:
[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 %}
现在它按预期工作了。在这里,我不与 model.Is 建立任何关系,好的,先生。请!谢谢