framework icon indicating copy to clipboard operation
framework copied to clipboard

Eloquent returns empty collection same table

Open ceesvanegmond opened this issue 12 years ago • 23 comments

Consider the following:

class Post extends Eloquent
{
    protected $table = 'posts';

    public function children()
    {
        return $this->hasMany('Post', 'parent_id')
    }

    public function parent()
    {
        return $this->belongsTo('Post', 'parent_id');
    }
}

So, i'm having a tree like configuration. When I have an children post and I try to get the parent of this children post, the following SQL query is returned from Eloquent.

select * from `posts` where (select count(*) from `posts` where `posts`.`parent_id` = `posts`.`id`)

which returns an empty Collection. When debugging, I found out that the Collection is empty because of the use the word posts in the query. What would work is to alias those tables, like:

select * from posts u where (select count(*) from posts p where p.parent_id = u.id)

Is there something with Eloquent we can use to solve this, or this has to be an custom query?

ceesvanegmond avatar Jun 18 '14 12:06 ceesvanegmond

This should work. Could you paste code as to what you are trying to do?

marcvdm avatar Jun 18 '14 12:06 marcvdm

@marcvdm I'm simply doing:

$posts = Post::has('parent')->get();

returns an empty collection, because it's executing

select * from `posts` where (select count(*) from `posts` where `posts`.`parent_id` = `posts`.`id`)

that also returns nothing. The query should be something like

select * from posts u where (select count(*) from posts p where p.parent_id = u.id)

instead of the first one.

ceesvanegmond avatar Jun 18 '14 13:06 ceesvanegmond

I accomplished something similar to this using belongsToMany and using a hierarchy table.

    public function parents()
    {
        return $this->belongsToMany('Entity', 'hierarchy', 'id', 'parent_id');
    }

    public function children()
    {
        return $this->belongsToMany('Entity', 'hierarchy', 'parent_id', 'id');
    }

This case was used because entities could have multiple parents and/or children.

riotCode avatar Jun 18 '14 14:06 riotCode

In your case I'm thinking your solution might be

class Post extends Eloquent
{
    protected $table = 'posts';

    public function children()
    {
        return $this->hasMany('Post', 'parent_id', 'id'); // the 'id' here probably isn't necessary, but just in case
    }

    public function parent()
    {
        return $this->belongsTo('Post', 'id', 'parent_id');
    }
}

riotCode avatar Jun 18 '14 14:06 riotCode

@riotCode I'm afraid thats not the solution, same result, an empty collection.

ceesvanegmond avatar Jun 18 '14 14:06 ceesvanegmond

Interesting, with your setup

$posts = Post::has('parent')->get();

I can confirm I can not get anything. I can get a parent through the relationship though

$post = Post::find($id);

$post->parent;

riotCode avatar Jun 18 '14 19:06 riotCode

@riotCode I can confirm that the second method works, I'm afraid this doesn't solve the problem.

ceesvanegmond avatar Jun 18 '14 21:06 ceesvanegmond

@ceesvanegmond, yeah it doesn't they're different use cases but the relationship does work to an extent. It seems that the has() and whereHas() methods do not in this case, if we could change the Eloquent behavior to use alias like you said would give us this ability.

riotCode avatar Jun 18 '14 22:06 riotCode

Hmm, might not be possbile. Never setup a relatinoship like this.

taylorotwell avatar Jun 19 '14 04:06 taylorotwell

@taylorotwell Do you have some alternatives to solve this problem?

ceesvanegmond avatar Jun 19 '14 21:06 ceesvanegmond

Does the relationship load? Only the has query doesn't work correctly?

taylorotwell avatar Jun 19 '14 22:06 taylorotwell

@taylorotwell That is correct, the has and whereHas methods dont work. The relationship does load with the use of with or manually fetching it.

ceesvanegmond avatar Jun 19 '14 22:06 ceesvanegmond

OK, might just have to work around that with your own existence check for now. I'll explore ways to get it working.

On Thu, Jun 19, 2014 at 5:09 PM, Cees van Egmond [email protected] wrote:

@taylorotwell https://github.com/taylorotwell That is correct, the has and whereHas methods dont work. The relationship does load with the use of with or manually fetching it.

— Reply to this email directly or view it on GitHub https://github.com/laravel/framework/issues/4754#issuecomment-46623557.

taylorotwell avatar Jun 19 '14 22:06 taylorotwell

You could try Post::whereNotNull("parent_id")->get();

So when the parent_id is filled in it has a parent

marcvdm avatar Jun 19 '14 22:06 marcvdm

That doesn't give the expected result, in that case it would retrieve all the posts that are children of another post. The expected result is to retrieve all posts that have children.

riotCode avatar Jun 23 '14 16:06 riotCode

@riotCode He said he was doing $posts = Post::has('parent')->get(); meaning he wants all posts having a parent. So he does not want root Posts

marcvdm avatar Jun 23 '14 16:06 marcvdm

@marcvdm that's correct, my mistake. That would be an alternative solution for his case instead of using has()

riotCode avatar Jun 23 '14 18:06 riotCode

I think this is related to 4084.

Most of the relations, Illuminate\Database\Eloquent\Relations\ *, have a function getRelationCountQuery. These functions return this:

return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey));

But for self referencing relations the table needs to be aliased. Something like this:

    $parentTable = $this->getParent()->getTable();
    $relatedTable = $this->getRelated()->getTable();
    $qForeignKey = $this->getQualifiedForeignKey();
    $otherKey = $this->getOtherKey();
    if($parentTable == $relatedTable) {
      $query->from($parentTable.' as '.$parentTable.'2');
      $query->whereRaw($qForeignKey.' = '.$parentTable.'2.'.$otherKey);
      return $query;

marshnmedia avatar Oct 08 '14 15:10 marshnmedia

Is this fixed?

GrahamCampbell avatar Dec 26 '14 12:12 GrahamCampbell

This means basically that self-joins cannot work with Eloquent, right? Isn't that a pretty common use case?

franzliedke avatar Dec 29 '14 12:12 franzliedke