Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8370401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:54:12+00:00 2026-06-09T13:54:12+00:00

I have multiple objects that I’d like to be accessible via human-readable URLs, so

  • 0

I have multiple objects that I’d like to be accessible via human-readable URLs, so I am generating aliases (e.g. blog.com/this-is-an-alias/) for all database records.

What is the best practice for generating these automatically?

I’m currently hook the model’s ‘values()’ method and producing a new alias in there (based on the required ‘name’ field), but I can’t help but feel this could done more elegantly using, say, Kohana’s built-in filters.

Here’s the stripped-down model:

class Model_Category extends ORM {
    // relevant rules:
    public function rules(){
            return array(
            'alias' => array(
                array('max_length', array(':value', 63)),
                array(array($this, 'unique'), array(':field', ':value')),
            ),
            'name' => array(
                array('max_length', array(':value', 63)),
            ),
            // (...)
        );
    }

    // overrides default method:
    public function values(array $values, array $expected = NULL){
        if(!$this->_loaded){
            if($values['name'] && !$values['alias'])
                $values['alias'] = Helper_Form::to_alias($values['name']);
        }

        return parent::values($values, $expected);
    }
}

FYI, the to_alias function simply looks like this:

return strtolower(substr(trim(preg_replace('/[^0-9a-zA-Z]+/','-',$str),'-'),0,63));

So, my questions:

  • is it possible and/or advisable to access a model’s property–other than the one being filtered–from within a filter callback?
    (so far my tests seem to suggest no, or, at best, unreliably)
  • more importantly, how can I build-in a uniqueness test? i.e., after auto-generating an alias, how can I be sure that slug isn’t already in use without throwing the model’s rules() exception?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:54:13+00:00Added an answer on June 9, 2026 at 1:54 pm

    I would do that automatic generation using filters as you mentioned. Below is my conception of doing this. For filter function you can pass some specific parameters like :model, :value, :field and in this case I’d use :model to get access to current model’s properties.

    class Model_Category extends ORM {
    
        public function filters()
        {
            return array(
                'slug' => array(
                    array(array($this, 'create_slug'), array(':model')),
                ),
            );
        }
    
        public function create_slug($model)
        {
            $model = ($model instanceof ORM) ? $model : $this;
    
            $number = 1;
    
            $slug = UTF8::transliterate_to_ascii($model->name);
            $slug = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $slug);
            $slug = UTF8::strtolower(trim($slug, '-'));
            $slug = preg_replace("/[\/_|+ -]+/", '-', $slug);
    
            while ( ! $this->unique_slug($slug))
            {
                if ( ! preg_match('/\d+$/', $slug))
                {
                    $slug .= '-'.$number;
                }
                $slug = preg_replace('/\d+$/', $number++, $slug);
            }
    
            return $slug;
        }
    
        public function unique_slug($slug)
        {
            return (bool) DB::select(array('COUNT("*")', 'total_count'))
                ->from($this->_table_name)
                ->where('slug', '=', $slug)
                ->where($this->_primary_key, '!=', $this->pk())
                ->execute($this->_db)
                ->get('total_count');
        }
    
    }
    

    Above example may be not fully functional but I hope you get the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting returned a list with objects that have multiple attributes like so: results
I have a class that I want to contain multiple objects of something I
I have multiple Controller objects that either execute some script and then redirect to
I have a static class that holds a dictionary of objects. I have multiple
I have multiple objects, each with a series of attributes. I'd like the user
i would like to have an array of objects in excel that call one
I have multiple objects that inherit from a base class and am trying to
I have multiple data transfer objects that need their data transferred over to their
I have multiple function objects declared from the same function that deal with their
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.