0

I have two classes:

class Employee
{
    use Knp\DoctrineBehaviors\Model\Translatable\Translatable;
    //...

    /**
     * @ORM\ManyToOne(targetEntity="Employee", inversedBy="employeeSubs")
     * @ORM\JoinColumn(name="employee_id", referencedColumnName="id", onDelete="CASCADE")
     **/
    protected $employeeMain;

    /**
     * @ORM\OneToMany(targetEntity="Employee", mappedBy="employeeMain")
     */
    protected $employeeSubs;
    //...
}

class Room
{
    use Knp\DoctrineBehaviors\Model\Translatable\Translatable;

    //...

    /**
    * @var \Doctrine\Common\Collections\ArrayCollection
    *
    * @ORM\ManyToMany(targetEntity="Employee", inversedBy="rooms")
    * @ORM\JoinTable(name="room_employee",
    *   joinColumns={
    *     @ORM\JoinColumn(name="room_id", referencedColumnName="id")
    *   },
    *   inverseJoinColumns={
    *     @ORM\JoinColumn(name="employee_id", referencedColumnName="id")
    *   }
    * )
    */
    protected $employees; 

    //room...
}

Both classes use translations from Knp\DoctrineBehaviors so I have a problem with too many queries in Sonata Admin (RoomAdmin).

If I have relation OneToMany without OneToMany in Employee then I can in admin class:

public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);

        $query
            ->addSelect('translations, employeeTranslations')
            ->leftJoin($query->getRootAlias().'.translations', 'translations')
        ->leftJoin($query->getRootAlias().'.employees', 'Employee')
        ->leftJoin('Employee.translations', 'employeeTranslations');
        return $query;
    }

Then the number of queries is reduced and everything is fine.

For ManyToMany (Room) and OneToMany (Employee) this not working.

yisidejug
  • 1
  • 1
  • I can't see the oneToOne relation in your entity, please can update the code posted to see all these details. In the other hand "this not working. " is redundant, please show the error or explain what is happen. – rafrsr May 08 '17 at 12:42
  • Sorry, I updated my question. – yisidejug May 08 '17 at 12:44

0 Answers0