# Belongsto AND belongsToMany

**URL:** <https://discourse.cakephp.org/t/belongsto-and-belongstomany/8751>\
**Category:** Uncategorized\
**Created:** [December 18, 2020, 9:00am UTC](https://discourse.cakephp.org/t/belongsto-and-belongstomany/8751 "2020-12-18T09:00:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![electoonix](https://avatars.discourse-cdn.com/v4/letter/e/b5e925/32.png) [@electoonix](https://discourse.cakephp.org/u/electoonix)\
**Post date:** [December 18, 2020, 9:00am UTC](https://discourse.cakephp.org/t/belongsto-and-belongstomany/8751/1 "2020-12-18T09:00:10Z")

</div>

Hi

I have a model where an entity belongsto and belongsToMany users:

```
    $this->belongsTo('Users', [
        'foreignKey' => 'user_id',
        'joinType' => 'INNER',
         'propertyName' => 'utilisateur'
    ]);
   $this->belongsToMany('Users', [
        'foreignKey' => 'quete_id',
        'targetForeignKey' => 'user_id',
        'joinTable' => 'users_quetes',
         'propertyName' => 'utilisateurs'
    ]);

```

I want to eager load the user and the users in pagination , so I do this :  
$this-\>paginate[‘contain’] = [‘Users’];

but only the belongsToMany association is loaded. If I comment the belongsToMany association, I Got an entity in utilisateur var;

I have try $this-\>paginate[‘contain’] = [‘Users’,‘User’];  
but it says that the association ‘User’ is not defined;

I have try $this-\>paginate[‘contain’] = [‘Users’,‘utilisateur’];  
but it says that the association ‘utilisateur’ is not defined;  
I guess the problems is because it is link to the same table

My question is how to eager load the both associations (users and user) in pagination

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [December 18, 2020, 2:35pm UTC](https://discourse.cakephp.org/t/belongsto-and-belongstomany/8751/2 "2020-12-18T14:35:48Z")

</div>

You cannot have 2 associations with the same alias. Rename one of them to something else, for e.g.:

```auto
   $this->belongsToMany('RelatedUsers', [
       'className' => 'Users',
        'foreignKey' => 'quete_id',
        'targetForeignKey' => 'user_id',
        'joinTable' => 'users_quetes',
         'propertyName' => 'utilisateurs'
    ]);

```

Then you can use `$this->paginate[‘contain’] = [‘Users’, 'RelatedUsers'];`.
