考虑以下示例:
+----------+--------+-------------+----------+
| Person_id| Person | Language_id | Language |
+----------+--------+-------------+----------+
| 1 | Bob | 5 | English |
| 1 | Bob | 3 | Italiano |
| 1 | Bob | 8 | Deutsch |
+----------+--------+-------------+----------+
并且查询是(不是那么重要,只是编写脚本来向您显示表结构):
SELECT pl.Person_id, Person, Language_id, Language FROM people as p
LEFT JOIN people_languages as pl ON p.Person_id = pl.Person_id
LEFT JOIN languages as l ON pl.language_id = l.language_id
WHERE pl.Person = 1;
所以基本上,如果表是以这种方式构造的,最好检索所有结果,如上所示,然后创建一个 php 函数,该函数创建一个具有语言 ID 和语言在数组中的人员模型,或者使用group_concat
检索单行然后将语言和languages_id 分解成一个数组?
顺便说一句,无论我做什么,最后我都希望有一个 Person 模型,如下所示:
class Person {
public $person_id; // 1
public $person; // Bob
public $language_id; // Array(5, 3, 8)
public $language; // Array(English, Italiano, Deutsch);
.
. // Functions
.
}