多语言网站按语言或默认显示 Post

Multilingualism website display Post by language or default

我正在用 symfony 编写一个多语言网站 我的文章 table:

id original_id rubrique_id status lang updated_at title
1 1 1 1 en 2021-02-16 00:01:00 pede libero En
2 1 1 1 fr 2021-02-16 00:02:00 pede libero Fr
3 1 1 1 * 2021-02-16 00:03:00 pede libero All
4 4 1 1 fr 2021-02-16 00:04:00 rhoncus aliquet Fr
5 4 1 1 * 2021-02-16 00:05:00 rhoncus aliquet All

table 还包含其他字段以及与 table 部分的关系

我想按照“en”语言显示一篇文章,如果没有找到“en”语言则显示“*”

我试过了:

SELECT a 
FROM App\Entity\Article 
WHERE (a.lang = 'en' OR a.lang = '*') 
GROUP BY a.original
ORDER BY a.updatedAt DESC

根据@eshirvana先生的解决方案

查询:

SELECT p, r FROM App\Entity\Article 
p INNER JOIN p.rubrique r 
WHERE (p.lang = :value_0 OR p.lang = :value_1) 
AND p.status = :stat AND r.status = :rstat 
GROUP BY p.original 
ORDER BY p.updatedAt DESC, p.lang DESC

它总是显示最后一次添加

我改进了@eshirvana先生提出的方案二

解决方案

SELECT * FROM `articles` as post
WHERE status = 1
AND (
    post.lang = 'en' 
    OR (
      NOT(EXISTS(SELECT * FROM `articles` as subpost 
                 WHERE subpost.lang = 'en' 
                 AND  subpost.status = 1 
                 AND subpost.original_id = post.original_id))
      AND post.lang = '*'
    )
) 
ORDER BY updated_at DESC

感谢您的任何建议。

如果你 return 每次只有一条记录并且总是 return '*' 如果你找不到你想要的语言,一种方法是:

SELECT TOP 1 * 
FROM App\Entity\Article a
WHERE a.lang  IN ( 'es', '*')
GROUP BY a.original
ORDER by a.lang DESC , a.updatedAt DESC

但防弹方法是:

SELECT * 
FROM `articles` 
WHERE (lang = 'fr' 
       OR (
           not exists(select 1 FROM `articles` WHERE lang = 'fr') 
           and lang = '*'
          ) 
       )
GROUP BY original_id
ORDER BY updated_at DESC