php - Symfony2 Doctrine 子查询

标签 php mysql symfony doctrine-orm doctrine

我尝试使用 queryBuilder() 在 symfony2 中编写此查询,但出现语法错误 [Syntax Error] line 0, col 58: Error: Expected end of string, got '选择'

SELECT  * FROM  upload_video as p ORDER BY (SELECT  COUNT(*) FROM vote as v WHERE v.video_id = p.id ) DESC;

谁能帮我把这个查询转换成 Symfony2 Doctrine 格式 $qb->createQuery("SELECT p FROM HotelPlanBundle:UploadVideo as p ORDER BY (SELECT COUNT(v) FROM HotelPlanBundle:Vote v WHERE v.video_id = p.id ) desc");

这是UploadVideo实体

namespace HotelPlanBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;

/**
 * UploadVideo
 *
 * @ORM\Table(name="upload_video")
 * @ORM\Entity(repositoryClass="HotelPlanBundle\Repository\UploadVideoRepository")
 * @ORM\HasLifecycleCallbacks
 */
class UploadVideo
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    private $file;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255, nullable=true)
     */
    private $title;

    /**
     * @var string
     *
     * @ORM\Column(name="path", type="text", nullable=true)
     */
    private $path;

    /**
     * @var string
     *
     * @ORM\Column(name="video_path", type="text", nullable=true)
     */
    private $videoPath;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_date", type="datetime")
     */
    private $createdDate;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_approved", type="boolean")
     */
    private $isApproved;

    /**
     * @var \DateTime
     * @ORM\Column(name="approved_date", type="datetime", nullable=true)
     */
    private $approvedDate;

    /**
     * @ORM\OneToMany( targetEntity="HotelPlanBundle\Entity\Vote", mappedBy="videoId", cascade={"all"}, orphanRemoval=true )
     * @ORM\OrderBy({"id" = "ASC"})
     */
    protected $votes;

    /**
     * @var integer
     * @ORM\Column(name="is_winner", type="boolean")
     */
    private $isWinner;

    /**
     * @var integer
     * @ORM\Column(name="views", type="integer")
     */
    private $views;

    /**
     * @var boolean
     * @ORM\Column(name="is_deleted", type="boolean")
     */
    private $isDeleted;

    /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $userId;

    /**
     * @var string
     *
     * @ORM\Column(name="status", type="string", length=255, nullable=true)
     * @ORM\ManyToOne(targetEntity="VideoStatus")
     * @ORM\JoinColumn(name="is_approved", referencedColumnName="id")
     */
    private $status;

    /**
     * @return null|string
     * @ORM\Column(name="link", type="string", length=255, unique=true)
     */
    private $link;

    /**
     * @var
     * @ORM\Column(name="thumbnail", type="string", length=100, nullable=true)
     */
    private $thumbnail;

    /**
     * @return null|string
     * @ORM\Column(name="resetlink", type="string", length=255, nullable=true)
     */
    private $resetLink;

    public function __construct()
    {
        $this->setIsWinner( FALSE );
        $this->setIsDeleted( FALSE );
        $this->setIsApproved( FALSE );
        $this->votes = new ArrayCollection();
    }

    public function getAbsolutePath()
    {
        return NULL === $this->path
            ? NULL
            : $this->getUploadRootDir() . '/' . $this->path;
    }

    public function getWebPath()
    {
        return NULL === $this->path
            ? NULL
            : $this->getUploadDir() . '/' . $this->path;
    }

    public function getWebThumbnail()
    {
        return '/uploads/documents' . '/' . $this->getThumbnail();
    }

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded
        // documents should be saved
        return __DIR__ . '/../../../web/' . $this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        return 'uploads/documents';
    }

    public function upload()
    {
        // the file property can be empty if the field is not required
        if ( NULL === $this->getFile() ) {
            return;
        }

        // use the original file name here but you should
        // sanitize it at least to avoid any security issues

        // move takes the target directory and then the
        // target filename to move to
        $this->getFile()->move(
            $this->getUploadRootDir(),
            $this->getFile()->getClientOriginalName()
        );

        // set the path property to the filename where you've saved the file
        $this->path = $this->getFile()->getClientOriginalName();

        // clean up the file property as you won't need it anymore
        $this->file = NULL;
    }

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


    /**
     * Set title
     *
     * @param string $title
     * @return UploadVideo
     */
    public function setTitle( $title )
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set file
     *
     * @param string $file
     * @return UploadVideo
     */
    public function setFile( $file )
    {
        $this->file = $file;

        return $this;
    }

    /**
     * Get file
     *
     * @return string
     */
    public function getFile()
    {
        return $this->file;
    }

    /**
     * Set path
     *
     * @param string $path
     * @return UploadVideo
     */
    public function setPath( $path )
    {
        $this->path = $path;

        return $this;
    }

    /**
     * Get path
     *
     * @return string
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * Set videoPath
     *
     * @param string $videoPath
     * @return UploadVideo
     */
    public function setVideoPath( $videoPath )
    {
        $this->videoPath = $videoPath;

        return $this;
    }

    /**
     * Get videoPath
     *
     * @return string
     */
    public function getVideoPath()
    {
        return $this->videoPath;
    }

    /**
     * Set createdDate
     *
     * @param \DateTime $createdDate
     * @return UploadVideo
     */
    public function setCreatedDate( $createdDate )
    {
        $this->createdDate = $createdDate;

        return $this;
    }

    /**
     * Get createdDate
     *
     * @return \DateTime
     */
    public function getCreatedDate()
    {
        return $this->createdDate;
    }

    /**
     * Set isApproved
     *
     * @param boolean $isApproved
     * @return UploadVideo
     */
    public function setIsApproved( $isApproved )
    {
        $this->isApproved = $isApproved;

        return $this;
    }

    /**
     * Get isApproved
     *
     * @return boolean
     */
    public function getIsApproved()
    {
        return $this->isApproved;
    }

    /**
     * Set approvedDate
     *
     * @param \DateTime $approvedDate
     * @return UploadVideo
     */
    public function setApprovedDate( $approvedDate )
    {
        $this->approvedDate = $approvedDate;

        return $this;
    }

    /**
     * Get approvedDate
     *
     * @return \DateTime
     */
    public function getApprovedDate()
    {
        return $this->approvedDate;
    }

    /**
     * Set isWinner
     *
     * @param boolean $isWinner
     * @return UploadVideo
     */
    public function setIsWinner( $isWinner )
    {
        $this->isWinner = $isWinner;

        return $this;
    }

    /**
     * Get isWinner
     *
     * @return boolean
     */
    public function getIsWinner()
    {
        return $this->isWinner;
    }

    /**
     * Set isDeleted
     *
     * @param boolean $isDeleted
     * @return UploadVideo
     */
    public function setIsDeleted( $isDeleted )
    {
        $this->isDeleted = $isDeleted;

        return $this;
    }

    /**
     * Get isDeleted
     *
     * @return boolean
     */
    public function getIsDeleted()
    {
        return $this->isDeleted;
    }

    /**
     * Set status
     *
     * @param string $status
     * @return UploadVideo
     */
    public function setStatus( $status )
    {
        $this->status = $status;

        return $this;
    }

    /**
     * Get status
     *
     * @return string
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Set link
     *
     * @param string $link
     * @return UploadVideo
     */
    public function setLink( $link )
    {
        $this->link = $link;

        return $this;
    }

    /**
     * Get link
     *
     * @return string
     */
    public function getLink()
    {
        return $this->link;
    }

    /**
     * Set thumbnail
     *
     * @param string $thumbnail
     * @return UploadVideo
     */
    public function setThumbnail( $thumbnail )
    {
        $this->thumbnail = $thumbnail;

        return $this;
    }

    /**
     * Get thumbnail
     *
     * @return string
     */
    public function getThumbnail()
    {
        return $this->thumbnail;
    }

    /**
     * Set resetLink
     *
     * @param string $resetLink
     * @return UploadVideo
     */
    public function setResetLink( $resetLink )
    {
        $this->resetLink = $resetLink;

        return $this;
    }

    /**
     * Get resetLink
     *
     * @return string
     */
    public function getResetLink()
    {
        return $this->resetLink;
    }

    /**
     * Add votes
     *
     * @param \HotelPlanBundle\Entity\Vote $votes
     * @return UploadVideo
     */
    public function addVote( \HotelPlanBundle\Entity\Vote $votes )
    {
        $this->votes[] = $votes;

        return $this;
    }

    /**
     * Remove votes
     *
     * @param \HotelPlanBundle\Entity\Vote $votes
     */
    public function removeVote( \HotelPlanBundle\Entity\Vote $votes )
    {
        $this->votes->removeElement( $votes );
    }

    /**
     * Get votes
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getVotes()
    {
        return $this->votes;
    }

    /**
     * Set userId
     *
     * @param \HotelPlanBundle\Entity\User $userId
     * @return UploadVideo
     */
    public function setUserId( \HotelPlanBundle\Entity\User $userId = NULL )
    {
        $this->userId = $userId;

        return $this;
    }

    /**
     * Get userId
     *
     * @return \HotelPlanBundle\Entity\User
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * Set views
     *
     * @param integer $views
     * @return UploadVideo
     */
    public function setViews($views)
    {
        $this->views = $views;

        return $this;
    }

    /**
     * Get views
     *
     * @return integer 
     */
    public function getViews()
    {
        return $this->views;
    }
}

