2
votes

I'm looking for the good way to use KnplabsDoctrineBehaviors with SonataAdmin.

I have already render a form in sonata admin bundle with the help of this bundle : https://github.com/a2lix/TranslationFormBundle It works fine and add correctly an entity with its translations.

To list entities in sonata I found the hack from this but when I add sortable property to Nom in listMapper of Sonata-admin it does not work.

class Sport
{

     use \Knp\DoctrineBehaviors\Model\Translatable\Translatable;

     public function __call($method, $arguments)
    {
        return $this->proxyCurrentLocaleTranslation($method, $arguments);
    }   

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     private $id;

    // Need this method for the admin list template
    public function getNom(){
        return $this->translate()->getNom();
    }

    // Work even the precedent method not here, the proxy call work fine.
    public function __toString(){
        return $this->getNom();
    }
}

class SportTranslation
{
    use ORMBehaviors\Translatable\Translation;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $nom;

    /**
     * @return string
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * @param  string
     * @return null
     */
     public function setNom($nom)
     {
         $this->nom = $nom;
     }
}

When I try to sort by Nom I get this error :

Catchable Fatal Error: Argument 1 passed to
Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery::entityJoin()
must be of the type array, null given, called in
.../vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Datagrid/ProxyQuery.php
on line 140
and defined in
.../vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Datagrid/ProxyQuery.php
line 245

I guess it's because Nom is not in Sport and I don't know how to handle this.

2

2 Answers

1
votes

A bit slow on the response as I have only just encountered this problem myself. I have found the solution to this is actually rather simple. In my case I wanted to be able to filter by article status and title, of which the title is a translatable field. The way I managed to get this to work was to explicitly use the translations relationship and specify fields on this child entity, as follows:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('translations.title', null, array('label' => 'Title'))
        ->add('status');
}

I hope this helps someone else in this situation...

0
votes

I finally switch to https://github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle which is from my point of view more flexible. Don't know if your solution works but when sorting most of the time it takes all translation into account.

For example:

  • Nom1 : fr : test1
  • Nom1 : en : test1
  • Nom2 : fr : test2
  • Nom2 : en : test0

when you want to sort by nom it will result with this :

  • Nom2 : en : test0
  • Nom1 : fr : test1
  • Nom1 : en : test1
  • Nom2 : fr : test2

or

this:

  • Nom2 : en : test2
  • Nom1 : fr : test1
  • Nom1 : en : test1
  • Nom2 : fr : test0

and so it will always show Nom2 before Nom1 as it does not take into account the language. I was able to solve this easily with symfony2admingenerator so I switch :-)