Question

Is it possible to have multiple slugs on one table in Doctrine?

I tried this in my yaml-file:

Article:
  tableName: tst_article
  actAs:
     Sluggable:
       unique: true
       fields: [title]
       canUpdate: true
     Sluggable:
       unique: true
       fields: [text]
       name: secondSlug
  columns:
    id:
      type: integer(8)
      primary: true
      autoincrement: true
    category_id:
      type: integer(8)
    title:
      type: text(255)
    text:
      type: clob

But after generating the sql only the secondSlug was generated...

Was it helpful?

Solution

It is possible. In your table definition write:

public function setUp() {
    parent::setUp();

    $sluggable0 = new Doctrine_Template_Sluggable(array(
        'fields' => array(0 => 'name'),
        'unique' => true,
        'canUpdate' => true
    ));
    $this->actAs($sluggable0);

    $sluggable1 = new Doctrine_Template_Sluggable(array(
        'fields' => array(0 => 'native_name'),
        'unique' => false,
        'canUpdate' => false,
        'name' => 'native_name_slug'
    ));
    $this->actAs($sluggable1);
}

The problem is in YAML itself. You have something like this:

keyA:
  keyB: value
  keyB: value

What might be translated into:

array(
    'keyA' => array(
        'keyB' => 'value',
        'keyB' => 'value'
    )
);

So as you see there is definition of keyB and then keyB is overwritten with new value. So in your YAML file second definition overrides the first one.

How to solve that? I don't know, but I'll do some researches. Right now you're forced to declare your models in pure PHP.

OTHER TIPS

Article:
  tableName: tst_article
  actAs:
     Sluggable:
       unique: true
       fields: [title, text]
       canUpdate: true
  columns:
    id:
      type: integer(8)
      primary: true
      autoincrement: true
    category_id:
      type: integer(8)
    title:
      type: text(255)
    text:
      type: clob

Although changing libraries is not recommended, sometimes it's a necessary evil. A very small change allows you to declare Sluggable_1, Sluggable_2, etc. in your YAML.

--- a/lib/vendor/doctrine/Doctrine/Import/Builder.php
+++ b/lib/vendor/doctrine/Doctrine/Import/Builder.php
@@ -711,8 +711,10 @@ class Doctrine_Import_Builder extends Doctrine_Builder
     {
         // find class matching $name
         $classname = $name;
-        if (class_exists("Doctrine_Template_$name", true)) {
-            $classname = "Doctrine_Template_$name";
+        // HACK to allow multiple Sluggables
+        $classname = preg_replace('/_[0-9]+$/', '', $classname);
+        if (class_exists("Doctrine_Template_$classname", true)) {
+            $classname = "Doctrine_Template_$classname";
         }
         return "        \$" . strtolower($name) . "$level = new $classname($option);". PHP_EOL;
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top