Too many db queries with DoctrineExtensions Translatable Symfony2

1.2k views Asked by At

I just installed the Translatable DoctrineExtensions from the StofDoctrineExtensionsBundle.

I have two entities "Space" and "Equipment" with many to one relation :

/**
 * Space
 * @ORM\Table(name="space")
 * @ORM\Entity(repositoryClass="Project\ProjectBundle\Repository\PlaceRepository")
 */
class Space implements Translatable
{
    /**
     * @var integer
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


     /**
     * @ORM\OneToMany(targetEntity="Project\ProjectBundle\Entity\Equipment", cascade={"persist"})
     */
     private $equipments;

    /**
     * @var string
     * @Gedmo\Translatable
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     */
    private $name;

    /**
     * @Gedmo\Locale
     */
    private $locale;

    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }
}

And :

/**
 * Equipment
 * @ORM\Table("equipment")
 * @ORM\Entity(repositoryClass="Project\ProjectBundle\Repository\EquipmentRepository")
 */
class Equipment implements Translatable
{
    /**
     * @var integer
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @Gedmo\Translatable
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     */
    private $name;

    /**
     * @Gedmo\Locale
     */
    private $locale;

    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }
}

In the spaceRepository, I have, in my get query function :

public function getSpace($id) {
    $query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker');
    $query->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,$locale);
    $db = $this
           ->createQueryBuilder('s')
           ->andWhere('s.id = :id')
           ->setParameter('id', $id)
           ->leftJoin('s.equipments', 'e')
           ->addSelect('e');

    $query = $db->getQuery();
    $query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker');
    $query->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE, 'en');

    return $query->getSingleResult();
}

The TranslationWalker should get all the results in one single query but it doesn't work, and, finally, I have one query for each equipment (more than 150 queries).

How could I get all translated elements in one single query ?

1

There are 1 answers

0
takeit On

Try to set the hydration mode manually as your issue is related to this problem.

Take a look at this answer on how to set the hydration mode. It worked for me.

In my case I simply added the following hints to a query

use Doctrine\ORM\Query;
use Gedmo\Translatable\Query\TreeWalker\TranslationWalker;
...
$query->setHydrationMode(TranslationWalker::HYDRATE_OBJECT_TRANSLATION);
$query->setHint(Query::HINT_REFRESH, true);

Hope that helps!