Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same table model association twice in cakephp 3.2

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.

like image 544
sradha Avatar asked Jun 23 '16 10:06

sradha


2 Answers

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']);
like image 120
AD7six Avatar answered Nov 11 '22 19:11

AD7six


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);
like image 22
kantsverma Avatar answered Nov 11 '22 19:11

kantsverma