1
votes

In a controller, I create a form:

$em = $this->getDoctrine()->getManager();

$formProjetSearch = $this->createForm(EgwProjetSearchType::class, $em, [
    'em' => $this->getDoctrine()->getManager(),
]);

In my EgwProjetSearchType, I have:

$builder->add('dispositif', 'entity', array(
  'class'   => 'LeaPrestaBundle:EgwDispositif', 
  'property'  => 'nomDispositif',
  'label'   => 'nomDispositif',
  'required'  => true,
  'empty_value' => '',
  'query_builder' => function(EntityRepository $er) 
  {
    return $er->createQueryBuilder('d')
      ->where('d.isActive = :isActive')
      ->setParameter('isActive', 1)
      ->orderBy('d.nomDispositif','ASC');
  },
)); 

And I've got this error:

Neither the property "dispositif" nor one of the methods "getDispositif()", "dispositif()", "isDispositif()", "hasDispositif()", "__get()" exist and have public access in class "Doctrine\ORM\EntityManager".

Nonetheless, the entity exists:

<?php

namespace Lea\PrestaBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\ORM\Mapping as ORM;

/**
 * Lea\PrestaBundle\Entity\EgwDispositif
 *
 * @ORM\Table(name="egw_dispositif")
 * @ORM\Entity
 */
class EgwDispositif
{
  /**
   * @var integer $idDispositif
   *
   * @ORM\Column(name="id_dispositif", type="integer", nullable=false)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="IDENTITY")
   */
  private $idDispositif;

  /**
  * @ORM\ManyToOne(targetEntity="EgwTypePrestation", inversedBy="dispositifs")
  * @ORM\JoinColumn(name="id_type_prestation", referencedColumnName="id")
  */
  private $idTypePrestation;

ETC ...

Thanks for your help !

Thakns for your messages, but, I just want to display in a form a entity in a listbox : i use the type EgwProjetSearchType, i add a field which is "dispositif" coming from entity EgwDispositif (which exists) and the return message is :

Neither the property "dispositif" nor one of the methods "getDispositif()", "dispositif()", "isDispositif()", "hasDispositif()", "__get()" exis``t and have public access in class "Doctrine\ORM\EntityManager"

So it's not a problem of argument EM passed in the form EgwProjetSearchType : Symfony says "the entity doesnt exists"....

I dont have to pass EwgDispositif ?? It was not the cas in Symfony 2 : i had :

$formProjetSearch = $this->createForm(new EgwProjetSearchType($this- 

getDoctrine()->getManager()));

And this doesnt work anymore in 3.4. So i changed the code :

$formProjetSearch = $this->createForm(EgwProjetSearchType::class, $em, [
'em' => $this->getDoctrine()->getManager(),
]);
1
The second argument to createForm expects the data to be handled (e.g. your entity [though beware best practices]), you're passing the entity manager, which is probably not what you want.Yoshi
That's quite alright, just pass it in the options array, as you're already doing. 'em' => $this->getDoctrine()->getManager(),.Yoshi
Again, you're passing $em as the second parameter to createForm. As per your code $em is the entity manager which obviously is not the form data! What you need is (for example), something like: $this->createForm(EgwProjetSearchType::class, new EgwDispositif(), [ 'em' => $this->getDoctrine()->getManager(), ]);Yoshi
if you fixed the above issue with passing the entity manager as parameter and are now passing the actual entity instance there instead - you should also fix the form builder add to use "idDispositif" instead of "dispositif" (the addition should match your entity property).ejuhjav

1 Answers

0
votes

In your entity you need to define your getters and setters. Your entity class is private so you must define public getters and setters to access the private objects.

https://symfony.com/doc/current/doctrine.html