如何用doctrine创建mysql请求

时间:2014-02-10 20:16:35

标签: php symfony doctrine

我想将我的sql行转换为doctrine对象。

SELECT * FROM t_download GROUP BY download_yt_id ORDER BY download_id DESC LIMIT 50

我已经做了但那不起作用:

$Download = new Download();


        $em = $this->getDoctrine()->getManager();
        $query = $em->createQueryBuilder();

        $query
            ->select('distinct :id')
            ->from('DimiYvmBundle:Download', 'd')
            ->orderBy(':id', 'DESC')
            ->setParameter('id', $Download->getId());


        $query->setMaxResults(50);

        $lastdl = $query->getQuery()->getResult();

使用getter和setter下载我的实体:

    namespace Dimi\YvmBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    class Download
    {

        private $id;

        private $ytId;

        private $date;

        private $title;

        public function getId()
        {
            return $this->id;
        }

    public function setYtId($ytId)
        {
            $this->ytId = $ytId;

            return $this;
        }

        public function getYtId()
        {
            return $this->ytId;
        }

        public function setTitle($title)
        {
            $this->title = $title;

            return $this;
        }

        public function getTitle()
        {
            return $this->title;
        }

        public function setDate($date)
        {
            $this->date = $date;

            return $this;
        }

        public function getDate()
        {
            return $this->date;
        }

        public function getLastDownload()
        {
            // SELECT * FROM t_download GROUP BY download_yt_id ORDER BY download_id DESC LIMIT 50
        }

    }

我想在我的模型中包含我的getLastDownload方法。你知道我怎么做吗?

谢谢大家的帮助。 最好的问候,

1 个答案:

答案 0 :(得分:0)

你可能正在寻找一些简单的东西吗?

$query->select('d')
        ->from('DimiYvmBundle:Download', 'd')
        ->orderBy('d.id', 'DESC')
        ->andWhere('d.id', ':id')
        ->setParameter('id', $Download->getId());

$lastdl = $query->getQuery()->getResult();