Doctrine 2使用四个表连接两个表,第三个表

时间:2013-12-23 07:29:51

标签: php sql join doctrine-orm zend-framework2

我很困惑,花了2天时间找到这个问题的答案而没有。 我已经有5张桌子了

  • 语言 - > id,iso_code
  • I18n_Entity - > ID
  • I18n_String - > id,i18n_entity_id,language_id,text
  • 产品 - > id,i18n_entity_id(唯一)
  • 类别 - > id,i18n_entity_id(唯一)

我需要做两种方法

getProductByName(语言$ language,$ name);

因此,用于此目的的普通PostgreSQL查询是:

SELECT p.*, i18s.text
FROM product as p
INNER JOIN I18n_String AS i18s ON i18s.i18n_entity_id = p.i18n_entity_id AND i18s.language_id = :language_id
WHERE i18s.text = :name

getCategoryByName(语言$ language,$ name);

SELECT c.*, i18s.text
FROM category as c
INNER JOIN I18n_String AS i18s ON i18s.i18n_entity_id = c.i18n_entity_id AND i18s.language_id = :language_id
WHERE i18s.text = :name

我知道加入可以通过INNER JOIN i18n_Entity作为i18e ON i18e.id = p.i18n_entity_id,但这是不必要的。

问题是:可以用学说完成,如果是的话,如何做正确的注释和dql?

product.php     

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Product
 *
 * @ORM\Table(name="product", uniqueConstraints={@ORM\UniqueConstraint(name="product_i18n_entity_id_unique", columns={"i18n_entity_id"})})
 * @ORM\Entity
 */
class Product {
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="product_id_seq", allocationSize=1, initialValue=1)
     */
    private $id;

    /**
     * @var \Application\Entity\I18nEntity
     *
     * @ORM\ManyToOne(targetEntity="Application\Entity\I18nEntity")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="i18n_entity_id", referencedColumnName="id")
     * })
     */
    private $i18nEntity;
    ...

i18nEntityId.php

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * I18nEntity
 *
 * @ORM\Table(name="i18n_entity", indexes={@ORM\Index(name="i18n_entity_status_index", columns={"status"})})
 * @ORM\Entity
 */
class I18nEntity {
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="i18n_entity_id_seq", allocationSize=1, initialValue=1)
     */
    private $id;
    ...

i18nString.php

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * I18nString
 *
 * @ORM\Table(name="i18n_string", uniqueConstraints={@ORM\UniqueConstraint(name="i18n_string_language_id_entity_id_uniq", columns={"language_id", "i18n_entity_id"})}, indexes={@ORM\Index(name="i18n_string_language_id_idx", columns={"language_id"}), @ORM\Index(name="i18n_string_entity_id_idx", columns={"i18n_entity_id"})})
 * @ORM\Entity
 */
class I18nString {
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="i18n_string_id_seq", allocationSize=1, initialValue=1)
     */
    private $id;

    /**
     * @var \Application\Entity\I18nEntity
     *
     * @ORM\ManyToOne(targetEntity="Application\Entity\I18nEntity")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="i18n_entity_id", referencedColumnName="id")
     * })
     */
    private $i18nEntity;
    ...

0 个答案:

没有答案