numediaweb numediaweb - 4 months ago 30
SQL Question

How to set column position in Doctrine (Symfony 3) Entity

I have an entity named Order which has a OneToMany relation with a User Entity.

/**
* Class Order
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\OrderRepository")
* @ORM\Table(name="orders")
*
* Defines the properties of the Order entity to represent the orders.
*/
class Order
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @var User $user
*
* @ORM\ManyToOne(
* targetEntity="User",
* inversedBy="orders"
* )
* @ORM\JoinColumn(
* name="user_id",
* referencedColumnName="id"
* )
*/
protected $user;

// other columns ..


When I run this this it creates a user_id column at the end of the table.

Is it possible to create this column after the ID column?

I tried:

..
*
* @ORM\ManyToOne(
* targetEntity="User",
* inversedBy="orders"
* )
* @ORM\JoinColumn(
* name="user_id",
* referencedColumnName="id"
* )
*
* @Column(
* columnDefinition="INT NOT NULL AFTER `id`"
* )


But I get an error:

[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "@Column" in property AppBundle\Entity\Order::$user was never imported. Did you maybe forget to add a "use" statement for this annotation?

Answer

You forgot the ORM namespace in your @Column annotation, but this will not fix your main issue. The columnDefinition can do what you want only if you apply it on the @ORM\JoinColumn annotation.

Comments