如何将计数添加到学说实体中



我是Doctirne Orm的新手,我正在从事一个项目来修复一些错误。

我有一个与该php文件关联的表wall_message_comments

<?php
use DoctrineORMMapping as ORM;
use JMSSerializerAnnotationExclusionPolicy;
use JMSSerializerAnnotationGroups;
use JMSSerializerAnnotationSerializedName;
use JMSSerializerAnnotationType;
use JMSSerializerAnnotationVirtualProperty;

/**
 * @ORMEntity
 * @ORMTable(name="wall_message_comment")
 */
class WallMessageComment {

    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     * @Groups({"user_details", "search_around"})
     */
    protected $id;
    /**
     * @ORMManyToOne(targetEntity="User", inversedBy="wall_message_comments")
     * @ORMJoinColumn(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
     * @ORMManyToOne(targetEntity="WallMessage", inversedBy="users_comments")
     * @ORMJoinColumn(name="wall_message_id", referencedColumnName="id", onDelete="CASCADE")
     */
    public $wall_message;
    /**
     * @var string
     * @ORMColumn(type="string")
     * @Groups({"user_details", "search_around"})
     */
    public $content;
    /**
     * @var int
     * @ORMColumn(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;
    }
}

我需要的是添加一个字段answers,即与wall_message_comment相关的答案计数。

每个答案都在另一个名为comment_answers的表中,带有ID和一个与wall_message_comment表相关的外键,称为parent_comment

谁能给我一个关于如何进行的建议?

谢谢!

如果您想要的只是count,最好的解决方案是使用EntityRepository

<?php
namespace AppBundleEntity;
class WallMessageComment extends DoctrineORMEntityRepository
{
    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();
    }
}

,但我建议创建关系,然后在GETTER结果上返回count()。例如,如果您在WallMessageComment上有OneToMany属性,称为answers,则可以使用以下代码:

 count($comment->getAnswers());

只要$commentWallMessageComment的实例。

相关内容

  • 没有找到相关文章