2014-12-26 150 views
1

我想在我的网站中设置一个初始AdminUser。当我将它加载到数据库时,由于“updateAt”字段而无法上载。我收到以下错误,当我离开setUpdatedAt空白:Symfony DataFixtures设置默认值

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'updatedAt' ca 
nnot be null 

我在夹具文件失败设置的任何值,我得到以下错误:(我曾经尝试输入在这个例子中,实际为准)

[Symfony\Component\Debug\Exception\FatalErrorException] 
Parse Error: syntax error, unexpected '"2014-12-26 21:01:40"' (T_CONSTANT_E 
NCAPSED_STRING), expecting identifier (T_STRING) 

这里是数据文件灯具:

<?php 

namespace Usuarios\AdminBundle\DataFixtures\ORM; 

use Doctrine\Common\DataFixtures\FixtureInterface; 
use Doctrine\Common\Persistence\ObjectManager; 
use Usuarios\UsersBundle\Entity\User; 

class LoadUserData implements FixtureInterface 
{ 
/** 
* {@inheritDoc} 
*/ 
public function load(ObjectManager $manager) 
    { 
    $userAdmin = new User(); 
    $userAdmin->setUsername('Admin'); 
    $userAdmin->setPlainPassword('1234'); 
    $userAdmin->setEmail('[email protected]'); 
    $userAdmin->setRoles(array('ROLE_ADMIN')); 
    $userAdmin->setEnabled('1'); 
    $userAdmin->setUpdatedAt(\"2014-12-26 21:01:40"); 

    $manager->persist($userAdmin); 
    $manager->flush(); 
    } 
} 

我试过很多不同类型INPUD在updatedAt数据,但我不能得到正确的数据类型或让代码留空。

这里是UpdatedAt实体代码:

<?php 

namespace Usuarios\UsersBundle\Entity; 

use DateTime; 
use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 
use FOS\UserBundle\Model\User as BaseUser; 
use Gedmo\Mapping\Annotation as Gedmo; 

/** 
* User 
* 
* @ORM\Table(name="0_00_users") 
* @ORM\Entity 
*/ 
class User extends BaseUser 

/** 
* @var datetime $updatedAt 
* 
* @Gedmo\Timestampable(on="update") 
* @ORM\Column(name="updatedAt", type="datetime", nullable=true) 
*/ 
protected $updatedAt; 

/** 
* Set updatedAt 
* 
* @param \DateTime $updatedAt 
* @return User 
*/ 
public function setUpdatedAt($updatedAt) 
{ 
    $this->updatedAt = $updatedAt; 

    return $this; 
} 

/** 
* Get updatedAt 
* 
* @return \DateTime 
*/ 
public function getUpdatedAt() 
{ 
    return $this->updatedAt; 
} 

我怎样才能解决这个问题呢?

回答

4

你错过了$updatedAt值。

这应该是学说的做法。删除你不想要的逻辑。

/** 
* //... 
* @ORM\HasLifecycleCallbacks 
*/ 
class User extends BaseUser 

    // ... 

    /** 
    * Tell doctrine that before we persist or update we call the updatedTimestamps() function. 
    * 
    * @ORM\PrePersist 
    * @ORM\PreUpdate 
    */ 
    public function updatedTimestamps() 
    { 
     if ($this->getCreatedAt() == null) { 
      $this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s'))); 
     } else { 
      $this->setUpdatedAt(new \DateTime(date('Y-m-d H:i:s'))); 
     } 
    } 
    // ... 
} 
1

您的日期必须是有效的\ DateTime,而不是字符串。试试这个:

$userAdmin->setUpdatedAt(new \DateTime("2014-12-26 21:01:40")); 
+0

谢谢!我错过了'新'标签。 – Garces