Question

i have a Fonctionnaire entity class that contains ManyToOne property to Grade entity class. when i want to add the idGrade field to ConfigureListField of FonctionnaireAdmin (using SonataAdmin) i got this error:

An exception has been thrown during the rendering of a template ("You must define an associated_property option or create a Proxies\__CG__\Examens\ExamensBundle\Entity\Grade::__toString method to the field option idGrade from service examens.examens.admin.fonctionnaire is ") in SonataDoctrineORMAdminBundle:CRUD:list_orm_many_to_one.html.twig at line 19.

Fontionnaire.php class:

<?php

 namespace Examens\ExamensBundle\Entity;


 use Doctrine\ORM\Mapping as ORM;

 /**
 * Fonctionnaire
  */
class Fonctionnaire
{
 //...//

 /**
 * @var \Examens\ExamensBundle\Entity\Grade
 */
private $idGrade;
//...//
}

FonctionnaireAdmin.php:

    <?php

    namespace Examens\ExamensBundle\Admin;

    use Sonata\AdminBundle\Admin\Admin;
    use Sonata\AdminBundle\Datagrid\ListMapper;
    use Sonata\AdminBundle\Datagrid\DatagridMapper;
    use Sonata\AdminBundle\Validator\ErrorElement;
    use Sonata\AdminBundle\Form\FormMapper;
    use Sonata\AdminBundle\Show\ShowMapper;
    use Examens\ExamensBundle\Entity\Fonctionnaire;

class FonctionnaireAdmin extends Admin
{
     protected $datagridValues = array(
        '_sort_order' => 'ASC',
        '_sort_by' => 'codeFonctionnaire'
     );
     protected function configureFormFields(FormMapper $formMapper)
     {
        $formMapper
        ->add('nomAr','text',array('label'=>'Nom arabe'))
        ->add('prenomAr','text',array('label'=>'Prénom arabe'))
        ->add('nomFr','text',array('label'=>'Nom français '))
        ->add('prenomFr','text',array('label'=>'Prénom français'))
        ->add('nomUtilisateur','text',array('label'=>'nom Utilisateur'))
        ->add('motDePasse','text',array('label'=>'motDePasse'))
        ->add('idGrade','entity',
            array('class'   => 'Examens\ExamensBundle\Entity\Grade',
                'property'  => 'libGradeAr'))
        ->add('dateNominationGrade','date',
                 array('years' => range(1980, date('Y'))))

        ;
     }

     protected function configureListFields(ListMapper $listMapper)
     {
        $listMapper
        ->add('nomAr')
        ->add('prenomAr')
        ->add('nomFr')
        ->add('prenomFr')
        ->add('nomUtilisateur')
        ->add('motDePasse')
        ->add('idGrade')
        ->add('dateNominationGrade')


        ;
     }

}
Was it helpful?

Solution 2

You just need to add a __toString() method to your Grade class. Something like :

public function __toString() {
    return $this->nomAr;
}

It will be used by SonataAdminBundle to display the name of your current object.

OTHER TIPS

You can just add:

->add('idGrade', null, array('associated_property' => 'libGradeAr'))

Don't forget you also can use it as follow to prevent Sonata from generating a name for you when creating a new object:

public function __toString {
    // Show name if it exist otherwise just show a custom label
    return $this->nomAr ? $this->nomAr : 'Custom new label name';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top