Skip to content

belongsToMany not working in nested collection #1810

Open
@callumthomson

Description

@callumthomson

Short: when using the belongsToMany relationship between a top level document, and a document that is nested in another document, the related_ids property is not set properly on the nested document.

I have three models, App\User, App\User\WishList, and App\Product. App\User and App\Product represent base collections, but App\WishList exists only inside User.

App\User model:

namespace App;

use Jenssegers\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable
{

    protected $collection = 'users';
    protected $guarded = [];

    public function wish_lists()
    {
        return $this->embedsMany('App\User\WishList');
    }
}

App\User\WishList model:

namespace App\User;

use Jenssegers\Mongodb\Eloquent\Model;

class WishList extends Model
{
    protected $guarded = [];

    public function products()
    {
        return $this->belongsToMany('App\Product');
    }
}

App\Product model:

namespace App;

use Jenssegers\Mongodb\Eloquent\Model;

class Product extends Model
{
    protected $collection = 'products';
    protected $guarded = [];

    public function wish_lists()
    {
        return $this->belongsToMany('App\User\WishList');
    }
}

Test: I want to add a product to a users wishlist.

$user = User::first();
$product = Product::first();
$wishList = $user->wish_lists->first();

$wishList->products()->attach($product->id);

Expected:

  • wish_list_ids array property added to the document in products collection, containing the ID of the users wish list.
  • product_ids array property added to user.wish_lists[0] in users collection, containing the ID of the product.
  • Retrieve products in wish list by calling $wishList->products
  • Retrieve wish lists containing products by calling $product->wish_lists

Actual:

  • wish_list_ids array property added to the document in products collection, containing the id of the users wish list.
  • product_ids array property not added to document.wish_lists[0] in users collection.
  • Retrieve products in wish list by calling $wishList->products
  • Empty collection when calling $product->wish_lists

Example documents after running the code:

users collection:

{  
   "_id":"5d5704d8fdce27086910aec3",
   "name":[  
      "John",
      "Smith"
   ],
   "wish_lists":[  
      {  
         "name":"TEST_LIST",
         "_id":"5d59e736fdce271cf23b3a34"
      }
   ]
}

products collection:

{
   "_id":"5d55b24ae924eb31649d20b7",
   "name":"Product 1",
   "wish_list_ids":[
      "5d59e736fdce271cf23b3a34"
   ]
}

NOTE: I tried running this code, but using a base collection to store the wish lists, instead of storing them inside users, and it worked completely fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs investigationNeed investigation about bugs described in issuebug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions