0

Can't create simple query for DB.

My Entity Givetask:

<?php

namespace RoSky\Bundle\GwsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * GivenTask
 */
class GivenTask
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $squad;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $task;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->squad = new \Doctrine\Common\Collections\ArrayCollection();
        $this->task = new \Doctrine\Common\Collections\ArrayCollection();
    }

...GETTER SETTERS

My Entity Squad:

<?php

namespace RoSky\Bundle\GwsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Squad
 */
class Squad
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

.....GETTERS SETTERS

My .yaml config GivenTask Entity:

RoSky\Bundle\GwsBundle\Entity\GivenTask:
    type: entity
    fields:
        id:
          id: true
          type: integer
          generator:
            strategy: AUTO
    manyToMany:
        squad:
          targetEntity: Squad
          joinTable:
            name: SquadToGivenTask
            joinColumns:
              given_task_id:
                referencedColumnName: id
                nullable: false
            inverseJoinColumns:
              squad_id:
                referencedColumnName: id
                nullable: false
        task:
          targetEntity: Task
          joinTable:
            name: TaskToGivenTask
            joinColumns:
              given_task_id:
                referencedColumnName: id
                nullable: false
            inverseJoinColumns:
              task_id:
                referencedColumnName: id
                nullable: false
    lifecycleCallbacks: {  }

My .yaml config Squad Entity:

RoSky\Bundle\GwsBundle\Entity\Squad:
    type: entity
    fields:
        id:
          id: true
          type: integer
          generator:
            strategy: AUTO
        name:
          type: string
          length: 100
          nullable: false
    lifecycleCallbacks: { }

Now i trying to make a Query...

$test = $this->em->getRepository('RoSkyGwsBundle:GivenTask')->findBySquad(4);

And... I got Doctrine Exception:

ContextErrorException: Notice: Undefined index: joinColumns in /home/DEA7H/Documents/Server/GraphWebSystem/www/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1665

WHAT IS THIS? =)

Details:

Symfony: 2.4

Doctrine: 2.2.3

ORDBMS: PostgreSQL 9.2

Thanks in Advance.

  • Can you post the Squad entity yml as well? – Kal Zekdor May 30 '14 at 15:39
  • Done! Edit post, add Squad.yaml – HeavenShallBurn May 30 '14 at 21:32
  • IDK, maybe this help better: https://github.com/OlshevskyR/GraphWebSystem/tree/master/src/RoSky/Bundle/GwsBundle – HeavenShallBurn May 30 '14 at 21:39
  • I'm assuming findByTask() works as expected? – Kal Zekdor May 31 '14 at 19:23
  • If findByTask() works but findBySquad() doesn't, I'd check your database configuration. Try running `php app/console doctrine:schema:validate` – Kal Zekdor May 31 '14 at 19:29
  • `$test=$this->managerRegistry->getManager()->getRepository('RoSkyGwsBundle:GivenTask')->findByTask(4);`SAME result.`Error "ContextErrorException" in BasicEntityPersister.php line 1665.`schema:validate result: `php console doctrine:schema:validate [Mapping] OK - The mapping files are correct. [Database] FAIL - The database schema is not in sync with the current mapping file.".` I see FAIL. But if i do: `doctrine:schema:update --force i got php console doctrine:schema:update --force Updating database schema... Database schema updated successfully! "2" queries were executed every time.` SUCCESS – HeavenShallBurn May 31 '14 at 19:46
  • This is a shot in the dark, but try renaming the joins in your GivenTask.orm.yml from `squad:` and `task:` to `squads:` and `tasks:` – Kal Zekdor May 31 '14 at 20:27
  • NOPE, not working. =*( – HeavenShallBurn Jun 01 '14 at 06:09
  • I think I see the problem, I was thinking about this backwards (and presumably you were as well). When you do a findBySquad() on the GivenTask repository, what Doctrine does is looks up the given Squad, and attempts to use the Join Table to get the GivenTask (essentially calling getGivenTask() on a Squad entity). However, your joins are unidirectional, and can't be walked backwards. See the doctrine documentation: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional Let me know if this solves it, and I'll write up an answer. – Kal Zekdor Jun 02 '14 at 21:08

1 Answers1

0

The problem you are having is likely based on misunderstanding the directionality of the call you are trying to make.

Calling $this->em->getRepository('RoSkyGwsBundle:GivenTask')->findBySquad(4); will first lookup the Squad Entity with id 4 and attempt a join with the GivenTask table. This is in contrast to selecting GivenTasks who match the criteria of being linked to Squad 4, which could be accomplished by a WHERE clause on a SELECT of GivenTasks, a left join from GivenTask -> Squad, or by simply iterating over $this->em->getRepository('RoSkyGwsBundle:GivenTask')->findAll(); for GivenTasks matching Squad 4.

The directionality of the findBySquad command is from Squad to GivenTask. However, GivenTask is the Owning (or Principal) entity, and your entity relationship is many-many unidirectional. This means that Doctrine can only find Squads based on GivenTasks, not the other way around.

In order to resolve this, you'll need to change your join into a many-many bidrectional, as exemplified in this section of the Doctrine documentation: Many-to-Many Bidirectional Mapping

Essentially, you'll need to modify your Squad.orm.yml file to the following:

RoSky\Bundle\GwsBundle\Entity\Squad:
    type: entity
    fields:
        id:
          id: true
          type: integer
          generator:
            strategy: AUTO
        name:
          type: string
          length: 100
          nullable: false
    manyToMany:
        givenTasks:
            targetEntity: GivenTask
            mappedBy: squad
    lifecycleCallbacks: { }
Kal Zekdor
  • 1,172
  • 2
  • 13
  • 23
  • No man, it's not working. Make a bi-directional: https://github.com/OlshevskyR/GraphWebSystem/commit/589d8101fc2cf908f004700f763e36f5b8b93373 Got same error: `Undefined index: joinColumns in /home/DEA7H/Documents/Server/GraphWebSystem/www/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1665` – HeavenShallBurn Jun 05 '14 at 09:58
  • Did you update your database schema after making that change? – Kal Zekdor Jun 05 '14 at 17:23
  • If that still doesn't work, I would just use a DQL query with a join, it'll be less of a headache. You could even manually write it in the GivenTask repository as a findBySquad method. – Kal Zekdor Jun 05 '14 at 17:33