2016-07-28 3 views
0

문서 엔티티가 있습니다. 기사에 속성보기가 있습니다. 조회수 표는 다음과 같습니다.Symfony. 엔티티 ID로 엔티티 속성의 행 수를

id | article_id | IP

독특한 (article_id를, IP)

지금 내가 다음을 수행, 내가 ArticleRepository에 findall은 방법을 수정합니다. 올바른 방법인가요, 아니면 기사 실체에서 할 수 있습니까?

도움이 필요하면 Thx.

/** 
* Class ArticleRepository 
* 
* @package FrontendBundle\Entity 
*/ 
class ArticleRepository extends EntityRepository 
{ 
    /** 
    * @return array 
    */ 
    public function findAll() 
    { 
     if ($all = parent::findAll()) { 
      foreach ($all as $article) { 
       $article->setViews($this->getViews($article->getId())); 
      } 
     } 

     return $all; 
    } 
    /** 
    * Add view to article. 
    * 
    * @param int $id Article id. 
    * @param string $ip User clients ip 
    */ 
    public function addView($id, $ip) 
    { 
     try 
     { 
      $this->_em->getConnection()->insert('articles_views', [ 
       'article_id' => $id, 
       'ip'   => $ip 
      ]); 
     } 
     catch(UniqueConstraintViolationException $e) 
     { 
      // do nothing 
     } 
    } 

    /** 
    * Return views for article. 
    * @param int $id 
    * 
    * @return mixed 
    * @throws NonUniqueResultException If the query result is not unique. 
    * @throws NoResultException  If the query returned no result. 
    */ 
    public function getViews($id) 
    { 
     // here is custom sql query to get count rows from articles_views 
    } 

답변

0
$qb = $this->createQueryBuilder('t'); 
return $qb 
    ->select('count(t.views)') 
    ->getQuery() 
    ->getSingleScalarResult(); 
+0

당신은 나를 이해 해달라고. articles_views에는 엔티티가 없으므로 querybuilder가 작동하지 않습니다. – Tapakan

+0

쿼리를 만드는 방법을 알고 있습니다. 이게 올바른 방법인지/심포니 방식인지 알고 싶습니까? 어쩌면 더 좋은 방법이 있을까요? – Tapakan

관련 문제