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

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()’?

On your NoticesTable:

$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/retrieving-data-and-resultsets.html#retrieving-associated-data

2 Likes