Doctrine在多用途表上收集多行(单向没有连接表ManyToMany?)

时间:2016-07-05 08:42:36

标签: php mysql symfony doctrine-orm doctrine

好吧,我在Doctrine的一张严格的桌子上这样:

  • TextsRepo(language_id, text_id ,文字)
  • SuchTable1(such_id, title_text_id another_text_id
  • SuchTable2(such_id, header_text_id meta_text_id
  • ...

数据示例:

  • TextRepo - > (1, 1, "Lorem"), (2, 1, "Ipsum"), (3, 1, "Dolor"), (1, 2, "Sit"), (1, 3, "Amet"), (1, 4, "Consecteur"), etc..
  • SuchTable1 - > (100, 1, 2), (102, 3, 4), (103, 4, 5), etc.
  • SuchTable1 - > (101, 6, 7), (102, 8, 9), (103, 10, 11), etc.

我需要从text_id(在所有languages_id中)恢复所有行,就像在DQL中一样:

SELECT s, n FROM AppBundle:SuchTable1 s, JOIN **s.titles** WHERE s.such_id=100

并收到:{such_id => 100, title_text_id => 1, another_text_id => 2, titles => [{1, 1, "Lorem"}, {2, 1, "Ipsum"}, {3, 1, "Dolor"}]

我看到的一个主要问题是,显然我不能将 title_text_id another_text_id 作为PK(例如)。

这个场景的问题是,是否可以使用映射关联恢复这些行?不使用第三个连接表,因为我无法改变结构。

1 个答案:

答案 0 :(得分:1)

是的,你可以映射它: (如果你想使用反面保留'%可选:...%'注释内容,否则删除它。)

class SuchTable1
{
    /**
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\TextIds"%optional: , inversedBy="suchTable1EntitiesWithThisAsTitle"%)
    * @ORM\JoinColumn(name="title_text_id")
    */
    private $title;

    /**
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\TextIds"%optional: , inversedBy="suchTable1EntitiesWithThisAsAnother")
    * @ORM\JoinColumn(name="another_text_id")
    */
    private $anotherText;
}

class SuchTable2
{
    /**
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\TextIds"%optional: , inversedBy="suchTable2EntitiesWithThisAsHeader")
    * @ORM\JoinColumn(name="header_text_id")
    */
    private $header;

    /**
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\TextIds"%optional: , inversedBy="suchTable2EntitiesWithThisAsMeta")
    * @ORM\JoinColumn(name="meta_text_id")
    */
    private $meta;
}

class TextRepo
{
    /**
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Languages"%optional: , inversedBy="texts")
    * @ORM\JoinColumn(name="language_id")
    */
    private $language

    /**
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\TextIds"%optional: , inversedBy="tranlastions")
    * @ORM\JoinColumn(name="text_id")
    */
    private $textId;

    private $text;
}

class TextIds
{
    /**
    * Optional :
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\TextRepo",mappedBy="textId")
    */
    private $translations;

    /**
    * Optional :
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\SuchTable1",mappedBy="title")
    */
    private $suchTable1EntitiesWithThisAsTitle;

    /**
    * Optional :
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\SuchTable1",mappedBy="anotherText")
    */
    private $suchTable1EntitiesWithThisAsAnother;

    /**
    * Optional :
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\SuchTable1",mappedBy="header")
    */
    private $suchTable2EntitiesWithThisAsHeader;

    /**
    * Optional :
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\SuchTable1",mappedBy="meta")
    */
    private $suchTable2EntitiesWithThisAsMeta;
}