2014-12-26 6 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) 
(I는 입력이 예에서 실제 날짜를 시도)

다음은 데이터기구 파일입니다

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

나는 많은 다른 유형의 updatedAt의 데이터를 inpud하려고하지만 올바른 데이터 유형을 얻거나 비워두고 코드를 얻을 수 없습니다.

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

가 어떻게이 문제를 해결 얻을 수 있습니다 : 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

관련 문제