2

If I have an entity class that uses a trait which declares entity relations, those entity relations will be ignored by the php app/console doctrine:migrations:diff command.

I assume it is the same with the Symfony command php app/console doctrine:schema:update --force.

For example:

trait FooTrait {

    /**
     * @ORM\OneToOne(targetEntity="AnotherEntity")
     * @ORM\JoinColumn(name="fk_another")
     *
     * @var AnotherEntity
     */
    private $another;
}

/**
 * @ORM\Entity
 */
public class Bar {

    use FooTrait;

    /**
     * @var string     *
     * @ORM\Column(type="string")
     */
    private $name;

}

Will generate a migration that creates field fk_another as VARCHAR(255) without any indices.

mickadoo
  • 3,337
  • 1
  • 25
  • 38
  • I'm pretty sure that Doctrine's schema-tool is trait-aware but from your report is seems that the migrations aren't, or at least aren't for that type of change. – Peter Bailey May 28 '15 at 19:09
  • I for one think you're abusing the `trait`s here. If you don't mind me asking, why didn't you go for a classing inheritance? – Jovan Perovic May 28 '15 at 23:00
  • I use traits defining relations with other entities and it works just fine, Doctrine has no problem with it, I suggest your problem is somewhere else. – Guillaume Fache May 29 '15 at 08:18
  • @GuillaumeFache Do you use the doctrine migrations bundle? – mickadoo May 29 '15 at 08:53
  • @JovanPerovic the code I inherited uses traits in entities regularly. I'm aware it might not be the best approach but it was working fine so far - in fact I can even see advantages, for example an IdTrait that defines the id property, getter and setter and reduces code repetition. – mickadoo May 29 '15 at 08:55
  • 1
    @mickadoo I do not, I just use it with Symfony 2, and `doctrine:schema:update` works fine – Guillaume Fache May 29 '15 at 08:58
  • @GuillaumeFache ok, then it seems to be a problem with just the doctrine migrations bundle. I would edit the question but don't have enough reputation. – mickadoo May 29 '15 at 09:05

0 Answers0