Question

I have 3 tables in DB: 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;

tasks:

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;

Entity files generated from existing database using following commands:

$ php app/console doctrine:mapping:import --force AcmeBlogBundle xml
$ php app/console doctrine:mapping:convert annotation ./src
$ php app/console doctrine:generate:entities AcmeBlogBundle

And I want to get results of this query:

SELECT * FROM task_estimation_fields tef LEFT JOIN task_estimations te ON (tef.id = te.task_estimation_field_id AND te.task_id = 1)

I am using symfony 2.4.3 and I have generated following code:

$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();

The problem is that i am getting following error:

[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

And thats true that in Entity there is no such field but there is:

/**
 * @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;

Which covers the relation.

Whene I remove relation from 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();

I am receiving:

[Semantical Error] line 0, col 124 near 'task_id = :i': Error: Class Synapthsis\SpecBundle\Entity\TaskEstimations has no field or association named task_id

Which is also true because in Entity it is covered by:

/**
 * @var \Synapthsis\SpecBundle\Entity\Tasks
 *
 * @ORM\ManyToOne(targetEntity="Synapthsis\SpecBundle\Entity\Tasks")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="task_id", referencedColumnName="id")
 * })
 */
private $task;

How to get results i need? How to print those results in twig?

Was it helpful?

Solution

In DQL queries you need to use the association name not the column name.

In your case you need to use te.taskEstimationField instead of te.task_estimation_field_id and te.task instead of te.task_id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top