2017-08-25 28 views
0

這個問題相關的用戶表中的所有電子郵件地址和用戶名是這個how to display all the users in table users to my subjects table in add.ctp view如何顯示ID爲

我想由USER_ID涉及用戶表打印在用戶表中的所有用戶名和電子郵件看到圖像的延續下面

https://ibb.co/dX8ynQ

我在畫面2個表中的用戶和對象表。在主題表中,它們是user_id,連接到用戶表數據。

+0

你定義的關係?請分享您嘗試過的代碼 – tarikul05

+0

NOPE我不知道如何使用關係,即時通訊新的cakephp這是我的代碼https://stackoverflow.com/questions/45830056/how-to-display-all-the-users-in -table -in-add-ctp-view/45834268 – distromob

+0

如果您是CakePHP的全新用戶,則應該閱讀本教程。關係是一個非常基本的概念,在那裏和手冊中都有涉及。一旦你建立了關係,你想要做的事情非常微不足道。 –

回答

1

你可以做這樣的事情在SQL

SELECT subjects.*,users.username, users.email 
FROM subjects 
LEFT JOIN users ON users.id = subjects.user_id 

在CakePHP

你可以建立2種型號(用戶和主題)

論主體型號/在初始化使它屬於關聯用戶,請參考 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']]); 
+0

您的sql查詢reall回答我的問題https://ibb.co/iehkZ5 ...但即時通訊仍然學習表關係。你有相同的情況下的示例代碼。 – distromob

+0

$ subjects = $ this-> Subjects-> find('all',['contain'=> ['Users'],'fields'=> ['Users.username','Users.email']]) - >水合物(假) - >指定者(); \t $ this-> set('subjects',$ subjects);如何在視圖中打印數據?我不能做這樣的<?php foreach($ subjects as $ subject):?><?= h($ subject-> username)?><?= h($ subject-> email)?><?php endforeach; ?> @PeterTanath – distromob