phpdoctrine-ormdoctrine

Doctrine 3.3 No identifier found when #[Id] does exists


Doctrine is giving me this error because i dont have an #[Id]

No identifier/primary key specified for Entity "Event\Entity\Usuari". Every Entity must have an identifier/primary key.

The problem is that I do have the tags:

namespace Event\Entity;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\DateType;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;

#[Entity]
#[Table(name: 'Usuari')]
class Usuari{
    #[Id]
    #[Column(type: 'integer')]
    #[GeneratedValue]
    private int $id ;
    
    #[Column(type: 'varchar',length: 100 )]
    private string $name;
    
    #[Column(type: 'varchar',length: 150 )]
    private string $email;
    
    #[Column(type: 'varchar',length: 30 )]
    private string $phone;
    
    #[Column(type: 'datetime')]
    private DateType $createdAt;
    
}

Thats the whole project, i don't know why it dosn't work


Solution

  • You've got a few errors here:

    So:

    namespace Event\Entity;
    
    use DateTime;
    use Doctrine\ORM\Mapping\Column;
    use Doctrine\ORM\Mapping\Entity;
    use Doctrine\ORM\Mapping\GeneratedValue;
    use Doctrine\ORM\Mapping\Id;
    use Doctrine\ORM\Mapping\Table;
    
    #[Entity]
    #[Table(name: 'Usuari')]
    class Usuari
    {
        #[Id]
        #[Column(type: 'integer')]
        #[GeneratedValue]
        private int $id;
    
        #[Column(type: 'string',length: 100)]
        private string $name;
    
        #[Column(type: 'string',length: 150)]
        private string $email;
    
        #[Column(type: 'string',length: 30)]
        private string $phone;
    
        #[Column(type: 'datetime')]
        private DateTime $createdAt;
    }
    

    You could also do it like this:

    namespace App\Entity;
    
    use DateTime;
    use Doctrine\ORM\Mapping as ORM;
    
    #[ORM\Entity]
    #[ORM\Table(name: 'Usuari')]
    class Usuari
    {
        #[ORM\Id]
        #[ORM\Column(type: 'integer')]
        #[ORM\GeneratedValue]
        private int $id;
    
        #[ORM\Column(type: 'string',length: 100)]
        private string $name;
    
        #[ORM\Column(type: 'string',length: 150)]
        private string $email;
    
        #[ORM\Column(type: 'string',length: 30)]
        private string $phone;
    
        #[ORM\Column(type: 'datetime')]
        private DateTime $createdAt;
    }