0
votes

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.

1
Can you post the Squad entity yml as well?Kal Zekdor
Done! Edit post, add Squad.yamlHeavenShallBurn
I'm assuming findByTask() works as expected?Kal Zekdor
If findByTask() works but findBySquad() doesn't, I'd check your database configuration. Try running php app/console doctrine:schema:validateKal Zekdor

1 Answers

0
votes

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: { }