如何计算 phalcon 伏特中的列数?

How can I count columns in phalcon volt?

我是 phalcon 的新手。我正在从博客中检索数据 table 有很多方法,但我认为我在同一个 table 中一次又一次地做同样的事情,例如 datetime DESCdatetime DESC LIMIT 5views DESC LIMIT 5 等。有没有一种简单的方法可以在一次查询中检索数据?

我想为每个 post 以伏特为单位计算评论。但它显示为:11 而不是 2。我如何计算评论数?

 # Blog Controller
 public function indexAction()
 {
   #Data Retrieve
    $bloger = Blogs::find(["order" => "datetime DESC"]);
    $this->view->setVar('blogs', $bloger);
  :Count How Many Post have each User
    $pcount = Blogs::findBybauthor($this->session->get('uname'));
    $this->view->setVar('eachpost',count($pcount));
  :Get Recent Posts
    $latest = Blogs::find(["order" => "datetime DESC limit 5"]);
    $this->view->setVar('recent', $latest);
  :Get Most visited Posts
    $viewer = Blogs::find(["order" => "views DESC limit 5"]);
    $this->view->setVar('views', $viewer);
  :Comments Retrieve
    $coment = Comments::find();
    $this->view->setVar('comented', $coment);
    }

   #[VOLT]

这是我的电压标签,它没有按预期显示。我也使用 |length 但它没有按预期工作:

 {% for coment in comented %}
 {% if coment.entry_id === bloger.id %}
 <?php echo(count($coment->entry_id)); ?>
 {% endif %}
 {% endfor %}

我怎样才能做到这一点?

public function indexAction()
{

// Get latest posts 
$bloger = Blogs::find([
    "order" => "datetime DESC", 
    "limit" => 10,
    "cache" => ["lifetime" => 3600, "key" => "my-find-key"]
]);
$this->view->setVar('blogs', $bloger);

// count posts by author
$pcount = Blogs::count([
    "bauthor = :author:",
    "bind" => [
        "bauthor" => $this->session->get('uname')
    ]
]);
$this->view->setVar('eachpost', $pcount);
// Info about model aggregations here: https://docs.phalconphp.com/en/latest/reference/models.html#generating-calculations

// latest posts (i would suggest you to cache those queries since they will not change frequenlty. More info in the link below)
$latest = Blogs::find([
    "order" => "datetime DESC", 
    "limit" => 5,
    "cache" => ["lifetime" => 3600, "key" => "my-find-key"]
]);
$this->view->setVar('recent', $latest);

// top views (i would suggest you to cache those queries since they will not change frequenlty. More info in the link below)
$viewer = Blogs::find([
    "order" => "views DESC", 
    "limit" => 5,
    "cache" => ["lifetime" => 3600, "key" => "my-find-key"]
]);
$this->view->setVar('views', $viewer);

// Get current post (if no other params are passed findFirst fetches records by table's Primary key)
$currentPost = Blogs::findFirst($postID);

// Get post comments (im not sure what you want to do here, but i guess you wnat to get comments for the current post only.)
$coment = Comments::find([
    "blog_id = :blog_id:",
    "bind" => [
        "blog_id" => $currentPost->id
    ]
]);
$this->view->setVar('comented', $coment);

// Another example using query build to fetch all posts and their comments count with one query
$this->modelsManager->createBuilder()
  ->columns(array('blogs.*', 'COUNT(comments.id) AS commentCount'))
  ->from(array('blogs' => 'Blogs'))
  ->leftJoin('Comments', 'comments.blog_id = blogs.id', 'comments')
  ->groupBy(array('Comments.blog_id'));    
  ->getQuery()->execute();
}

有关使用 Phalcon 模型的更多信息:https://docs.phalconphp.com/en/latest/reference/models.html

In controller first try to render each posts detail  in view (blog/show)
Here is my controller.php.


public function showfullAction($pid)
{
    $blog = Blogs::findFirstById($pid);
    $gid = $blog->id;
    $data = Blogs::findFirstById($gid);
    $this->view->setVar('detail', $data);
    #Similar Posts
    $similar = Blogs::find(["btitle LIKE :title:","bind"=>["title"=>'%'.$data->btitle.'%'],"order" => "datetime DESC limit 5"]);

    $this->view->setVar('datas', $similar); 
    $this->view->pick('blog/show');
   }

   And finally in my view page (blog/show):

 {% for similar in datas %}
 {{link_to('blog/showfull/'~similar.id,similar.btitle,'class':'cats')}}  <br/>
 {% endfor %}

现在一切正常!