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

PHP Fatal error: Uncaught Doctrine\ORM\ORMException: Entity of type Alura\Doctrine\Entity\Aluno is missing an assigned ID for field 'id'. The identifier generation strategy for this entity requires the ID field to

Estou tentando rodar o php commands\criar-aluno.php e está acusando o seguinte erro:


C:\Users\Larissa\Desktop\doctrine>php commands\criar-aluno.php
PHP Fatal error:  Uncaught Doctrine\ORM\ORMException: Entity of type Alura\Doctrine\Entity\Aluno 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\Larissa\Desktop\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\ORMException.php:87
Stack trace:
#0 C:\Users\Larissa\Desktop\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Id\AssignedGenerator.php(53): Doctrine\ORM\ORMException::entityMissingAssignedIdForField(Object(Alura\Doctrine\Entity\Aluno), 'id')
#1 C:\Users\Larissa\Desktop\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(937): Doctrine\ORM\Id\AssignedGenerator->generate(Object(Doctrine\ORM\EntityManager), Object(Alura\Doctrine\Entity\Aluno))
#2 C:\Users\Larissa\Desktop\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(1768): Doctrine\ORM\UnitOfWork->persistNew(Objec in C:\Users\Larissa\Desktop\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\ORMException.php on line 87

Fatal error: Uncaught Doctrine\ORM\ORMException: Entity of type Alura\Doctrine\Entity\Aluno 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\Larissa\Desktop\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\ORMException.php:87
Stack trace:
#0 C:\Users\Larissa\Desktop\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Id\AssignedGenerator.php(53): Doctrine\ORM\ORMException::entityMissingAssignedIdForField(Object(Alura\Doctrine\Entity\Aluno), 'id')
#1 C:\Users\Larissa\Desktop\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(937): Doctrine\ORM\Id\AssignedGenerator->generate(Object(Doctrine\ORM\EntityManager), Object(Alura\Doctrine\Entity\Aluno))
#2 C:\Users\Larissa\Desktop\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(1768): Doctrine\ORM\UnitOfWork->persistNew(Objec in C:\Users\Larissa\Desktop\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\ORMException.php on line 87

C:\Users\Larissa\Desktop\doctrine>php commands\criar-aluno.php

Gostaria de ajuda para resolver esse erro!

3 respostas

Você poderia adicionar o código da sua entidade aluno para a gente poder dar uma olhada?

Está assim meu código:

doctrine>src>Entity>aluno.php

<?php

namespace Alura\Doctrine\Entity;

/**
 * @Entity
 */

class Aluno
{
    /**
     * @Id
     * @GenerateValue
     * @Column(type="integer")
     */
    private $id;

    /**
     * @Column(type="string")
     */
    private $nome;

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

doctrine>src>Helper>EntityManagerFactory.php

<?php


namespace Alura\Doctrine\Helper;


use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\Setup;

class EntityManagerFactory
{
    /**
     * @return EntityManagerInterface
     * @throws \Doctrine\ORM\ORMException
     */
    public function getEntityManager(): EntityManagerInterface
    {
        $rootDir = __DIR__ . '/../..';
        $config = Setup::createAnnotationMetadataConfiguration(
            [$rootDir . '/src'],
            true
        );
        $connection = [
            'driver' => 'pdo_sqlite',
            'path' => $rootDir . '/var/data/banco.sqlite'
        ];

        return EntityManager::create($connection, $config);
    }
}

doctrine>vendor>cli-config.php

<?php

use Alura\Doctrine\Helper\EntityManagerFactory;
use Doctrine\ORM\Tools\Console\ConsoleRunner;

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

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

return ConsoleRunner::createHelperSet($entityManager);

doctrine>commands>criar-aluno.php

<?php

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

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

$aluno = new Aluno();
$aluno->setNome('Welington Silva');

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

$entityManager->persist($aluno);
$aluno->setNome('Wellington Silva');
solução!

Achei o erro na Entity Aluno.php era @GeneratedValue e digitei @GenerateValue

/**
 * @Id
 * @GeneratedValue
 * @Column(type="integer")
 */