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

Uniqueness using the replicate method #20

Closed
trideout opened this issue Feb 20, 2014 · 4 comments
Closed

Uniqueness using the replicate method #20

trideout opened this issue Feb 20, 2014 · 4 comments

Comments

@trideout
Copy link

When a record is copied using the Eloquent replicate method the slug is not updated to be unique. The replicate method should check for uniqueness when saving the record.

@cviebrock
Copy link
Owner

I think the issue is that replicate() doesn't change the exists field of the new model. So, as far as eloquent-sluggable is concerned, the model already exists and the slug field is populated. The check on lines 48-54 of Sluggable.php will skip all processing:

// skip slug generation if the model exists or the slug field is already populated,
// and on_update is false ... unless we are forcing things!
if ( !$force )
{
    if ( ( $model->exists || !empty($model->{$save_to}) ) && !$on_update )
    {
        return true;
    }
}

Unless the replicate() method changes somehow (unsetting $this->exists or firing a specific replicate event), I don't see how I can fix this without forcing a reslugging on each save (which will likely break other stuff). Your safest bet is to force the slugging when you replicate:

$new_model = $old_model->replicate();
Sluggable::make($new_model,true);

@cviebrock
Copy link
Owner

On further inspection, I need to add another check when forcing the slugging to make this work. Fix is coming soon ... (although you will still need to manually run Sluggable::make() to force new slug generation).

@cviebrock
Copy link
Owner

Fixed in 1.0.8.

@trideout
Copy link
Author

Awesome, thanks

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