检查返回的记录是否是最后一个Symfony2

时间:2014-01-30 13:20:48

标签: symfony doctrine

抱歉我的英文不好

我的问题是如何检查返回的记录是否是最后一次。

<?php

    /**
     * @ORM\Entity
     * @ORM\Table(name="post")
     */
    class Post
    {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
     */
    protected $comments;


    public function __construct()
    {
        $this->comments    = new ArrayCollection();
    }

    public function addComment(Comment $comments)
    {
        $this->comments[] = $comments;

        return $this;
    }


    public function removeComment(Comment $comments)
    {
        $this->comments->removeElement($comments);
    }


    public function getComments()
    {
        return $this->comments;
    }
}

评论实体

class Comment {
    /**
     * @ORM\id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Post", inversedBy="comments")
     * @ORM\JoinColumn(name="post_id", referencedColumnName="id")
     */
    protected $post;

    public function setPost(Post $post = null)
    {
        $this->post = $post;

        return $this;
    }


    public function getPost()
    {
        return $this->post;
    } 
}

在我的控制器中:

use Doctrine\Common\Collections\Criteria;  
    class PostController  
    {
        public function myAction()
        {
            $criteria = Criteria::create()
                ->orderBy(array("id" => Criteria::ASC))
                ->setFirstResult($offset)
                ->setMaxResults($limit);

            $em = $this->getDoctrine->getManager();
            $post = $em->getRepository("Post")->find(1);

            $comments = $post->getComments()->matching($criteria);
        }  
    }

评论完全按照我的要求返回。但是如何检查返回的注释是否在注释表的最后?

请帮忙

1 个答案:

答案 0 :(得分:0)

我不太确定你想要什么,但我相信它是:

if ($offset + $limit >= $post->getComments()->count()) {
  // Last comments.
}