symfonystofdoctrineextensions

Symfony2 - StofDoctrineExtensionBundle


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.


Solution

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