Ainda não tem acesso? Estude com a gente! Matricule-se
Ainda não tem acesso? Estude com a gente! Matricule-se

Erro ao tentar executar criar_aluno.php

Após seguir as aulas, tentei executar o arquivo criar_aluno.php, mas me deparei com o seguinte erro. O que estou fazendo de errado? Se entendi corretamente, ele diz que a tabela Aluno não existe, mas ela não deveria ter sido criada na migração?

PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000]: General error: 1 no such table: Aluno in D:\Alura\projeto-doctrine\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:78
Stack trace:
#0 D:\Alura\projeto-doctrine\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php(78): PDO->prepare('INSERT INTO Alu...', Array)
#1 D:\Alura\projeto-doctrine\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php(80): Doctrine\DBAL\Driver\PDOConnection->prepare('INSERT INTO Alu...')
#2 D:\Alura\projeto-doctrine\vendor\doctrine\dbal\lib\Doctrine\DBAL\Connection.php(1211): Doctrine\DBAL\Statement->__construct('INSERT INTO Alu...', Object(Doctrine\DBAL\Connection))
#3 D:\Alura\projeto-doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(273): Doctrine\DBAL\Connection->prepare('INSERT INTO Alu...')
#4 D:\Alura\projeto-doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(1088): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts()
#5 D:\Alura\projeto-doctrine\ven in D:\Alura\projeto-doctrine\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractSQLiteDriver.php on line 59

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1 no such table: Aluno in D:\Alura\projeto-doctrine\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:78
Stack trace:
#0 D:\Alura\projeto-doctrine\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php(78): PDO->prepare('INSERT INTO Alu...', Array)
#1 D:\Alura\projeto-doctrine\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php(80): Doctrine\DBAL\Driver\PDOConnection->prepare('INSERT INTO Alu...')
#2 D:\Alura\projeto-doctrine\vendor\doctrine\dbal\lib\Doctrine\DBAL\Connection.php(1211): Doctrine\DBAL\Statement->__construct('INSERT INTO Alu...', Object(Doctrine\DBAL\Connection))
#3 D:\Alura\projeto-doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(273): Doctrine\DBAL\Connection->prepare('INSERT INTO Alu...')
#4 D:\Alura\projeto-doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(1088): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->executeInserts()
#5 D:\Alura\projeto-doctrine\ven in D:\Alura\projeto-doctrine\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractSQLiteDriver.php on line 59

Arquivo criar_aluno

<?php

use Alura\Doctrine\Entity\Aluno;
use Alura\Doctrine\Entity\Telefone;
use Alura\Doctrine\Helper\EntityManagerFactory;

require_once __DIR__ . '/../vendor/autoload.php';

$entityManagerFactory = new EntityManagerFactory();
$entityManager = $entityManagerFactory->getEntityManager();

$aluno = new Aluno();
$aluno->setName($argv[1]);

for ($i = 2; $i < $argc; $i++){
    $numeroTelefone = $argv[$i];
    $telefone = new Telefone();
    $telefone->setNumero($numeroTelefone);

    $entityManager->persist($telefone);

    $aluno->addTelefone($telefone);
}

$entityManager->persist($aluno);

$entityManager->flush();

Código do migrations

<?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 Version20201109173746 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->addSql('CREATE TABLE Aluno (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(255) NOT NULL)');
        $this->addSql('CREATE TABLE Telefone (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, aluno_id INTEGER DEFAULT NULL, numero VARCHAR(255) NOT NULL)');
        $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->addSql('DROP TABLE Aluno');
        $this->addSql('DROP TABLE Telefone');
    }
}
1 resposta

Você executou a Migration, João? Qual a saída da execução?

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