`这是投票实体

namespace HotelPlanBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Vote
 *
 * @ORM\Table(name="vote")
 * @ORM\Entity(repositoryClass="HotelPlanBundle\Repository\VoteRepository")
 */
class Vote
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="Likes")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $userId;

    /**
     * @var int
     *
     * @ORM\ManyToOne(targetEntity="HotelPlanBundle\Entity\UploadVideo", inversedBy="votes", cascade={"persist"})
     * @ORM\JoinColumn(name="video_id", referencedColumnName="id")
     *
     */
    private $videoId;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $createdAt;



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

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return Vote
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set userId
     *
     * @param \HotelPlanBundle\Entity\User $userId
     * @return Vote
     */
    public function setUserId(\HotelPlanBundle\Entity\User $userId = null)
    {
        $this->userId = $userId;

        return $this;
    }

    /**
     * Get userId
     *
     * @return \HotelPlanBundle\Entity\User
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * Set videoId
     *
     * @param \HotelPlanBundle\Entity\UploadVideo $videoId
     * @return Vote
     */
    public function setVideoId(\HotelPlanBundle\Entity\UploadVideo $videoId = null)
    {
        $this->videoId = $videoId;

        return $this;
    }

    /**
     * Get videoId
     *
     * @return \HotelPlanBundle\Entity\UploadVideo
     */
    public function getVideoId()
    {
        return $this->videoId;
    }
}

我想做的是尝试根据投票表或集合中的投票对视频进行排序。

最佳答案

您可以使用QueryBuilder实现这一目标:

$qb = $em->createQueryBuilder();
$result = $qb->select('uv, COUNT(v) AS HIDDEN votesCount')
    ->from('HotelPlanBundle\Entity\UploadVideo', 'uv')
    ->leftJoin('uv.votes', 'v')
    ->where('v.videoId = uv.id')
    ->orderBy('votesCount', 'DESC')
    ->groupBy('uv')
    ->getQuery()
    ->getResult();

或者如果您想使用 DQL :

$em->createQuery('SELECT uv, COUNT(v) as AS HIDDEN votesCount FROM HotelPlanBundle\Entity\UploadVideo uv LEFT JOIN uv.votes v WHERE v.videoId = uv.id GROUP BY uv ORDER BY votesCount DESC');

关于php - Symfony2 Doctrine 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35632772/

相关文章:

php - 使用 PHP 和 Drive API 将文件上传到 Google Drive 上的特定文件夹

PHP 数组,将数组项的计数递归附加到数组

mysql - 多个 SQL JOIN 语句

symfony - 如何在 Symfony + Doctrine 中监控和记录慢速查询

Symfony2 - 检查 Doctrine 实体关联是否已在不触发延迟加载的情况下初始化/加载

php - 尝试创建 MySQL 数据库给出 HTTP/404 请求的 URL 别名未定义

php - nl2br() 创建一个额外的新行

mysql - 没有 KEY 的 SQL 更新或插入

php - Yii2电话领域问题

php - Symfony 在调试 :container --show-private 中不显示私有(private)服务