Ao fazer os exercícios do capítulo 4, Relacionamento ManyToMany, me deparo com este erro quando tento inserir um aluno com telefone.
An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: Phone.phoneNumber#0 C:\Users\bruno\eclipse-workspace 2\php-pdo-projeto-inicial\vendor\doctrine\dbal\src\Connection.php(1822): Doctrine\DBAL\Driver\API\SQLite\ExceptionConverter->convert(Object(Doctrine\DBAL\Driver\PDO\Exception), Object(Doctrine\DBAL\Query))
#1 C:\Users\bruno\eclipse-workspace 2\php-pdo-projeto-inicial\vendor\doctrine\dbal\src\Connection.php(1763): Doctrine\DBAL\Connection->handleDriverException(Object(Doctrine\DBAL\Driver\PDO\Exception), Object(Doctrine\DBAL\Query))
#2 C:\Users\bruno\eclipse-workspace 2\php-pdo-projeto-inicial\vendor\doctrine\dbal\src\Statement.php(193): Doctrine\DBAL\Connection->convertExceptionDuringQuery(Object(Doctrine\DBAL\Driver\PDO\Exception), 'INSERT INTO Pho...', Array, Array)
#3 C:\Users\bruno\eclipse-workspace 2\php-pdo-projeto-inicial\vendor\doctrine\dbal\src\Statement.php(248): Doctrine\DBAL\Statement->execute(NULL)
#4 C:\Users\bruno\eclipse-workspace 2\php-pdo-projeto-inicial\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(278): Doctrine\DBAL\Statement->executeStatement()
#5 C:\Users\bruno\eclipse-workspace 2\php-pdo-projeto-inicial\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(1133): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts()
#6 C:\Users\bruno\eclipse-workspace 2\php-pdo-projeto-inicial\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(430): Doctrine\ORM\UnitOfWork->executeInserts(Object(Doctrine\ORM\Mapping\ClassMetadata))
#7 C:\Users\bruno\eclipse-workspace 2\php-pdo-projeto-inicial\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php(403): Doctrine\ORM\UnitOfWork->commit(NULL)
#8 C:\Users\bruno\eclipse-workspace 2\php-pdo-projeto-inicial\bin\insert-student.php(16): Doctrine\ORM\EntityManager->flush()
#9 {main}
Seguem os códigos escritos.
Classe Student
<?php
namespace Bruno\Doctrine\Domain\Model;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Column;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
#[Entity]
class Student
{
#[Id, GeneratedValue, Column]
private int $id;
#[Column]
private string $name;
#[OneToMany(Phone::class,"students")]
public Collection $phones;
public function __construct(string $name)
{
$this->name = $name;
$this->phones = new ArrayCollection();
}
public function id(): int
{
return $this->id;
}
public function name(): string
{
return $this->name;
}
public function changeName(string $name): void
{
$this->name = $name;
}
public function addPhone(Phone $phone): void
{
$this->phones->add($phone);
$phone->setStudent($this);
}
public function phones(): iterable
{
return $this->phones;
}
}
Classe Phone
<?php
namespace Bruno\Doctrine\Domain\Model;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Column;
#[Entity]
class Phone
{
#[Id, GeneratedValue, Column]
private int $id;
#[Column]
private string $phoneNumber;
#[ManyToOne(Student::class, "student", "phones")]
private Student $student;
public function __construct(string $phoneNumber)
{
$this->$phoneNumber = $phoneNumber;
}
public function getId(): int
{
return $this->id;
}
public function getPhoneNumber(): string
{
return $this->$phoneNumber;
}
public function setStudent (Student $student): void
{
$this->student = $student;
}
}
Binário insert-student.php
<?php
use Bruno\Doctrine\Domain\Model\Student;
use Bruno\Doctrine\Infrastructure\Helper\EntityManagerCreator;
use Bruno\Doctrine\Domain\Model\Phone;
require_once __DIR__ . '/../vendor/autoload.php';
$entityManager = EntityManagerCreator::createEntityManager();
$phone1 = new Phone("11 97116-0689");
$entityManager->persist($phone1);
$student = new Student("Novo Estudante");
$student->addPhone($phone1);
$entityManager->persist($student);
try{
$entityManager->flush();
echo "Tudo certo";
}catch (Exception $e){
echo $e->getMessage();
echo $e->getTraceAsString();
}
O que deu errado?