Boa tarde,
Está dando erro, quando uso o comando "php commands\criar-aluno.php" com o nome e telefone na aula de "Atualizando o CRUD de Aluno", o erro é esse:
PHP Fatal error: Uncaught Doctrine\ORM\Exception\EntityMissingAssignedId: Entity of type Alura\Doctrine\Entity\Telefone is missing an assigned ID for field 'id'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly. in C:\Users\felip\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\Exception\EntityMissingAssignedId.php:16 Stack trace:
#0 C:\Users\felip\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\Id\AssignedGenerator.php(34): Doctrine\ORM\Exception\EntityMissingAssignedId::forField()
#1 C:\Users\felip\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(989): Doctrine\ORM\Id\AssignedGenerator->generateId()
#2 C:\Users\felip\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(1815): Doctrine\ORM\UnitOfWork->persistNew()
#3 C:\Users\felip\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(1 in C:\Users\felip\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\Exception\EntityMissingAssignedId.php on line 16
segue abaixo os códigos que usei
criar-aluno.php
<?php
use Alura\Doctrine\Entity\Aluno;
use Alura\Doctrine\Entity\Telefone;
use Alura\Doctrine\Helper\EntityManagerFactory;
require_once __DIR__ . '/../vendor/autoload.php';
$aluno = new Aluno();
$aluno-> setNome($argv[1]);
$entityManagerFactory = new EntityManagerFactory();
$entityManager = $entityManagerFactory->getEntityManager();
for ($i =2; $i< $argc; $i++) {
$numeroTelefone = $argv[$i];
$telefone = new Telefone();
$telefone->setNumero($numeroTelefone);
$entityManager->persist($telefone);
$aluno->addTelefones($telefone);
}
$entityManager->persist($aluno);
$entityManager->flush();
Aluno.php
<?php
namespace Alura\Doctrine\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @Entity
*/
class Aluno
{
/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
private $id;
/**
* @Column(type="string")
*/
private $nome;
/**
* @OneToMany(targetEntity="Telefone", mappedBy="aluno")
*/
private $telefones;
public function __construct()
{
$this->telefones = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
public function getNome(): string
{
return $this->nome;
}
public function setNome(string $nome): self
{
$this->nome = $nome;
return $this;
}
public function addTelefones(Telefone $telefone)
{
$this->telefones->add($telefone);
$telefone->getAluno($this);
return $this;
}
public function getTelefones(): Collection
{
return $this->telefones;
}
}
Telefone.php
<?php
namespace Alura\Doctrine\Entity;
/**
* @Entity
*/
class Telefone
{
/**
* @Id
* @GenerateValue
* @Column(type="integer")
*/
private $id;
/**
* @Column(type="string")
*/
private $numero;
/**
* @ManyToOne(targetEntity="Aluno")
*/
private $aluno;
public function getId():int
{
return $this->id;
}
public function setIt(int $id): self
{
$this->id = $id;
return $this;
}
public function getNumero(): string
{
return $this->numero;
}
public function setNumero(string $numero): self
{
$this->numero = $numero;
return $this;
}
public function getAluno(): Aluno
{
return $this->aluno;
}
public function setAluno(Aluno $aluno): self
{
$this->aluno = $aluno;
return $this;
}
}