Paul February 2016

doctrine2 jointable annotation error

I try to make a join table from 2 entities with a manytomany relationship like this:

class Entity1
{
    /**
     * @ORM\ManyToMany(targetEntity="Orders")
     * @JoinTable(name="users_orders",
     *   joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
     *   inverseJoinColumns={@JoinColumn(name="order_id", referencedColumnName="id", unique=true)
     */
    private $orders;
}

but each time I try to generate the getters and setters I have the same error:

The annotation @JoinTable... was never imported. Did you forget to add a use statement for this annotation?

So how can I add the use statement? Thank you for your help

Answers


Jason Roman February 2016

You need to put @ORM\ in front of all of your Doctrine annotations:

/**
 * @ORM\ManyToMany(targetEntity="Orders")
 * @ORM\JoinTable(name="users_orders",
 *   joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *   inverseJoinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id", unique=true)
 */
private $orders;


TheOddGuy February 2016

Add ORM in front of JoinTable and JoinColumn

/**
 * @ORM\ManyToMany(targetEntity="Orders")
 * @ORM\JoinTable(name="users_orders",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id", unique=true)
 */

And add the 'use'-statement at the top of your class:

use Doctrine\ORM\Mapping as ORM;

Post Status

Asked in February 2016
Viewed 1,778 times
Voted 9
Answered 2 times

Search




Leave an answer