Skip to content
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

Entity type / Model type #26

Closed
jonagoldman opened this issue Dec 26, 2015 · 3 comments
Closed

Entity type / Model type #26

jonagoldman opened this issue Dec 26, 2015 · 3 comments

Comments

@jonagoldman
Copy link

Having the ability to easily specify an entity type and a entity id is awesome!

Bouncer::allow($user)->to('edit', Post::class);
Bouncer::allow($user)->to('edit', $post);

The entity class name is saved as a string in the entity_type column. This is simple and straightforward but what do you thing about moving this column to its own table?

Pros:

  • Database normalization.
  • Easy to change the entity class name without touching the abilities table.

Cons:

  • Additional table relationship to manage.
@Arcesilas
Copy link
Contributor

I haven't dived so much in the code, but it seems this is the way polymorphic relations work. So I think it has to be here, not anywhere else.

@jonagoldman
Copy link
Author

Looks like a Morph Map could fix this issue.
Some people had this issue too and they added to the laravel repo what seems to be an undocumented method to map a string to a class name. See laravel/framework#9839
Will give it a try later and update.

@JosephSilber
Copy link
Owner

Look at this Laravel PR if you want to use the table names instead of the model class names.

Simply add this to your AppServieProvider's register method:

Relation::morphMap([
    App\Post::class,
]);

Add all of the models you use into that array (remember to import the Relation's namespace).

This is what I personally do for all my morphing models.

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