Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

Erro ao vincular aluno com curso...

Olá. Ao vincular aluno com curso, apresenta o seguinte erro: Could not open input file: commands\vincular-aluno-curso

D:\Fundamentos Doctrine\Doctrine Alura>php commands\vincular-aluno-curso.php
PHP Notice:  Undefined offset: 1 in D:\Fundamentos Doctrine\Doctrine Alura\commands\vincular-aluno-curso.php on line 1
2
PHP Notice:  Undefined offset: 2 in D:\Fundamentos Doctrine\Doctrine Alura\commands\vincular-aluno-curso.php on line 1
3
PHP Fatal error:  Uncaught Doctrine\ORM\ORMException: The identifier id is missing for a query of Alura\Doctrine\Entit
y\Curso in D:\Fundamentos Doctrine\Doctrine Alura\vendor\doctrine\orm\lib\Doctrine\ORM\ORMException.php:309
Stack trace:
#0 D:\Fundamentos Doctrine\Doctrine Alura\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php(410): Doctrine\ORM\OR
MException::missingIdentifierField('Alura\\Doctrine\\...', 'id')
#1 D:\Fundamentos Doctrine\Doctrine Alura\commands\vincular-aluno-curso.php(16): Doctrine\ORM\EntityManager->find('Alu
ra\\Doctrine\\...', Array)
#2 {main}
  thrown in D:\Fundamentos Doctrine\Doctrine Alura\vendor\doctrine\orm\lib\Doctrine\ORM\ORMException.php on line 309
4 respostas

Segue o código do 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();

Algo que identifiquei diferente nas aulas foi o arquivo migration com as versões das migrações. Segue o código gerado ao gerar as tabelas no DB.

<?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 Version20190704183711 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_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)');
    }
}
solução!

Fala, Cleyton.

Você só se esqueceu de passar os IDs dos alunos no comando. rsrsrs

Você está executando o comando sem argumento nenhum. ;-)

Olá, Vinícius.

Apesar do bug, mesmo inserindo os argumentos apresentava outro erro, porém após vários verificações e comparações dos códigos apresentados nas aulas, constatei que na marcação @ManyToMany, havia inserido um @ a mais.

Obrigado pela atenção novamente e a aulas que seguem agora. Valeu pelas aulas e explicações. Abraços.

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