Search code examples
doctrine-ormentityzend-framework2

No identifier/primary key specified for Entity in doctrine 2


I am using Zend 2 Framework and I am trying to get data using Doctrine 2.

However the following error is coming in the Entity file.

Doctrine\ORM\Mapping\MappingException

No identifier/primary key specified for Entity "Acl\Entity\Permission". Every Entity must have an identifier/primary key.

How can I specify the primary key?

I am using the following code.

/**
 * User Permissions
 *
 * @ORM\Entity
 * @ORM\Table(name="acl_permissions")
 * @property int $id
 * @property int $role_id
 * @property int $resource_id
 * @property string $action
 */
class Permission
{
    /**
     * @ORM\Column(type="integer")
     */
    public $id;

    /**
     * @ORM\Column(type="integer")
     * @ORM\OneToOne(targetEntity="Role")
     * @ORM\JoinColumn(name="role_id", referencedColumnName="id")
     */
    public $role;

    /**
     * @ORM\Column(type="integer")
     * @ORM\OneToOne(targetEntity="Resource")
     * @ORM\JoinColumn(name="resource_id", referencedColumnName="id")
     */
    public $resource;

    /**
     * @ORM\Column(type="string")
     */
    public $action;

    public function getRole()
    {
    return $this->role;
    }

    public function getResource()
    {
    return $this->resource;
    }
}

Solution

  • Have you checked the docs?

    You can define a primary key by using the @ORM\Id annotation. In case the value is generated automatically (e.g. if using auto_increment), you also need to set the @ORM\GeneratedValue(strategy="IDENTITY") annotation.