0

I have installed the StofDoctrineExtensionsBundle and am having trouble getting it to work.

Here is my composer.json config:

.....
"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.2.*",
    "doctrine/orm": "~2.2,>=2.2.3",
    "doctrine/doctrine-bundle": "1.2.*",
    "twig/extensions": "1.0.*",
    "symfony/assetic-bundle": "2.1.*",
    "symfony/swiftmailer-bundle": "2.2.*",
    "symfony/monolog-bundle": "2.2.*",
    "sensio/distribution-bundle": "2.2.*",
    "sensio/framework-extra-bundle": "2.2.*",
    "sensio/generator-bundle": "2.2.*",
    "jms/security-extra-bundle": "1.4.*",
    "jms/di-extra-bundle": "1.3.*",
    "friendsofsymfony/user-bundle": "*",
    "doctrine/doctrine-migrations-bundle": "dev-master",
    "stof/doctrine-extensions-bundle": "~1.1@dev"
},
....

I have added the bundle in my appKernel:

new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),

I have added the following to my config.yml:

stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            sluggable: true
            sortable: true

Here is my Entity:

<?php

namespace SixString\PearBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="icon")
 * @ORM\HasLifecycleCallbacks()
 */
class Icon
{

....(other properties and getters/setters)

/**
 * @var \datetime $created
 *
 * @Gedmo\Timestampable(on="create")
 * @ORM\Column(type="datetime")
 */
protected $created;

/**
 * @param \datetime $created
 */
public function setCreated($created)
{
    $this->created = $created;
}

In my controller I have:

/**
 * @Route("/admin/go")
 */
public function goAction(){

    $icon = new \SixString\PearBundle\Entity\Icon();
    $icon->setName("shawn");
    $icon->setZip(12345);
    $icon->setType("go");

    $em = $this->getDoctrine()->getManager();
    $em->persist($icon);
    $em->flush();
}

When I load /admin/go I receive the following error:

An exception occurred while executing 'INSERT INTO icon (name, zip, thumb, created, updated, createdBy_id) VALUES (?, ?, ?, ?, ?, ?)' with params ["shawn", 12345, "go", null, null, null]:

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

I am not sure if I am missing a step or configuration but I have read through the documentation a few times and do not see anything.

Shawn Northrop
  • 5,826
  • 6
  • 42
  • 80

2 Answers2

6

Your annotation is fine, but you're not enabling the Timestampable extension:

stof_doctrine_extensions:
    orm:
        default:
            timestampable: true

Additionally, you may remove the setCreated method and @ORM\HasLifecycleCallbacks() is not necessary for Timestampable to work.

Pier-Luc Gendreau
  • 13,553
  • 4
  • 58
  • 69
  • Doh! I thought it was in there.. wow, you look to hard and you miss the simple things :). Thanks! – Shawn Northrop May 26 '13 at 18:03
  • Haha, I didn't see it when I commented 15 minutes ago either! – Pier-Luc Gendreau May 26 '13 at 18:04
  • 1
    In case anyone else is wondering where this is documented: https://github.com/stof/StofDoctrineExtensionsBundle/blob/master/Resources/doc/index.rst (It's in the documentation for the DoctrineExtensionsBundle, not just for DoctrineExtensions). – Sam Feb 12 '15 at 11:08
0

This is working for me:

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

Most important thing is, that stof_doctrine_extensions must be under doctrine.

Lackeeee
  • 459
  • 1
  • 8
  • 13