简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM