1

ファルコンボルトで数え方に問題があります。という名前のテーブルがcategoryあり、そこに と の 2 つの列がidありcname、ブログのテーブルもあり、列があります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 %}
4

2 に答える 2

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');
}
于 2016-04-10T11:31:48.773 に答える
0

いいえ、うまくいきません。しかし、私はこのように私の問題を解決しました:

[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 %}

現在、期待どおりに動作しています。ここでは、関係イオン モデルを作成しません。大丈夫ですか。お願いします!thnx

于 2016-04-11T04:23:23.397 に答える