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条)

报告相同问题?

悬赏问题

  • ¥20 双非跨考工科哪个专业和方向就业前景好?
  • ¥20 求会6sv辐射传输模型,辅导(可py6s🙏🏻有偿
  • ¥15 .xla后缀的文件拖到excel里什么内容也没有怎么办
  • ¥20 Workbench中Mechanical打不开、闪退是什么原因?
  • ¥240 MapReduce应用实践 学生课程
  • ¥15 hlss视频显示AUTHORITY_INVALID
  • ¥15 MAX9296A+MAX96717,美信gmsl解串有人做过吗?
  • ¥15 求帮我解决一下inode 爆满的问题(有偿)
  • ¥15 关于#vscode#的问题:布料滤波算法中C++实现pcl在Vscode中pcl库没有#include <pcl>
  • ¥15 fpga:ov5640采集tft显示