我在DB中有3个表: task_estimation_fields:
CREATE TABLE `task_estimation_fields` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB;
task_estimations:
CREATE TABLE `task_estimations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`task_id` int(11) NOT NULL,
`task_estimation_field_id` int(11) NOT NULL,
`description` blob,
`summary` blob,
`effort` int(11) NOT NULL,
`created_at` datetime NOT NULL,
`created_by` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `g1` (`task_id`),
KEY `g2` (`created_by`),
KEY `g3` (`task_estimation_field_id`),
CONSTRAINT `g1` FOREIGN KEY (`task_id`) REFERENCES `tasks` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `g2` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `g3` FOREIGN KEY (`task_estimation_field_id`) REFERENCES `task_estimation_fields` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
任务:
CREATE TABLE `tasks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`assignee_id` int(11) NOT NULL,
`status_id` int(11) NOT NULL,
`prioryty_id` int(11) NOT NULL,
`title` varchar(45) NOT NULL,
`description` longblob,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
使用以下命令从现有数据库生成的实体文件:
$ php app/console doctrine:mapping:import --force AcmeBlogBundle xml
$ php app/console doctrine:mapping:convert annotation ./src
$ php app/console doctrine:generate:entities AcmeBlogBundle
我希望获得此查询的结果:
SELECT * FROM task_estimation_fields tef LEFT JOIN task_estimations te ON (tef.id = te.task_estimation_field_id AND te.task_id = 1)
我正在使用symfony 2.4.3并且我已经生成了以下代码:
$estimations = $this->getDoctrine()
->getManager()
->createQueryBuilder()
->select('tef, te')
->from('SynapthsisSpecBundle:TaskEstimationFields', 'tef')
->leftJoin('SynapthsisSpecBundle:TaskEstimations', 'te', 'WITH', 'tef.id = te.task_estimation_field_id AND te.task_id = :id')
->setParameter('id', $id)
->getQuery()
->getResult();
问题是我遇到了以下错误:
[Semantical Error] line 0, col 133 near 'task_estimation_field_id': Error: Class Synapthsis\SpecBundle\Entity\TaskEstimations has no field or association named task_estimation_field_id
这就是说,在实体中没有这样的领域,但有:
/**
* @var \Synapthsis\SpecBundle\Entity\TaskEstimationFields
*
* @ORM\ManyToOne(targetEntity="Synapthsis\SpecBundle\Entity\TaskEstimationFields")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="task_estimation_field_id", referencedColumnName="id")
* })
*/
private $taskEstimationField;
涵盖了这种关系。
我从queryBuilder中删除了关系:
$estimations = $this->getDoctrine()
->getManager()
->createQueryBuilder()
->select('tef, te')
->from('SynapthsisSpecBundle:TaskEstimationFields', 'tef')
->leftJoin('SynapthsisSpecBundle:TaskEstimations', 'te', 'WITH', 'te.task_id = :id')
->setParameter('id', $id)
->getQuery()
->getResult();
我收到了:
[Semantical Error] line 0, col 124 near 'task_id = :i': Error: Class Synapthsis\SpecBundle\Entity\TaskEstimations has no field or association named task_id
这也是事实,因为在实体中它包含在:
/**
* @var \Synapthsis\SpecBundle\Entity\Tasks
*
* @ORM\ManyToOne(targetEntity="Synapthsis\SpecBundle\Entity\Tasks")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="task_id", referencedColumnName="id")
* })
*/
private $task;
如何获得我需要的结果? 如何在树枝上打印这些结果?
答案 0 :(得分:1)
在DQL查询中,您需要使用关联名称而不是列名称。
在您的情况下,您需要使用te.taskEstimationField
代替te.task_estimation_field_id
和te.task
代替te.task_id
。