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

feature request/patch: fix slugs - a method to fix all existing records' slugs #3

Open
jrevillini opened this issue May 16, 2010 · 0 comments

Comments

@jrevillini
Copy link

I added this to my copy of sluggable. It's a single use thing, so maybe you don't want to bloat your code with it, but here it is in case you think it would be useful for others:

/**
 * fixes slugs on all current objects (useful when working with existing data set).
 * @param AppModel $model the model that this behavior is applied to 
 */
public function fix_slugs (&$model) {

    //no need for extra junk
    $model->virtualFields = array();
    $model->contain = array();

    //fetch all data for current model
    $data = $model->find('all');

    //force overwriting of slugs, even if not set up for this model as such
    $this->settings[$model->alias]['overwrite'] = true;

    //save all data for model - slugs should be generated automatically
    $model->saveAll($data); 
}

It does have to be invoked somewhere for each model. I suppose it could be done in app_model, but I just went into the controller that needed slugs and added:

function fix_slugs () { $this->YOURMODELNAME->fix_slugs(); }

Then I went to http://localhost/YOURMODELNAME/fix_slugs and it slugged everything. I then commented out the method so it could never be called via URL GET.

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

1 participant