我尝试用queryBuilder()
在symfony2中编写此查询,但我收到语法错误[Syntax Error] line 0, col 58: Error: Expected end of string, got 'SELECT'
SELECT * FROM upload_video as p ORDER BY (SELECT COUNT(*) FROM vote as v WHERE v.video_id = p.id ) DESC;
所以任何人都可以帮助我将此查询转换为Symfony2教义格式
$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;
}
}
我正在尝试做的是尝试根据投票表或集合中的投票来订购视频。
答案 0 :(得分:0)
您可以使用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');