2014-01-30 36 views
0

對不起我的英文不好檢查返回的記錄是去年的Symfony2

我的問題是,如何檢查,如果返回的記錄是最後一次。

<?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); 
     } 
    } 

評論返回正是我想要的。但是如何檢查返回的評論是否在評論表中最後?

請幫

+0

是由**創建日期**不是一個選項排序? – nifr

回答

0

我不太清楚你想要什麼,但我相信它是:

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

它的工作原理。非常感謝你 – Faruh