ao executar php commands\vincular-aluno-curso.php 1 1 ele me retorna o seguinte
PHP Fatal error: Uncaught TypeError: Argument 1 passed to Alura\Doctrine\Entity\Curso::addAluno() must be an instance of Alura\Doctrine\Entity\Aluno, null given, called in C:\Users\Layane Tavares\Documents\Alura\Formacao PHP\Doctrine-ORM\commands\vincular-aluno-curso.php on line 20 and defined in C:\Users\Layane Tavares\Documents\Alura\Formacao PHP\Doctrine-ORM\src\Entity\Curso.php:48
Stack trace:
#0 C:\Users\Layane Tavares\Documents\Alura\Formacao PHP\Doctrine-ORM\commands\vincular-aluno-curso.php(20): Alura\Doctrine\Entity\Curso->addAluno(NULL)
#1 {main}
thrown in C:\Users\Layane Tavares\Documents\Alura\Formacao PHP\Doctrine-ORM\src\Entity\Curso.php on line 48
Fatal error: Uncaught TypeError: Argument 1 passed to Alura\Doctrine\Entity\Curso::addAluno() must be an instance of Alura\Doctrine\Entity\Aluno, null given, called in C:\Users\Layane Tavares\Documents\Alura\Formacao PHP\Doctrine-ORM\commands\vincular-aluno-curso.php on line 20 and defined in C:\Users\Layane Tavares\Documents\Alura\Formacao PHP\Doctrine-ORM\src\Entity\Curso.php:48
Stack trace:
#0 C:\Users\Layane Tavares\Documents\Alura\Formacao PHP\Doctrine-ORM\commands\vincular-aluno-curso.php(20): Alura\Doctrine\Entity\Curso->addAluno(NULL)
#1 {main}
thrown in C:\Users\Layane Tavares\Documents\Alura\Formacao PHP\Doctrine-ORM\src\Entity\Curso.php on line 48
arquivo vincular-aluno-curso.php
<?php
use Alura\Doctrine\Entity\Aluno;
use Alura\Doctrine\Entity\Curso;
use Alura\Doctrine\Helper\EntityManagerFactory;
require_once __DIR__ . '/../vendor/autoload.php';
$entityManagerFactory = new EntityManagerFactory();
$entityManager = $entityManagerFactory->getEntityManager();
$idAluno = $argv[1];
$idCurso = $argv[2];
/** @var Curso $curso */
$curso = $entityManager->find(Curso::class, $idCurso);
/** @var Aluno $aluno */
$aluno = $entityManager->find(Aluno::class, $idAluno);
$curso->addAluno($aluno);
$entityManager->flush();
esse é o meu arquivo Curso.php
<?php
namespace Alura\Doctrine\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
*/
class Curso
{
/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
private $id;
/**
* @Column(type="string")
*/
private $name;
/**
* @ManyToMany(targetEntity="Aluno", inversedBy="cursos")
*/
private $alunos;
public function __construct()
{
$this->alunos = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(String $name): self
{
$this->name = $name;
return $this;
}
public function addAluno(Aluno $aluno)
{
if ($this->alunos->contains($aluno)) {
return $this;
}
$this->alunos->add($aluno);
$aluno->addCurso($this);
return $this;
}
public function getAluno()
{
return $this->alunos;
}
}
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 $name;
/**
* @OneToMany(targetEntity="Telefone", mappedBy="aluno", cascade={"remove", "persist"})
*/
private $telefones;
/**
* @ManyToMany(targetEntity="Curso", mappedBy="alunos")
*/
private $cursos;
public function __construct()
{
$this->telefones = new ArrayCollection();
$this->cursos = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function addTelefone(Telefone $telefone)
{
$this->telefones->add($telefone);
$telefone->setAluno($this);
return $this;
}
public function getTelefones(): Collection
{
return $this->telefones;
}
public function addCurso(Curso $curso): self
{
if ($this->cursos->contains($curso)) {
return $this;
}
$this->cursos->add($curso);
$curso->addAluno($this);
return $this;
}
public function getCursos(): Collection
{
return $this->cursos;
}
}