Skip to content

Simple hasMany not working? #1383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nbyloff opened this issue Dec 6, 2017 · 5 comments
Closed

Simple hasMany not working? #1383

nbyloff opened this issue Dec 6, 2017 · 5 comments

Comments

@nbyloff
Copy link

nbyloff commented Dec 6, 2017

Granted I am trying to store both models in the same collection, but that shouldn't be an issue, should it? I tested that theory just in case and changed the collection name on one of the models, still the same issue.

My Models are simple:

class Revision extends Model
{
  protected $table = 'revisions';

  public function sections()
  {
    return $this->hasMany(Section::class);
  }
}
class Section extends Model
{
  protected $table = 'revisions';

  public function revision()
  {
    return $this->belongsTo(Revision::class);
  }
}

Here's my test to try and get the data right:

$revision = new Revision(['version' => 1]);
$revision->save();
$section = new Section(['name' => 'Section Name']);
$revision->sections()->save($section);
dd($revision);

My expectation is that $revision->sections will be a Collection with one item in it, and $section will have a revision_id property.

However there is no $revision->sections collection (there is no sections property period). revision_id is created on the Standard model, and has the correct id as a string.

This seems pretty straight forward and set it up according to any docs I have found. Why isn't this working? I'm on Laravel 5.5 and this package is set to "jenssegers/mongodb": "^3.3",

EDIT: I now see the L5.5 compatibility version seems to be (recently was) in Alpha. The README mentions that 3.3 should work with L5.5 though.

Am I doing something wrong or this an upgrade bug?

@nbyloff
Copy link
Author

nbyloff commented Dec 8, 2017

Using the same models, I even tried this to ensure the test was running as expected, but the sections collection is still empty and there is no sections property in the database.

Is no one else experiencing this in L5.5?

$r = new Revision(['version' => 1]);

$r->save();
$s = new Section(['name' => 'Section Name']);
$s->save();
$r->sections()->save($s);
$r->sections->add($s);
$r->save();

@nbyloff
Copy link
Author

nbyloff commented Dec 9, 2017

OK, I think I see what's happening. First of all, I read that a collection of IDs won't be stored in the sections property in Revision model. It will just run another query looking for Section models with revision_id == <SOME ID>.

So aside from that note, I started stepping through this package, it appears when I save like this:

$revision->sections()->save($section);

It creates a revision_id property on Section model AS A STRING.

However, when I run this code:

foreach($revision->sections as $section {
  dd($section);
}

It's trying to find all Section models with a revision_id as an OBJECT ID. I altered this code as a test:

https://github.com/jenssegers/laravel-mongodb/blob/master/src/Jenssegers/Mongodb/Query/Builder.php#L846-L853

And just had it return $id at the beginning of the code. My test worked and it started returning $section models.

So where is the breakdown here? Should this package be storing an ObjectId as the foreign key on the Section model, or does the problem lie elsewhere?

I have changed my relationship to look like this, and it works now. But I don't see this referenced anywhere in docs, so I know it's wrong (it will work for now)

public function sections()
  {
    return $this->hasMany(Section::class, 'revision_id', '_id->oid');
  }

EDIT Kind of works. Querying now works, but saving...the foreign key is null.

@nbyloff
Copy link
Author

nbyloff commented Dec 10, 2017

For whatever reason, even though composer.json had "jenssegers/mongodb": "^3.3" it pulled a very old version. I changed my composer to this "jenssegers/mongodb": ">=3.3", deleted the old package and ran the update.

It all works now. That was frustrating. haha.

@nbyloff nbyloff closed this as completed Dec 10, 2017
@saeedvz
Copy link

saeedvz commented Apr 16, 2018

My foreign keys are oid like bellow

{
    "_id" : ObjectId("5ad261321c76fb0c2800303f"),
    "slug" : "5ad26132ad114",
    "title" : "Title",
    "content" : "Content",
    "tags" : [ 
        "Tag1", 
        "Tag2"
    ],
    "user_id" : ObjectId("5ad2612e1c76fb0c28000922"),
}

in this case hasMany not working... How can i fix this?

@tolgatasci
Copy link

"user_id" : ObjectId("5ad2612e1c76fb0c28000922"),

objectID not result

{
    "_id" : ObjectId("5ad261321c76fb0c2800303f"),
    "slug" : "5ad26132ad114",
    "title" : "Title",
    "content" : "Content",
    "tags" : [ 
        "Tag1", 
        "Tag2"
    ],
    "user_id" : "5ad2612e1c76fb0c28000922",
}

checking successed
i want search object id hasmany

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants