有人可以帮助我将我的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
我尝试了什么:
$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();
这引发以下异常:
[语义错误]第0行,第71行附近的任务t ':错误:课程'任务'没有定义。
My Entites被称为:Task.php和TaskDone.php,Classname与filename相同。 表名是:tasks和task_done
修改
结构TaskDone.php Entite
<?php
命名空间Seotool \ MainBundle \ Entity;
使用Doctrine \ Common \ Collections \ ArrayCollection; 使用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;
}
}
结构Task.php Entitie
<?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;
}
}
编辑2
我的控制器的当前代码,结果是一个空数组:
$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);
表TaskDone有条目(ID 1 |任务ID 36 |用户ID 13) 存在ID为36的任务。
答案 0 :(得分:2)
我认为您的查询应该更简单。
你到底想要什么? 特定用户完成的所有任务?
所以,如果你加入这两个表并在taskDone.user上过滤,它应该可以工作。
我们试试这个。
SELECT
t
FROM
SeotoolMainBundle:Tasks t, SeotoolMainBundle:TaskDone td
WHERE
t.id=td.task_id
AND
td.user = $userid
(小心我给你的桌子的名字,我不确定)
也许,我不明白你想要什么。如果是这样,请更准确;)