Vinelab / NeoEloquent

The Neo4j OGM for Laravel
MIT License
633 stars 199 forks source link

Querying the relation of a non-persisted model shouldn't return all of that label #225

Closed Anahkiasen closed 4 years ago

Anahkiasen commented 7 years ago

Imagine I have an User model that can have multiple roles defined as such:

public function roles()
{
    return $this->hasMany(Role::class, 'IS');
}

Now if I get an User model and fetch its roles, all good:

>>>Acme\Entities\Models\User::first()->roles->count()
=> 1

But if I do this on a new model, it will return all existing roles:

>>> (new Acme\Entities\Models\User)->roles->count()
=> 16

Instead of nothing like it does with normal Eloquent, example on another project of mine that does not use neo4j:

>>> Acme\Entities\Models\User::first()->groups->count()
=> 1
>>> (new Acme\Entities\Models\User)->groups->count()
=> 0

I've tripped on this a good bunch of times, mostly when writing upsert logic dealing with models that may or may not exist, some cases causing quite the headache so, thought I'd see if this could maybe be changed to behave like SQL Eloquent does.

Mulkave commented 6 years ago

What is the intended behaviour that you're expecting from this?

>>>(new Acme\Entities\Models\User)->roles->count()
=> 1
Anahkiasen commented 6 years ago

It should return 0 like it does in Eloquent in my opinion

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.