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

Preferences hidden option? #2

Closed
timothyallan opened this issue Feb 22, 2016 · 3 comments
Closed

Preferences hidden option? #2

timothyallan opened this issue Feb 22, 2016 · 3 comments

Comments

@timothyallan
Copy link

Great library here, so easy!

One suggestions from myself:

Have a $hidden array as well as the "casts" and "defaults" so we can set it to not return created_at etc. fields? They're adding unnecessary bloat to my returning JSON as I just grab all preferences attached to the user.

Secondly, and unrelated... is there a way to do a cascading delete? So when I delete my model, all the prefs are deleted as well? So if I have a Department, with a hasMany relationship to Users that have preferences, I could go $department->users()->delete() as I do now, but have the prefs for the users get nuked as well?

@klaude
Copy link
Owner

klaude commented Feb 22, 2016

Thanks! I'm glad you like it!

Have a $hidden array as well as the "casts" and "defaults" so we can set it to not return created_at etc. fields? They're adding unnecessary bloat to my returning JSON as I just grab all preferences attached to the user.

That's a good idea. I was going to get back into this library pretty soon to add preference caching. I may do hidden preferences at the same time. There may be other Eloquent features to move into the library too. I'll dig around for 'em.

Secondly, and unrelated... is there a way to do a cascading delete? So when I delete my model, all the prefs are deleted as well? So if I have a Department, with a hasMany relationship to Users that have preferences, I could go $department->users()->delete() as I do now, but have the prefs for the users get nuked as well?

Depending on how fancy your RDBMS is you might be able to add an ON CASCADE DELETE clause into your user table's schema. Otherwise I'd probably do it in your user model's deleting or deleted event. Something might do the trick:

class User extends Eloquent
{
    use HasPreferences;

    // ...

    public static function boot()
    {
        parent::boot();

        User::deleting(function (User $user) {
            // Delete a user's preferences before deleting the user.
            $user->preferences()->delete();
        });
    }
}

Notes and disclaimers about data loss from deleting things and testing code that deletes before copy/pasting this directly into production. :) Hope this helps!

@timothyallan
Copy link
Author

Awesome. I didn't want to muck around with the Cascading too much as I'd end up forgetting, it's much easier to trace through code ;)

Your function worked well after I figured out that deleting never get's called on a bulk delete.

$department->users()->delete(); 

won't call it.

$department->users()->each(function ($user) 
    $users->delete();
});

works fine... although not as tidy as I'd like ;)

@klaude
Copy link
Owner

klaude commented Feb 23, 2016

Sucks that Eloquent won't call events on a bulk delete, but I guess that's what cascading on the database end is for. Looping through users to delete takes slightly longer and isn't as efficient, but so long as it works that's the important thing. :)

I just realized that the library has a small helper that can at least make the deleting/deleted event a little simpler. Call $user->clearAllPreferences() instead of $user->preferences()->delete() if you want.

Since this looks like it's been answered I'm going to close this issue. I'll add support for hiding columns when I get a spare moment or two. Thanks again!

@klaude klaude closed this as completed Feb 23, 2016
klaude added a commit that referenced this issue Apr 24, 2016
Add support for hiding preference attributes from JSON export by
defining hidden attributes in either the config file or the
MODEL_PREFERENCE_HIDDEN_ATTRIBUTES constant.

See issue #2 for the feature request.
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

2 participants