Search code examples
phpsymfonysonata-admin

Symfony Sonata Admin Bundle - expected numeric


Hey my Seance entity have following fields :

- idseance
- day
- hour
- idmovie (references to Movie entity => id)
- idromm (references to Room entity => id)

When I want to list all seances everyting is ok, I see Movie title in col, but if I try to edit seance I have following error: Unable to transform value for property path "idmovie": Expected a numeric.

The same error occurs in idroom case.

Below is my configureFormField from SeanceAdmin

   $formMapper->add('day', 'datetime', ['label' => 'Data seansu', 'format' => 'y M d']);
    $formMapper->add('hour', 'time', ['label' => 'Godzina rozpoczęcia ']);
    $formMapper->add('discountticket', 'number', ['label' => 'Cena biletu ulgowego']);
    $formMapper->add('normalticket', 'number', ['label' => 'Cena biletu normalnego']);
    $formMapper->add('idmovie', 'integer', ['label' => 'Film']);
    $formMapper->add('idroom', 'integer', ['label' => 'Sala']);

If I remove idmovie and idroom, edit page load correctyl but I can't edit movie or room value of course.

Anyone have idea why it doestn't work correctly?


Solution

  • If it's not done, you need to write annotations for the idmovie/idromm fields.

    First, add this under your entity namespace : use Doctrine\ORM\Mapping as ORM;

    Then you have to annotate your fields like this :

     * @ORM\ManyToOne(targetEntity="YourBundle\Entity\Movie", cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_movie", referencedColumnName="id")
     * })
     /
     private $idMovie;
    

    Finally, in SeanceAdmin.php, you need to replace the integer type of your field by entity or sonata_type_model

    You can see more about form field types here -> https://sonata-project.org/bundles/admin/master/doc/reference/form_types.html