Jayyrus
Jayyrus

Reputation: 13051

How to add count into doctrine entity

I'm newbie in Doctirne ORM and I'm working on a project just to fix some bugs.

I've got a table wall_message_comments that is associated to that PHP file:

<?php

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\VirtualProperty;


/**
 * @ORM\Entity
 * @ORM\Table(name="wall_message_comment")
 */
class WallMessageComment {


    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Groups({"user_details", "search_around"})
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="wall_message_comments")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
     * @Groups({})
     */
    public $user;


    /**
     * @VirtualProperty()
     * @Groups({"user_details"})
     * @Type("integer")
     * @return int
     */
    public function getWallMessageId(){
        return $this->wall_message->getId();
    }
    /**
     * @var WallMessage
     * @ORM\ManyToOne(targetEntity="WallMessage", inversedBy="users_comments")
     * @ORM\JoinColumn(name="wall_message_id", referencedColumnName="id", onDelete="CASCADE")
     */
    public $wall_message;
    /**
     * @var string
     * @ORM\Column(type="string")
     * @Groups({"user_details", "search_around"})
     */
    public $content;

    /**
     * @var int
     * @ORM\Column(type="integer")
     * @Groups({"user_details", "search_around"})
     */
    public $timestamp;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId( $id )
    {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getTimestamp()
    {
        return intval($this->timestamp);
    }

    /**
     * @param int $timestamp
     */
    public function setTimestamp( $timestamp )
    {
        $this->timestamp = $timestamp;
    }

    /**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param mixed $user
     */
    public function setUser( $user )
    {
        $this->user = $user;
    }

    /**
     * @return mixed
     */
    public function getWallMessage()
    {
        return $this->wall_message;
    }

    /**
     * @param mixed $wall_message
     */
    public function setWallMessage( $wall_message )
    {
        $this->wall_message = $wall_message;
    }

    /**
     * @return int
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * @param string $content
     */
    public function setContent( $content )
    {
        $this->content = $content;
    }
}

What i need is to add a field answers that is the count of answers related to a wall_message_comment.

each answer is in another table called comment_answers with an id and a foreign key related to the wall_message_comment table called parent_comment.

Could anyone give me an advise on how to proceed?

Thanks!

Upvotes: 1

Views: 768

Answers (1)

Oscar P&#233;rez
Oscar P&#233;rez

Reputation: 4397

If what you want is just the count, the best solution would be to use a EntityRepository:

<?php

namespace AppBundle\Entity;

class WallMessageComment extends \Doctrine\ORM\EntityRepository
{
    public function countAnswers(WallMessageComment $message)
    {
       return $this->getEntityManager()
                   ->createQueryBuilder()
                   ->select('COUNT(u.id)')
                   ->from('AppBundle:Answer')
                   ->andWhere('u.parent_comment=:message_id')
                   ->setParameter('message_id',$message->getId())
                   ->getQuery()
                   ->getSingleScalarResult();
    }
}

But I would suggest to create the relationship and then return a count() on the getter result. For example, if you have a OneToMany property on WallMessageComment called answers, you could use a code like:

 count($comment->getAnswers());

As long as $comment is an instance of WallMessageComment.

Upvotes: 1

Related Questions