2

ファルコン初心者です。ブログ テーブルからさまざまな方法でデータを取得していますが、同じテーブルから、 、 などのようdatetime DESCに同じことを何度も繰り返していると思います。1 回のクエリでデータを取得する簡単な方法はありますか?datetime DESC LIMIT 5views DESC LIMIT 5

各投稿のコメントをボルトでカウントしたい。しかし、次のように表示されます: 2 ではなく 11. コメントをカウントするにはどうすればよいですか?

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

どうすればそれを達成できますか?

4

2 に答える 2

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

于 2016-03-05T13:24:13.957 に答える