如何显示用户 table 中与 id 相关的所有电子邮件和用户名

how to display all email and username in users table related with id

这个问题是这个

的延续

我想通过将 user_id 与用户 table 相关联来打印用户 table 中的所有用户名和电子邮件,请参见下图

https://ibb.co/dX8ynQ

我在图片中有 2 tables 用户和主题 table。在主题 table 中,它们 user_id 连接到用户 table 数据。

你可以在 SQL

中做类似的事情
SELECT subjects.*,users.username, users.email
FROM subjects
LEFT JOIN users ON users.id = subjects.user_id

在 CakePhP 中

您可以构建 2 个模型(用户和主题)

On Subject Model / in initialize make it belongsTo User, 请参考 https://book.cakephp.org/3.0/en/orm/associations.html

然后您可以检索与用户关联的主题。请参阅 https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#retrieving-associated-data

$query = $subjects->find('all', ['contain' => ['User']]);