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.