开发者

Doctrine 2, help on doing annotation for relations?

开发者 https://www.devze.com 2023-03-15 09:18 出处:网络
I\'m starting out with doctrine2. So, I\'m taking the following example, to understand how to do annotations for table relationship, for example:

I'm starting out with doctrine2. So, I'm taking the following example, to understand how to do annotations for table relationship, for example:

-------- the tab开发者_Python百科les

USER id username group_id

GROUP id name

A given user, is part of a group (admin, member). For example, john is admin peter is member

<?php

/** @Entity */
class User
{
    // ...

    /**
     * @ManyToOne(targetEntity="group")
     * @JoinColumn(name="group_id", referencedColumnName="id")
     */
    private $group;
}

/** @Entity */
class group
{

}
?>

I would like to understand if this is correct ?

Thanks for looking!


Thats correct as it is.

You could even ommit the @JoinColumn Statement as it will default to this values.

If you want a bidirectional relationship you also have to set the relationship on the group class like this:

@OneToMany(targetEntity="User", mappedBy="group")
private $users;
0

精彩评论

暂无评论...
验证码 换一张
取 消