Php 原则2使用四个表将两个表与第三个表连接起来
我很困惑,花了两天的时间来寻找这个问题的答案,什么都没有。 我已经有5张桌子了Php 原则2使用四个表将两个表与第三个表连接起来,php,sql,join,doctrine-orm,zend-framework2,Php,Sql,Join,Doctrine Orm,Zend Framework2,我很困惑,花了两天的时间来寻找这个问题的答案,什么都没有。 我已经有5张桌子了 语言->id,iso\U代码 I18n_实体->id I18n\u字符串->id,I18n\u实体\u id,语言\u id,文本 产品->标识,i18n\u实体\u标识(唯一) 类别->id,i18n\u实体\u id(唯一) 我需要做两种方法 getProductByName(语言$Language,$name) SELECT c.*, i18s.text FROM category as c INNER J
- 语言->id,iso\U代码
- I18n_实体->id
- I18n\u字符串->id,I18n\u实体\u id,语言\u id,文本
- 产品->标识,i18n\u实体\u标识(唯一)
- 类别->id,i18n\u实体\u id(唯一)
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
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;
...
因此,用于此目的的普通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
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;
...
我知道可以通过内部连接i18n_实体作为i18e ON i18e.id=p.i18n_实体,但这是不必要的
问题是:这可以用条令来完成吗?如果可以,如何进行正确的注释和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;
...
原则旨在从您那里提取所有SQL生成。如果正确配置实体注释,则甚至不需要在中创建此查询DQL@AlexP嗨,我不知道如何为这种情况编写正确的注释,有人能帮我吗?