Search code examples
symfonydoctrine-ormslug

Symfony 2 - Sluggable not set when using form


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

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

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:

    /**
     * @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.

Can anyone help?

Thanks


Solution

  • Got it.

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

    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