8

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

Dan
  • 6,265
  • 8
  • 40
  • 56

1 Answers1

35

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
Dan
  • 6,265
  • 8
  • 40
  • 56
  • thx, was looking for this answer a long time... in the last project, my colleague did this and i couldn't figure out what i was doing wrong... – AlexK Oct 23 '13 at 14:11
  • 3
    Make sure you put this under "stof_doctrine_extensions" not in "doctrine" as it took me a while to see my mistake – TroodoN-Mike Apr 11 '14 at 12:29
  • I had the same problem, and i add these configurations, but I got this error: `There is no extension able to load the configuration for "stof_doctrine_extensions" `. Can anyone help me? – Laiso Oct 16 '17 at 12:00