doulan9287 2013-06-15 10:13
浏览 43
已采纳

Doctrine 2 - 实体通过密钥持有者表收集了另一个实体

I want to implement this type of relationship:

enter image description here

Class SimpleUser must have a "languages" class property which should contain list of all SimpleLanguage-s that are binded to the current user in the UserLanguages table.

Please look at the classes:

public class SimpleUser {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=50)
     */
    private $name;

    // what do I type here? I want to have SimpleLanguage[] here
    private $languages;
}

 public class SimpleLanguage {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=50)
     */
    private $name;
}

 public class UserLanguages {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


/**
 * @var integer
 * @ORM\Column(name="user_id", type="integer")
 */
private $userId;

/**
 * @var integer
 * @ORM\Column(name="language_id", type="integer")
 */
private $languageId;

/**
 * @var SimpleUser
 *
 * @ManyToOne(targetEntity="SimpleUser")
 * @JoinColumn(name="userId", referencedColumnName="id")
 */
private $user;

/**
 * @var SimpleLanguage
 *
 * @ManyToOne(targetEntity="SimpleLanguage")
 * @JoinColumn(name="languageId", referencedColumnName="id")
 */
private $language;
}
  • 写回答

2条回答 默认 最新

  • duanjuebiao6730 2013-06-15 11:58
    关注

    You are looking for a uni-directional one-to-many relationship with a join-table which is described in doctrine with the @ManyToMany annotation. This can lead to confusion.

    You don't even need the glueing UserLanguages class at all ... doctrine will take care of the table for you.

    <kbd>SimpleUser</kbd>

    /**
     * @ORM\ManyToMany(targetEntity="SimpleLanguage", inversedBy="user")
     * @ORM\JoinTable(name="UserLanguages",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="language_id", referencedColumnName="id", unique=true)}
     * )
     */
     protected $languages;
    

    <kbd>SimpleLanguage</kbd>

    /**
     * @ORM\ManyToMany(targetEntity="SimpleUser", mappedBy="languages")
     */
     protected $user;
    

    now doctrine will create the UserLanguages table for you and keep the id's ( user_id , language_id ) in there.

    Read more on association mappings with a join-table here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