I have done model association in cake 3.2
Here i have done it for one id of same table .
I have tried to do it for other one ,but its not working at all
below is the flow.
This output i am getting
{
"id": 1,
"publisher_id": 133,
"user_id": 118,
"Publisher": {
"id": 133,
"name": "Sradha sradha"
}
Here i want to bind the user id also ,which is belongs to that same user table
The output should come like this(I want to get like this below)
{
"id": 1,
"publisher_id": 133,
"user_id": 118,
"Publisher": {
"id": 133,
"name": "Sradha sradha"
}
"Users": {
"id": 118,
"name": "Sradha anjo"
}
Here both publisher_id and user_id are belongs to same user table .
$this->AdminRevenues->belongsTo('Users', [
'className' => 'Users',
'foreignKey' => 'user_id',
'propertyName' => 'Users']);
$this->AdminRevenues->belongsTo('Users', [
'className' => 'Publisher',
'foreignKey' => 'publisher_id',
'propertyName' => 'Publisher']);
$totalAdminRevenue = $this->AdminRevenues->find('all')
->contain([
'Users' => ['queryBuilder' => function ($q) {
return $q->select(['id', 'name']);
}]])
->toArray();
Please suggest ,any suggestion will be highly appreciated.
What this is doing:
$this->AdminRevenues->belongsTo('Users', [
'className' => 'Users',
'foreignKey' => 'user_id',
'propertyName' => 'Users']);
$this->AdminRevenues->belongsTo('Users', [
'className' => 'Publisher',
'foreignKey' => 'publisher_id',
'propertyName' => 'Publisher']);
Is declaring an association using AdminRevenues.user_id
and then immediately overwriting with an association AdminRevenues.publisher_id
. Effectively the first call to belongsTo
isn't doing anything.
Association aliases need to be unique, otherwise code such as $foo = $this->AdminRevenues->Users
would be ambiguous. So, just make the association aliases unique:
$this->AdminRevenues->belongsTo('Users', [
'className' => 'Users',
'foreignKey' => 'user_id',
'propertyName' => 'Users']);
$this->AdminRevenues->belongsTo('Publishers', [ // <---
'className' => 'Users', // <---
'foreignKey' => 'publisher_id',
'propertyName' => 'Publisher']);
It work for me in cakephp 3+
Lets suppose you have two table i.e.
1) RequestArticles
3) Store
We have similiar two model:-
1) RequestArticlesTable 2) StoreTable
Here is the association that you need to define in RequestArticlesTable we are joing Store table twice
public function initialize(array $config)
{
parent::initialize($config);
$this->table('request_articles');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo( 'yourstore', [
'foreignKey' => 'store_id',
'className' => 'Store'
]);
$this->belongsTo( 'fromstore', [
'foreignKey' => 'from_store_id',
'className' => 'Store'
]);
}
Now we will join tables in Controller like below and set data to view :-
// query to fetch the data from RequestArticles table and set join for store table twice
$requestArticlesDetaile = $this->RequestArticles->get($id, [
'contain' => ['yourstore','fromstore']
]);
// set fetched data to view
$this->set('requestArticlesDetaile', $requestArticlesDetaile);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With