1

I hava a class City which consists of a number of traits.

/**
 * @ORM\Entity
 * @ORM\Table(name="City")
 * @SoftDeleteable(fieldName="deletedAt")
 */
class City
{
    use IdentifiableEntity;
    use TimestampableEntity;    
}

I have a trait IdentifiableEntity

trait IdentifiableEntity
{
    /**
     * @var integer
     * @ORM\Column(name="id", type="integer", options={"unsigned":true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    //setter getter
}

the problem occurs when I execute bin/console doctrine:migrations:diff I get this error message

[Doctrine\ORM\Mapping\MappingException]                                                                                         
  No identifier/primary key specified for Entity "AppBundle\Entity\City\City". Every Entity must have an identifier/primary key. 

Why is that? I have clearly stated in my trait that $id should be treated like @ORM\Id. Adding @ORM\GeneratedValue(strategy="IDENTITY") does not help either. What's wrong?

P.S. I am using symfony 3.2 and doctrine/doctrine-migrations-bundle: 1.0

hvertous
  • 1,133
  • 11
  • 25

1 Answers1

0

This is possible only if your class City has other $id property without annotaion. So it overrides $id from trait. Otherwise make sure this is the exact City class, not some other from another namespace.

Artur Yukhatov
  • 190
  • 1
  • 9
  • not quite sure I understood the second part, so I ask again: you say I have to override an $id every time from a trait in order to make it work? – hvertous May 26 '17 at 12:10
  • What he means is your City entity must have an $id attribute, and this $id attribute will be overriden by the one from your trait. If the only purpose of your trait is to have an $id attribute as primary key on all your entity, you should avoid using it – OlivierC May 26 '17 at 13:01
  • I mean if you what to get an id exactly from trait, make sure you dont have an id inside the City class. Because an id from City class will be in priority for doctrine and than from trait. – Artur Yukhatov May 26 '17 at 13:41
  • @ArturYukhatov I don't have another id apart from the one from trait. So I think it is just not possible for doctrine to use traits primary key as primary key for a entity which uses the trait. – hvertous May 26 '17 at 14:22
  • @OlivierC that's bad, to have a trait which sets primary key for an entity which uses it looks nice – hvertous May 26 '17 at 14:24
  • @user7808407 It is possible. Than make sure you dont have mess ups with namespaces and class names. Maybe you have other City class? – Artur Yukhatov May 26 '17 at 14:35
  • it IS possible, and it does NOT require an extra id property inside the class, i just checked in one of my projects – Sam Janssens May 30 '17 at 06:59