简体   繁体   English

Symfony2 QueryBuilder SQL到DQL

[英]Symfony2 QueryBuilder SQL to DQL

can anybody help me converting my SQL into valid Doctrine DQL Code? 有人可以帮助我将SQL转换为有效的Doctrine DQL代码吗?

SELECT * FROM tasks WHERE (SELECT COUNT(*) FROM tasks_done WHERE tasks_done.user_id = $userid AND tasks_done.task_id = tasks.id) = 1 AND tasks.user = $userid

What I tried: 我试过的

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

    $qb = $em->createQuery("
        SELECT
            t
        FROM
            tasks t
        WHERE
            t.id IN (
                SELECT
                    td
                FROM
                    TaskDone td
                WHERE
                    td.task_id = t.id
                AND
                    td.user_id = $id
            )

        AND
           t.user = $id
    ");
    $taskDone = $qb->getResult();

This throws following exception: 这引发以下异常:

[Semantical Error] line 0, col 71 near 'tasks t ': Error: Class 'tasks' is not defined. [语义错误]第0行,第71列靠近“任务t”:错误:未定义类“任务”。

My Entites are called: Task.php and TaskDone.php , Classname is the same like filename. 我的实体称为:Task.php和TaskDone.php,类名与文件名相同。 Table names are: tasks and task_done 表名称是:task和task_done

EDIT 编辑

Structure TaskDone.php Entite 结构TaskDone.php实体

<?php

namespace Seotool\\MainBundle\\Entity; 命名空间Seotool \\ MainBundle \\ Entity;

use Doctrine\\Common\\Collections\\ArrayCollection; 使用Doctrine \\ Common \\ Collections \\ ArrayCollection; use Doctrine\\ORM\\Mapping as ORM; 使用Doctrine \\ ORM \\ Mapping作为ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="task_done")
 */
class TaskDone
{


/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;


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

/**
 * @ORM\Column(type="integer")
 */
private $task_id;

/**
 * @ORM\Column(type="integer")
 */
private $user_id;

/**
 * Set task_id
 *
 * @param integer $taskId
 * @return TaskDone
 */
public function setTaskId($taskId)
{
    $this->task_id = $taskId;

    return $this;
}

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

/**
 * Set user_id
 *
 * @param integer $userId
 * @return TaskDone
 */
public function setUserId($userId)
{
    $this->user_id = $userId;

    return $this;
}

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

}

Structure Task.php Entitie 结构Task.php实体

<?php

namespace Seotool\MainBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="tasks")
 */
class Task {

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string")
 */
protected $task_title;

/**
 * @ORM\Column(type="text")
 */
protected $task_description;


/**
 * @ORM\Column(type="string")
 */
protected $task_priority;

/**
 * @ORM\ManyToOne(targetEntity="TaskTypes", inversedBy="task")
 * @ORM\JoinColumn(name="tasktype", referencedColumnName="id")
 */
protected $TaskTypes;

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="task")
 * @ORM\JoinColumn(name="user", referencedColumnName="id")
 */
protected $User;

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

/**
 * Set task_title
 *
 * @param string $taskTitle
 * @return Task
 */
public function setTaskTitle($taskTitle)
{
    $this->task_title = $taskTitle;

    return $this;
}

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

/**
 * Set task_description
 *
 * @param string $taskDescription
 * @return Task
 */
public function setTaskDescription($taskDescription)
{
    $this->task_description = $taskDescription;

    return $this;
}

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

/**
 * Set task_priority
 *
 * @param string $taskPriority
 * @return Task
 */
public function setTaskPriority($taskPriority)
{
    $this->task_priority = $taskPriority;

    return $this;
}

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


/**
 * Set TaskTypes
 *
 * @param \Seotool\MainBundle\Entity\TaskTypes $taskTypes
 * @return Task
 */
public function setTaskTypes(\Seotool\MainBundle\Entity\TaskTypes $taskTypes = null)
{
    $this->TaskTypes = $taskTypes;

    return $this;
}

/**
 * Get TaskTypes
 *
 * @return \Seotool\MainBundle\Entity\TaskTypes 
 */
public function getTaskTypes()
{
    return $this->TaskTypes;
}

/**
 * Set User
 *
 * @param \Seotool\MainBundle\Entity\User $user
 * @return Task
 */
public function setUser(\Seotool\MainBundle\Entity\User $user = null)
{
    $this->User = $user;

    return $this;
}

/**
 * Get User
 *
 * @return \Seotool\MainBundle\Entity\User 
 */
public function getUser()
{
    return $this->User;
}


}

EDIT 2 编辑2

Current code of my controller, which result is an empty array: 我控制器的当前代码,结果是一个空数组:

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

    $qb = $em->createQuery("
        SELECT
            t
        FROM
            SeotoolMainBundle:Task t
        WHERE
            t.id IN (
                SELECT
                    td
                FROM
                    SeotoolMainBundle:TaskDone td
                WHERE
                    td.task_id = t.id
                AND
                    td.user_id = $id
            )

        AND
           t.User = $id
    ");
    $taskDone = $qb->getResult();
    $done = print_r($taskDone, true);

Table TaskDone has entries ( ID 1 | Task ID 36 | User ID 13 ) Task with ID 36 exists. 表TaskDone具有条目(ID 1 |任务ID 36 |用户ID 13)存在ID为36的任务。

I think your query should be more simple. 我认为您的查询应该更简单。

What do you want exactly? 你到底想要什么? All of the tasks which has been done by a specific user? 所有由特定用户完成的任务?

So, if you join these two tables and filter on the taskDone.user, it should works. 因此,如果您将这两个表连接起来并在taskDone.user上进行过滤,则它应该可以工作。

Let's try this. 让我们尝试一下。

SELECT 
    t
FROM
    SeotoolMainBundle:Tasks t, SeotoolMainBundle:TaskDone td
WHERE
    t.id=td.task_id
AND
    td.user = $userid

(be careful about the names I gave to your tables, I'm not sure) (不确定我给您表指定的名称,我不确定)

Perhaps, I did not understand what do you want. 也许,我不明白你想要什么。 If it the case, please be more accurate ;) 如果是这样,请更准确;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM