Kyslik/column-sortable

Issue with parent relations

psychonetic opened this issue · 8 comments

First of all: great package!

I found an issue, if the model relates itself. Imagine you have a model Category, which references its parent category.

class Category extends Model
{

 public function parent() {
    	return $this->belongsTo('App\Category', 'parent_id', 'category_id');
 }

}

If you try to sort by parent.name or anything related to the parent category, it will fail, because both tables have the same name. But it can be fixed easily if one of the related tables would get an alias by default, when executing the join.

I may create a pull request.

By the way: Is the package still maintained?

Any PRs are welcome :) and yes package is still maintained.

@psychonetic any progress on creating PR?


I guess, best way to do this would be to detect self-referencing relationship and only use aliasing if detection returns true.

@Kyslik I wasn't at home during weekend. We open a pull request today :)

@Kyslik Thanks, you are fast :) I will check that out and will edit this answer here.

@psychonetic yea, I pushed it to 5.4 now; not tagged yet; so you can install it using specific hash

require: {
    "kyslik/column-sortable": "dev-L5.4#a43a5a53b8a1ebcd2eadad54689e61a9eeb15d15"
}

Please do test and report here :)

@Kyslik Yep, I already did. It's working perfectly! So you can tag it.

Done ;), 5.4.6

Thanks for reporting the bug.