Php Symfony 2理论左连接

Php Symfony 2理论左连接,php,symfony,doctrine-orm,left-join,Php,Symfony,Doctrine Orm,Left Join,我在DB中有3个表: 任务\估计\字段: 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; 任务估计: CREATE TABLE `task_estimations` (

我在DB中有3个表: 任务\估计\字段:

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;
任务估计:

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;
如何获得我需要的结果?
如何在twig中打印这些结果?

在DQL查询中,您需要使用关联名称,而不是列名


在您的情况下,您需要使用
te.taskEstimationField
而不是
te.task\u estimation\u field\u id
te.task
而不是
te.task\u id

在查询中使用实体字段名
te.task=:id
Works:)THX:D以及如何在twig中打印它?我在TWIG中使用这样的代码:{%for estimations%}{{es.description2}
{%endfor%},我得到了对象“Synapthsis\SpecBundle\Entity\TaskEstimationFields”的方法“description”在SynapthsSpecBundle:TaskEstimation:index.html.twig的第7行中不存在。你应该问一个单独的问题——它们之间没有太大的关联。不管怎样,我都需要从你的控制器中看到更多的代码——我不知道你是如何将查询结果传递给twig的,我在这里创建了一个新问题,详细描述如下: