user594044
user594044

Reputation: 265

doctrine 2 join association not found

I've followed some of the questions on this site and still haven't been able to figure out what's wrong with my setup. I've been been stumped for awhile. Sorry if this is a basic mistake, I'm still new to Doctrine. Thank you.

The error I get is:

Error: Class models\\Classes_users has no association named users_username...

The query is:

SELECT u FROM models\Classes_users c JOIN c.users_username u WHERE c.class_id = 1

The entities:

/**
 * models\Classes_users
 *
 * @Table(name="classes_users")
 * @Entity
 */
class Classes_users
{
    ...

    /**
     * @var string $users_username
     *
     * @Id
     * @ManyToOne(targetEntity="Users", inversedBy="users_username")
     * @JoinColumn(name="users_username", referencedColumnName="users_username")
     * @Column(name="users_username", type="integer", precision=0, scale=0, nullable=false, unique=false)
     */
    private $users_username;

    ...
}



/**
 * models\Users
 *
 * @Table(name="users")
 * @Entity
 */
class Users
{
    ....

    /**
     * @var string $users_username
     * @Id
     * @OneToMany(targetEntity="Classes_users", mappedBy="users_username")
     * @Column(name="users_username", type="string")
     */
    private $users_username;

    ....
}

Upvotes: 0

Views: 195

Answers (1)

Travis Yang
Travis Yang

Reputation: 239

I don't know exactly what you want to accomplish, but I think your design of the entities was a little "strange", I post some code here for your reference:

/**
 * models\Classes_users
 *
 * @Table(name="classes_users")
 * @Entity
 */
class Classes_users
{
    ...
    /**
     * @Id
     */
    private $class_id;   // use other varible as Id

    /**
     * @ManyToOne(targetEntity="Users", inversedBy="user_usernames")
     * @JoinColumn(name="user_id", referencedColumnName="id")
     **Delect this line  =>** * @Column(name="users_username", type="integer", precision=0, scale=0, nullable=false, unique=false)  
     */
    private $user;   // change name to user, this varible is a virtual variable, only make sense in php objects, there would not be a real column in database, but a user_id column produced by Doctrine instead

    ...
}



/**
 * models\Users
 *
 * @Table(name="users")
 * @Entity
 */
class Users
{
    ....
    /**
     * @Id
     *
     */
    $private id;   // use other variable as Id


    /**
     * @var string $users_username
     * @Id
     * @OneToMany(targetEntity="Classes_users", mappedBy="user")
     **Delect this line  =>** * @Column(name="users_username", type="string")
     */
    private $user_usernames;    // suggest change name to reflect the one to many associations, this variable should be an arraycollection, and there would not be a real column in database

    public function __construct()
    {
        $this->user_usernames = new ArrayCollection();
    }

    ....
}

Now you can change your query to:

SELECT u FROM models\Users u JOIN u.user_usernames c WHERE c.class_id = 1

I don't know if you use Symfony, but I suggest you to read this link

Upvotes: 2

Related Questions