0

我已经安装了 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"
},
....

我已经在我的 appKernel 中添加了这个包:

new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),

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

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

这是我的实体:

<?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;
}

在我的控制器中,我有:

/**
 * @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();
}

当我加载 /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 

我不确定我是否遗漏了一个步骤或配置,但我已经通读了几次文档并且没有看到任何内容。

4

2 回答 2

6

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

stof_doctrine_extensions:
    orm:
        default:
            timestampable: true

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

于 2013-05-26T18:00:39.340 回答
0

这对我有用:

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

最重要的是,stof_doctrine_extensions必须在教义之下。

于 2014-11-28T14:20:04.750 回答