# Cakephp3.2, 1 foreign key on 2 or more fields in table

**URL:** <https://discourse.cakephp.org/t/cakephp3-2-1-foreign-key-on-2-or-more-fields-in-table/583>\
**Category:** Need Help\
**Created:** [June 20, 2016, 10:26am UTC](https://discourse.cakephp.org/t/cakephp3-2-1-foreign-key-on-2-or-more-fields-in-table/583 "2016-06-20T10:26:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhxz945](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@dhxz945](https://discourse.cakephp.org/u/dhxz945)\
**Post date:** [June 20, 2016, 10:26am UTC](https://discourse.cakephp.org/t/cakephp3-2-1-foreign-key-on-2-or-more-fields-in-table/583/1 "2016-06-20T10:26:20Z")

</div>

i have 2 tables:

-1. users- (id, username,password )

-2. notices- (id, name,user\_id,touser\_id)

user\_id belong to user  
touser\_id belong to user

How to build relationships in the model?  
How to use ‘contain’ to find relational data in ‘find()’?

---

<div class="post-metadata">

**Author:** ![alysson\_azevedo](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/alysson_azevedo/32/194_2.png) [@alysson\_azevedo](https://discourse.cakephp.org/u/alysson_azevedo)\
**Post date:** [June 20, 2016, 5:40pm UTC](https://discourse.cakephp.org/t/cakephp3-2-1-foreign-key-on-2-or-more-fields-in-table/583/2 "2016-06-20T17:40:32Z")

</div>

On your `NoticesTable`:

```auto
$this->belongsTo( 'Users', [
    'foreignKey' => 'user_id',
    'className' => 'Users'
] );
$this->belongsTo( 'ToUsers', [
    'foreignKey' => 'touser_id',
    'className' => 'Users'
] );

```

For more info:  
[http://book.cakephp.org/3.0/en/orm/associations.html#belongsto-associations](http://book.cakephp.org/3.0/en/orm/associations.html#belongsto-associations)  
[http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#retrieving-associated-data](http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#retrieving-associated-data)
