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

时间:2016-03-05 09:54:28

标签: php phalcon volt

我是phalcon的新手。我正在从博客中检索数据     表格有很多方法,但我想我一次又一次地做同样的事情,比如来自同一张桌子的datetime DESCdatetime DESC LIMIT 5views DESC LIMIT 5等。有一种简单的方法可以在一个查询中检索数据吗?

我想计算每个帖子的伏特评论。但它显示如下: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]

这是我的伏特标签没有按预期显示。我也使用|长度,但它没有按预期工作:

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

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

现在按预期工作了!