Symfony2 - StofDoctrineExtensionBundle

Sha*_*rop 0 symfony stofdoctrineextensions

我已经安装了StofDoctrineExtensionsBundle并且无法使其正常工作.

这是我的composer.json配置:

.....
"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"
},
....
Run Code Online (Sandbox Code Playgroud)

我在appKernel中添加了bundle:

new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),

我在config.yml中添加了以下内容:

stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            sluggable: true
            sortable: true
Run Code Online (Sandbox Code Playgroud)

这是我的实体:

<?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;
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中我有:

/**
 * @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();
}
Run Code Online (Sandbox Code Playgroud)

当我加载/ admin/go时收到以下错误:

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 
Run Code Online (Sandbox Code Playgroud)

我不确定我是否缺少步骤或配置,但我已经阅读了几次文档并且没有看到任何内容.

Pie*_*eau 6

您的注释很好,但您没有启用Timestampable扩展程序:

stof_doctrine_extensions:
    orm:
        default:
            timestampable: true
Run Code Online (Sandbox Code Playgroud)

此外,您可以删除该setCreated方法,@ORM\HasLifecycleCallbacks()而不是Timestampable工作所必需的.