1
resposta

[Dúvida] Problema ao listar instrutores

Olá.

Estou praticando outras possiveis entidades neste mini mundo do projeto de cursos, onde implementei o Instrutor ministrando cursos. Criei uma propriedade email do tipo Email e ao listar os instrutores estou recebendo a seguinte mensagem de erro abaixo.

vagrant@homestead:~/code/doctrine-orm$ php bin/list-instructor.php
PHP Fatal error:  Uncaught TypeError: Cannot assign string to property Alura\Doctrine\Entity\Instructor::$email of type Alura\Doctrine\Entity\Email in /home/vagrant/code/doctrine-orm/vendor/doctrine/persistence/src/Persistence/Reflection/RuntimeReflectionProperty.php:59

Segue minha classe de Instrutor:

<?php

namespace Alura\Doctrine\Entity;

use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToMany;

#[Entity]
class Instructor 
{
    #[Id, Column, GeneratedValue]
    public int $id;

    #[ManyToMany(targetEntity: Course::class, inversedBy: 'instructors')]
    private Collection $courses;

    public function __construct(
        #[Column(unique: true)]
        public string $name,
        #[Column(type: 'string', unique: true, nullable: true, options: ['default'=>null])]
        public ?Email $email = null,
        #[Column(type:'date', nullable: true)]
        public ?DateTime $birth_date = null,
    )
    {
        $this->courses = new ArrayCollection();
    }

    public function courses(): Collection
    {
        return $this->courses;
    }

    public function ministerInCourse(Course $course): void
    {
        if($this->courses->contains($course)){
            return;
        }
        $this->courses->add($course);
        $course->addInstructor($this);
    }
}

Segue a classe de Email:

<?php 

namespace Alura\Doctrine\Entity;

use InvalidArgumentException;
use Stringable;

class Email implements Stringable
{
    public function __construct(public string $description)
    {
        if(FALSE === filter_var($description, FILTER_VALIDATE_EMAIL)){
            throw new InvalidArgumentException("Email '$description' is invalid");
        }
    }

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

Segue o arquivo de listar instrutores:

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Alura\Doctrine\Entity\Instructor;
use Alura\Doctrine\Helper\EntityManagerCreator;

$entity_manager = EntityManagerCreator::createEntityManager();

$instructor_repository = $entity_manager->getRepository(Instructor::class);

/**
 * @var Instructor[] $instructor_list
 */
$instructor_list = $instructor_repository->findAll();

foreach ($instructor_list as $instructor){
    echo "ID: $instructor->id\n";
    echo "Name: $instructor->name\n";
    echo "E-mail: $instructor->email\n";
    echo "Cursos: \n";
    foreach($instructor->courses() as $course){
        echo $course->name . PHP_EOL;
    }
    echo PHP_EOL.PHP_EOL;
}
1 resposta

O erro parece está relacionado com o tipo de dado definido para o email.