propelorm / Propel2

Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP
http://propelorm.org/
MIT License
1.26k stars 399 forks source link

Sluggable behavior generates truncated slugs when column has no size #1770

Open clemlatz opened 3 years ago

clemlatz commented 3 years ago

Adding Propel to a legacy project with an existing database, I've noticed that sluggable behavior might sometimes generate truncated slugs.

Here is my sluggable behavior config:

    <behavior name="sluggable">
      <parameter name="slug_column" value="url" />
      <parameter name="slug_pattern" value="{Name}" />
    </behavior>

and the url column that already existed:

<column name="url" phpName="Url" type="LONGVARCHAR"/>

With this, the generated slug always looses the name's last three characters (eg "Un long voyage" would become "un-long-voy") whatever the name's length is. I found that the generated limitSlugSize method is the culprit here:

    /**
     * Make sure the slug is short enough to accommodate the column size
     *
     * @param    string $slug            the slug to check
     *
     * @return string                        the truncated slug
     */
    protected static function limitSlugSize($slug, $incrementReservedSpace = 3)
    {
        // check length, as suffix could put it over maximum
        if (strlen($slug) > ( - $incrementReservedSpace)) {
            $slug = substr($slug, 0,  - $incrementReservedSpace);
        }

        return $slug;
    }

Because the used column has no size, the size is not inserted when this method is generated and the three last characters are always trimed out.

I've fixed my issue by converting the column to VARCHAR with a length of 256, but I thought it might be nice to have a warning or an error when using a column without size to store a slug.