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 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置