简体   繁体   中英

Argument 1 passed to App\Entity\Invitacion::setEmail() must be an instance of App\Entity\User or null - Symfony 4

I am new to Symfony4. I have a problem related to the value set in the table. I have been following the Symfony documentation. I want to store the user's email and codeinvitations variable in the invitation table, but it gives me an error.

Please help me to solve my problems.

These are my entities

App / Entity / User.php


class User implements UserInterface, \Serializable
{
    (...)

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     * @ORM\OneToMany(targetEntity="App\Entity\Invitacion", mappedBy="user")
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     * @ORM\OneToMany(targetEntity="App\Entity\Invitacion", mappedBy="user")
     */
    private $codeinvitaciones;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Invitacion", mappedBy="user")
     */
    private $invitaciones;

    /**
     * User constructor
     */
    public function __construct()
    {
        $this->invitaciones = new ArrayCollection();
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getCodeinvitaciones(): ?string
    {
        return $this->codeinvitaciones;
    }

    public function setCodeinvitaciones(string $codeinvitaciones): self
    {
        $this->codeinvitaciones = $codeinvitaciones;

        return $this;
    }

    /**
     * @return Collection|Invitacion[]
     */
    public function getInvitaciones(): Collection
    {
        return $this->invitaciones;
    }

    public function addInvitaciones(Invitacion $invitacion): self
    {
        if (!$this->invitaciones->contains($invitacion)) {
            $this->invitaciones[] = $invitacion;
            $invitacion->setCodeinvitaciones($this);
        }

        return $this;
    }

}

App / Entity / Invitacion.php


class Invitacion
{
    (...)

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="email")
     * @ORM\JoinColumn(nullable=false)
     */
    private $email;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="invitaciones")
     * @ORM\JoinColumn(nullable=false)
     */
    private $codeinvitaciones;


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

    public function getEmail(): ?User
    {
        return $this->email;
    }

    public function setEmail(?User $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getCodeinvitaciones(): ?User
    {
        return $this->codeinvitaciones;
    }

    public function setCodeinvitaciones(User $codeinvitaciones): self
    {
        $this->codeinvitaciones = $codeinvitaciones;

        return $this;
    }

}


In the Controller I create the user when registering in a form. At the same time, I store the email and the variable codeinvitations in the table invitation but it gives me an error.

Controller

(...)
$invitacion->setEmail($email);
$invitacion->setCodeinvitaciones($registroInv);

ERROR

Argument 1 passed to App\Entity\Invitacion::setEmail() must be an instance of App\Entity\User or null,
string given, called in C:\Users\adria\Documents\app\bebeBB\src\Controller\UserController.php on line 38

thanks

--------------EDIT-----------

I had already tried passing Invitacion::setEmail(?string) but it gives this error:

Expected value of type "App\Entity\User" for association field
"App\Entity\Invitacion#$codeinvitaciones", got "string" instead.

As already said in the comments setEmail(?User email) takes null or an User-object as input. I would suspect that you would want to enter the email-address there, so you could do:

Setter:

setEmail(?string $email)

Call:

$user = new User();
$user->setEmail('test@test.com');
$invitacion->setEmail($user->getEmail());

or if you really want to add a full user object as it is:

$user = new User();
$user->setEmail('test@test.com');
$invitacion->setEmail($user);

In both cases their has to be a user object first. Then you could access the object or mail for your setter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM