Search code examples
phpphalconvolt

How can I count columns in phalcon volt?


I'm new to phalcon. I am retrieving data from the blog table with many ways but I think I'm doing same thing again and again like datetime DESC, datetime DESC LIMIT 5, views DESC LIMIT 5, etc. from the same table. Is there an easy way to retrieve data in one query?

I want to count comments in volt for each post. But it shows like: 11 instead of 2. How do I count comments?

 # 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]

This is my volt tags its not showing as expected. I also use |length but it's not working as expected:

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

How can I achieve that?


Solution

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

    and now its working as expected!