No identifier/primary key specified for Entity (…)

2019-04-21 05:16发布

问题:

I have Peticion entity but something is missing because appears the following error:

No identifier/primary key specified for Entity (...) Every Entity must have and identifier/primary key

This is the entity code:

<?php

namespace Project\UsuarioBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Peticion
 *
 * @ORM\Table(name="peticion")
 * @ORM\Entity
 */
class Peticion
{
    /**
     *
     * @ORM\Id
     * @ORM\ManyToMany(targetEntity="Project\UsuarioBundle\Entity\Usuario", inversedBy="usuNick2")
     * @ORM\JoinTable(name="USUARIO",
     *      joinColumns={@ORM\JoinColumn(name="USU_NICK_1", referencedColumnName="USU_NICK")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="USU_NICK_2", referencedColumnName="USU_NICK")}
     *      )
     */
    private $usuNick1;

    /**
     *
     * @ORM\Id
     * @ORM\ManyToMany(targetEntity="Project\UsuarioBundle\Entity\Usuario", mappedBy="usuNick1"))
     */
    private $usuNick2;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="PET_FECHA", type="date", nullable=false)
     */
    private $fecha;

回答1:

You need to specify id field and remove other @ORM\Id annotations. Identifiers / Primary Keys at doctrine documentation.

Every entity class needs an identifier/primary key. You designate the field that serves as the identifier with the @Id marker annotation.

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;


回答2:

In my case, what happened was this:

I create the entity file, and put it in the entity directory, with the database schema.

But here is the thing, I also created created a YML file for the entity and put it inside Resources/config/doctrine, without a schema. Symfony was looking for the schema inside YML. Once I removed the YML file, the schema in my entity file worked just fine.



回答3:

The solution is to add the id field to the EntityName.orm.yml id: true For example: AppBundle \ Entity \ Supplier:    type: entity    table: sylius_supplier    fields:      id:        type: integer        id: true        generator:          strategy: AUTO      name: .......