Symfony 2 - 使用表单时无法设置Sluggable

时间:2021-12-20 06:46:20

I am trying to use the Sluggable behaviour from the Doctrine Extensions bundle:

我试图使用Doctrine Extensions包中的Sluggable行为:

http://gediminasm.org/article/sluggable-behavior-extension-for-doctrine-2

http://gediminasm.org/article/sluggable-behavior-extension-for-doctrine-2

I have set up a sluggable field in my entity using annotation but the value does not get set when I use a form to create an instance, which causes the following error:

我使用注释在我的实体中设置了一个可缓冲的字段,但是当我使用表单创建实例时,该值不会设置,这会导致以下错误:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'slug' cannot be null

SQLSTATE [23000]:完整性约束违规:1048列'slug'不能为空

Here's the code from my controller:

这是我的控制器的代码:

        $form = $this->createFormBuilder($section)
                        ->add('title', 'text')
                        ->getForm();

        if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

            if ($form->isValid()) {

                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($section);
                $em->flush();

                if (empty($id)) {
                    return $this->redirect($this->generateUrl('ContentBundle_section_new'));
                }
                else {
                    return $this->redirect($this->generateUrl('ContentBundle_section_edit', array('id' => $id)));
                }

            }
        }

And the sluggable field definition in the Entity class:

和Entity类中的可缓冲字段定义:

    /**
     * @Gedmo\Slug(fields={"title"})
     * @ORM\Column(length=128, unique=true)
     */
    private $slug;

If I add the slug field to the formbuilder and set a value manually, it works OK but obviously I don't want to be messing around with this.

如果我将slug字段添加到formbuilder并手动设置一个值,它可以正常工作,但显然我不想搞乱这个。

Can anyone help?

有人可以帮忙吗?

Thanks

谢谢

1 个解决方案

#1


30  

Got it.

得到它了。

I had forgotten to add the following line to the config.yml file:

我忘了将以下行添加到config.yml文件中:

sluggable: true

可怜的:真的

So it should read something like:

所以它应该是这样的:

stof_doctrine_extensions:
    default_locale: en
    translation_fallback: true
    orm:
        default:
            tree: true
            timestampable: true
            sluggable: true

#1


30  

Got it.

得到它了。

I had forgotten to add the following line to the config.yml file:

我忘了将以下行添加到config.yml文件中:

sluggable: true

可怜的:真的

So it should read something like:

所以它应该是这样的:

stof_doctrine_extensions:
    default_locale: en
    translation_fallback: true
    orm:
        default:
            tree: true
            timestampable: true
            sluggable: true