You are currently reading the 1.2 documentation. Switch to 2.0 

Sluggable

The Sluggable behavior is a nice piece of functionality that will automatically add a column to your model for storing a unique human readable identifier that can be created from columns like title, subject, etc. These values can be used for search engine friendly urls.

Lets expand our BlogPost model to use the Sluggable behavior because we will want to have nice URLs for our posts:

// models/BlogPost.php

class BlogPost extends Doctrine_Record
{
    // ...

    public function setUp()
    {
        // ...

        $this->actAs('Sluggable', array(
                'unique'    => true,
                'fields'    => array('title'),
                'canUpdate' => true
            )
        );
    }
}

Here is the same example in YAML format. You can read more about YAML in the YAML Schema Files chapter:

---
# schema.yml

# ...
BlogPost:
  actAs:
# ...
    Sluggable:
      unique: true
      fields: [title]
      canUpdate: true
# ...

Now look what happens when we create a new post. The slug column will automatically be set for us:

$blogPost = new BlogPost();
$blogPost->title = 'Test blog post';
$blogPost->body = 'test';
$blogPost->save();

print_r($blogPost->toArray());

The above example would produce the following output:

$ php test.php
Array
(
    [id] => 1
    [title] => Test blog post
    [body] => test
    [version] => 1
    [created_at] => 2009-01-21 17:57:05
    [updated_at] => 2009-01-21 17:57:05
    [slug] => test-blog-post
)

Notice how the value of the slug column was automatically set based on the value of the title column. When a slug is created, by default it is urlized which means all non-url-friendly characters are removed and white space is replaced with hyphens(-).

The unique flag will enforce that the slug created is unique. If it is not unique an auto incremented integer will be appended to the slug before saving to database.

The canUpdate flag will allow the users to manually set the slug value to be used when building the url friendly slug.

Here is a list of all the options you can use on the Sluggable behavior:

Name Default Description
name slug The name of the slug column.
alias null The alias of the slug column.
type string The type of the slug column.
length 255 The length of the slug column.
unique true Whether or not unique slug values are enforced.
options array() Any other options for the slug column.
fields array() The fields that are used to build slug value.
uniqueBy array() The fields that make determine a unique slug.
uniqueIndex true Whether or not to create a unique index.
canUpdate false Whether or not the slug can be updated.
builder array('Doctrine_Inflector', 'urlize') The Class::method() used to build the slug.
indexName sluggable The name of the index to create.

Questions and Feedback

If you find a problem with the documentation or have a suggestion, please register and open a ticket.

If you need support or have a technical question, you can post to the user mailing-list.