Doctrine 中一个表可以有多个 slugs 吗?

发布于 2024-08-26 17:00:32 字数 520 浏览 7 评论 0原文

Doctrine 中一张桌子上可以有多个 slugs 吗?

我在我的 yaml 文件中尝试了这个:

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

但是在生成 sql 后,只生成了第二个Slug...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆伤 2024-09-02 17:00:32

这是可能的。在表定义中写入:

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);
}

问题出在 YAML 本身。你有这样的东西:

keyA:
  keyB: value
  keyB: value

可能会被翻译成:

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

正如你所看到的,有 keyB 的定义,然后 keyB 被新值覆盖。因此,在 YAML 文件中,第二个定义会覆盖第一个定义。

怎么解决呢?我不知道,但我会做一些研究。现在您被迫用纯 PHP 声明您的模型。

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.

清醇 2024-09-02 17:00:32
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
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
無心 2024-09-02 17:00:32

尽管不建议更改库,但有时这是一种必要的罪恶。一个非常小的更改允许您在 YAML 中声明 Sluggable_1Sluggable_2 等。

--- 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;
     }

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;
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文