Expected argument of type "App\Entity\Modulo", "string" given at property path "modulos"

27 Views Asked by At

`When i try to add a new record of acao (course) that has a many to many relationship with modulos, i get the error Expected argument of type "App\Entity\Modulo", "string" given at property path "modulos".

Acao class

<pre><code>
<?php

namespace App\Entity;

use App\Repository\AcaoRepository;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

#[ORM\Entity(repositoryClass: AcaoRepository::class)]
class Acao
{   
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column]
    private ?int $number = null;

    #[ORM\Column(length: 255)]
    private ?string $nome = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNumber(): ?int
    {
        return $this->number;
    }

    public function setNumber(int $number): self
    {
        $this->number = $number;
        return $this;
    }

    public function getNome(): ?string
    {
        return $this->nome;
    }

    public function setNome(string $nome): self
    {
        $this->nome = $nome;
        return $this;
    }

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Modulo")
     * @ORM\JoinTable(name="acao_modulo",
     *      joinColumns={@ORM\JoinColumn(name="acao_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="modulo_id", referencedColumnName="id")}
     * )
     */
    private $modulos;

    // ...

    public function __construct()
    {
        $this->modulos = new ArrayCollection();
    }

    /**
     * @return Collection|Modulo[]
     */
    public function getModulos(): Collection
    {
        return $this->modulos ?: new ArrayCollection();
    }

    public function addModulo(Modulo $modulo): self
    {
        if (!$this->modulos->contains($modulo)) {
            $this->modulos[] = $modulo;
        }

        return $this;
    }

    public function removeModulo(Modulo $modulo): self
    {
        $this->modulos->removeElement($modulo);

        return $this;
    }

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


AcaoControllerCRUD
<?php

namespace App\Controller\Admin;

use App\Entity\Acao;
use App\Form\ModuloType;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;


class AcaoCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Acao::class;
    }

    
    public function configureFields(string $pageName): iterable
    {
        return [
            NumberField::new('number'),
            TextField::new('nome'),
            
            // Define the Many-to-Many relationship field
            CollectionField::new('modulos')
                //->setEntryType(ModuloType::class) // Replace ModuloType with your form type for Modulo entity
                ->setEntryIsComplex(false) // Set to true if related entity is complex
                ->setFormTypeOptions([
                    'by_reference' => false, // Ensure that the form is processed correctly for Many-to-Many
                ])
                ->onlyOnForms(), // Display this field only on forms
        ];
    }

}

ModuloEntity

<?php

namespace App\Entity;

use App\Repository\ModuloRepository;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

#[ORM\Entity(repositoryClass: ModuloRepository::class)]
class Modulo
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column]
    private ?int $number = null;

    #[ORM\Column(length: 255)]
    private ?string $nome = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNumber(): ?int
    {
        return $this->number;
    }

    public function setNumber(int $number): static
    {
        $this->number = $number;

        return $this;
    }

    public function getNome(): ?string
    {
        return $this->nome;
    }

    public function setNome(string $nome): static
    {
        $this->nome = $nome;

        return $this;
    }

    /**
     * @ORM\ManyToMany(targetEntity=Acao::class, mappedBy="modulos")
     */
    private $acoes;

    public function __construct()
    {
        $this->acoes = new ArrayCollection();
    }

    /**
     * @return Collection|Acao[]
     */
    public function getAcoes(): Collection
    {
        return $this->acoes;
    }

    public function addAcao(Acao $acao): self
    {
        if (!$this->acoes->contains($acao)) {
            $this->acoes[] = $acao;
            $acao->addModulo($this);
        }

        return $this;
    }

    public function removeAcao(Acao $acao): self
    {
        if ($this->acoes->removeElement($acao)) {
            $acao->removeModulo($this);
        }

        return $this;
    }
}


</code></pre>

I dont understand the error I´m getting. I passing a instance of modulos not a string in my crud controller.

1

There are 1 best solutions below

0
MoWiz On

I think you can try the AssociationField instead of the CollectionField in your CRUD Controller:

<?php

namespace App\Controller\Admin;

use App\Entity\Acao;
use App\Form\ModuloType;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;


class AcaoCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Acao::class;
    }

    
    public function configureFields(string $pageName): iterable
    {
        return [
            NumberField::new('number'),
            TextField::new('nome'),
            
            // Define the Many-to-Many relationship field
            AssociationField::new('modulos')
// Add your Options here
               
        ];
    }

}

That way EasyAdmin will map entities in your CRUD. I hope that will help :)