Same table model association twice in cakephp 3.2
Asked Answered
M

3

5

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.

Magneto answered 23/6, 2016 at 10:10 Comment(1)
you just messed up the name of the relationship with the name of the Table. belongsTo('Publisher', [ 'className' => 'Users',. Same in the query.Romola
P
8

Aliases must be unique

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']);
Puggree answered 23/6, 2016 at 11:16 Comment(4)
from what the OP says seems that both Users and Publishers are referred to the same Table object, so it should be 'className' => 'Users' in both case, I thinkRomola
here user is same as viewer and publisher is diffrent one. i mean user_id(viewer) and publisher_id belongs to one parent table Users. As it belongs to same table,i am getting only one id out of two. I need both two will come in bind.Magneto
@Puggree Here no publisher model is present , :( :(Magneto
This is correct, you just need to add Publishers in your contain in addition to UsersEgression
B
3

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);
Brammer answered 9/3, 2017 at 9:53 Comment(1)
Excellent answer with example too.Versus
O
0

My solution , call it in the controller in contain

$this->belongsTo('Senders', [
    'foreignKey' => 'sender_id',
    'joinType' => 'LEFT',
    'className' => 'Manager.Users'
]);

$this->belongsTo('Receivers', [
    'foreignKey' => 'receiver_id',
    'joinType' => 'LEFT',
    'className' => 'Manager.Users'
]);
Oyster answered 14/11, 2018 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.