2
respostas

PHP Fatal error: Uncaught TypeError

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;
    }
}
2 respostas

Arquivo do migration

<?php

declare(strict_types=1);

namespace Alura\Doctrine\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20210406165644 extends AbstractMigration
{
    public function getDescription() : string
    {
        return '';
    }

    public function up(Schema $schema) : void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.');

        $this->addSql('DROP INDEX IDX_6F96721A87CB4A1F');
        $this->addSql('DROP INDEX IDX_6F96721AB2DDF7F4');
        $this->addSql('CREATE TEMPORARY TABLE __temp__curso_aluno AS SELECT curso_id, aluno_id FROM curso_aluno');
        $this->addSql('DROP TABLE curso_aluno');
        $this->addSql('CREATE TABLE curso_aluno (curso_id INTEGER NOT NULL, aluno_id INTEGER NOT NULL, PRIMARY KEY(curso_id, aluno_id), CONSTRAINT FK_6F96721A87CB4A1F FOREIGN KEY (curso_id) REFERENCES Curso (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_6F96721AB2DDF7F4 FOREIGN KEY (aluno_id) REFERENCES Aluno (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)');
        $this->addSql('INSERT INTO curso_aluno (curso_id, aluno_id) SELECT curso_id, aluno_id FROM __temp__curso_aluno');
        $this->addSql('DROP TABLE __temp__curso_aluno');
        $this->addSql('CREATE INDEX IDX_6F96721A87CB4A1F ON curso_aluno (curso_id)');
        $this->addSql('CREATE INDEX IDX_6F96721AB2DDF7F4 ON curso_aluno (aluno_id)');
        $this->addSql('DROP INDEX IDX_D8448137B2DDF7F4');
        $this->addSql('CREATE TEMPORARY TABLE __temp__Telefone AS SELECT id, aluno_id, numero FROM Telefone');
        $this->addSql('DROP TABLE Telefone');
        $this->addSql('CREATE TABLE Telefone (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, aluno_id INTEGER DEFAULT NULL, numero VARCHAR(255) NOT NULL COLLATE BINARY, CONSTRAINT FK_D8448137B2DDF7F4 FOREIGN KEY (aluno_id) REFERENCES Aluno (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
        $this->addSql('INSERT INTO Telefone (id, aluno_id, numero) SELECT id, aluno_id, numero FROM __temp__Telefone');
        $this->addSql('DROP TABLE __temp__Telefone');
        $this->addSql('CREATE INDEX IDX_D8448137B2DDF7F4 ON Telefone (aluno_id)');
    }

    public function down(Schema $schema) : void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'sqlite', 'Migration can only be executed safely on \'sqlite\'.');

        $this->addSql('DROP INDEX IDX_D8448137B2DDF7F4');
        $this->addSql('CREATE TEMPORARY TABLE __temp__Telefone AS SELECT id, aluno_id, numero FROM Telefone');
        $this->addSql('DROP TABLE Telefone');
        $this->addSql('CREATE TABLE Telefone (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, aluno_id INTEGER DEFAULT NULL, numero VARCHAR(255) NOT NULL)');
        $this->addSql('INSERT INTO Telefone (id, aluno_id, numero) SELECT id, aluno_id, numero FROM __temp__Telefone');
        $this->addSql('DROP TABLE __temp__Telefone');
        $this->addSql('CREATE INDEX IDX_D8448137B2DDF7F4 ON Telefone (aluno_id)');
        $this->addSql('DROP INDEX IDX_6F96721A87CB4A1F');
        $this->addSql('DROP INDEX IDX_6F96721AB2DDF7F4');
        $this->addSql('CREATE TEMPORARY TABLE __temp__curso_aluno AS SELECT curso_id, aluno_id FROM curso_aluno');
        $this->addSql('DROP TABLE curso_aluno');
        $this->addSql('CREATE TABLE curso_aluno (curso_id INTEGER NOT NULL, aluno_id INTEGER NOT NULL, PRIMARY KEY(curso_id, aluno_id))');
        $this->addSql('INSERT INTO curso_aluno (curso_id, aluno_id) SELECT curso_id, aluno_id FROM __temp__curso_aluno');
        $this->addSql('DROP TABLE __temp__curso_aluno');
        $this->addSql('CREATE INDEX IDX_6F96721A87CB4A1F ON curso_aluno (curso_id)');
        $this->addSql('CREATE INDEX IDX_6F96721AB2DDF7F4 ON curso_aluno (aluno_id)');
    }
}

Antonio, o aluno com o id especificado não foi encontrado.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software