# Data Results in controller

**URL:** https://discourse.cakephp.org/t/data-results-in-controller/4930
**Category:** Need Help
**Created:** [September 14, 2018, 2:51pm UTC](https://discourse.cakephp.org/t/data-results-in-controller/4930 "2018-09-14T14:51:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![docelmo](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@docelmo](https://discourse.cakephp.org/u/docelmo)
#### Post date: [September 14, 2018, 2:51pm UTC](https://discourse.cakephp.org/t/data-results-in-controller/4930/1 "2018-09-14T14:51:31Z")

</div>

So I have the following code…

$user = $this-\>Users-\>get($id, [  
 ‘contain’ =\> [‘Regions’, ‘Perms’, ‘Usertypes’, ‘Roles’, ‘Assets’, ‘Accmemos’, ‘Rsmlinks’, ‘Tclinks’, ‘Tclinks.Users’, ‘Tclinks.Regions’, ‘Rsmlinks.Users’, ‘Rsmlinks.Regions’]  
]);

It pulls back data correctly except when we get to RSMLinks and TCLinks… Its pulling back based on the users user\_id column. I would like those 2 tables to pull back data based on another column in the users table. How do I accomplish this?

Brian

---

<div class="post-metadata">

### Author: ![bird](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bird/32/1157_2.png) [@bird](https://discourse.cakephp.org/u/bird)
#### Post date: [September 14, 2018, 3:23pm UTC](https://discourse.cakephp.org/t/data-results-in-controller/4930/2 "2018-09-14T15:23:37Z")

</div>

> I would like those 2 tables to pull back data based on another column in the users table. How do I accomplish this?

by removing the RSMLinks and TCLinks Tables from the “contain”-array and adding

```PHP
->innerJoin(table, condition)

```

See [Query Builder - 3.10](https://book.cakephp.org/3.0/en/orm/query-builder.html)

IDK if this is possible only using “contain”.

---

<div class="post-metadata">

### Author: ![docelmo](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@docelmo](https://discourse.cakephp.org/u/docelmo)
#### Post date: [September 14, 2018, 3:35pm UTC](https://discourse.cakephp.org/t/data-results-in-controller/4930/3 "2018-09-14T15:35:46Z")

</div>

Hey thanks for the info. This is what I was looking for in the query builder documentation but couldnt make heads/tails from it. Im not concerned by doing this in the contain array but just having the ability to do it without manually scripting the SQL query.

b

---

<div class="post-metadata">

### Author: ![docelmo](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@docelmo](https://discourse.cakephp.org/u/docelmo)
#### Post date: [September 15, 2018, 3:17am UTC](https://discourse.cakephp.org/t/data-results-in-controller/4930/4 "2018-09-15T03:17:48Z")

</div>

To add to this Ive made a bit of headroom kinda. . Im getting this built by cakephp

SELECT  
Rsmlinks.id AS `Rsmlinks__id`,  
Rsmlinks.user\_id AS `Rsmlinks__user_id`,  
Rsmlinks.region\_id AS `Rsmlinks__region_id`,  
Rsmlinks.created AS `Rsmlinks__created`,  
Rsmlinks.modified AS `Rsmlinks__modified`,  
Users.id AS `Users__id`,  
Users.email AS `Users__email`,  
Users.code1 AS `Users__code1`,  
Users.name AS `Users__name`,  
Users.region\_id AS `Users__region_id`,  
Users.company AS `Users__company`,  
Users.perm\_id AS `Users__perm_id`,  
Users.usertype\_id AS `Users__usertype_id`,  
Users.onecoreApproved AS `Users__onecoreApproved`,  
Users.vdsApproved AS `Users__vdsApproved`,  
Users.role\_id AS `Users__role_id`,  
Users.notificationtype AS `Users__notificationtype`,  
Users.mobilePhone AS `Users__mobilePhone`,  
Users.otherPhone AS `Users__otherPhone`,  
Users.address AS `Users__address`,  
Users.address2 AS `Users__address2`,  
Users.city AS `Users__city`,  
Users.state AS `Users__state`,  
Users.zip AS `Users__zip`,  
Users.created AS `Users__created`,  
Users.modified AS `Users__modified`,  
Users.webbrowser AS `Users__webbrowser`  
FROM  
rsmlinks Rsmlinks  
INNER JOIN users Users ON (  
Rsmlinks.region\_id = ‘Users.region\_id’  
AND Users.id = (Rsmlinks.user\_id)  
)  
WHERE  
Rsmlinks.user\_id in (75)

When I need this:

SELECT  
Rsmlinks.id AS `Rsmlinks__id`,  
Rsmlinks.user\_id AS `Rsmlinks__user_id`,  
Rsmlinks.region\_id AS `Rsmlinks__region_id`,  
Rsmlinks.created AS `Rsmlinks__created`,  
Rsmlinks.modified AS `Rsmlinks__modified`,  
Users.id AS `Users__id`,  
Users.email AS `Users__email`,  
Users.code1 AS `Users__code1`,  
Users.name AS `Users__name`,  
Users.region\_id AS `Users__region_id`,  
Users.company AS `Users__company`,  
Users.perm\_id AS `Users__perm_id`,  
Users.usertype\_id AS `Users__usertype_id`,  
Users.onecoreApproved AS `Users__onecoreApproved`,  
Users.vdsApproved AS `Users__vdsApproved`,  
Users.role\_id AS `Users__role_id`,  
Users.notificationtype AS `Users__notificationtype`,  
Users.mobilePhone AS `Users__mobilePhone`,  
Users.otherPhone AS `Users__otherPhone`,  
Users.address AS `Users__address`,  
Users.address2 AS `Users__address2`,  
Users.city AS `Users__city`,  
Users.state AS `Users__state`,  
Users.zip AS `Users__zip`,  
Users.created AS `Users__created`,  
Users.modified AS `Users__modified`,  
Users.webbrowser AS `Users__webbrowser`  
FROM  
rsmlinks Rsmlinks  
INNER JOIN users Users ON (  
Rsmlinks.user\_id = users.id  
)  
WHERE  
Rsmlinks.region\_id in (1)

My code is this:

-\>contain([‘Rsmlinks.Users’ =\> function ($q) {  
 return $q-\>where([‘Rsmlinks.region\_id’ =\> ‘Users.region\_id’]);  
}])
