问题
I would like to update DateTime with Doctrine in my controller.
The aim of page is to upload photos on a database. Only one upload by user is allowed.
Everything is working but the DateTime is not persisted in the database.
Here is my controller:
public function delationAction(Request $request)
{
$_SESSION['AriseID']="boucas2013";
$finder = new Finder();
$finder->in('/var/www/html/separatiiste/web/uploads');
$photos = array();
foreach ($finder as $file) {
array_push($photos, $file->getRelativePathname());
}
$em = $this->getDoctrine()->getManager();
$b=1;
$user = $this ->getDoctrine()
->getManager()
->getRepository('SlothBundle:User')
->findOneBy(array('ariseID' => $_SESSION['AriseID']));
if ($user==null) {
$b=0;
$user = new User();
$user->setAriseID($_SESSION['AriseID']);
}
$interval= date_diff(new \DateTime(),$user->getLastPost())->d;
var_dump($user->getLastPost());
if ($interval>0||$b==0) {
$photo = new Photo();
$form = $this->get('form.factory')->createBuilder(new PhotoType(), $photo)->getForm();
if ($form->handleRequest($request)->isValid()) {
$em->persist($photo);
$user->updateLastPost();
$em->persist($user);
$em->flush();
$photo->upload();
return $this->redirect($this->generateUrl('sloth_default_delation'));
}
return $this->render('SlothBundle:Default:delation.html.twig', array(
'form' => $form->createView(),
'photos' => $photos,
));
}
else {
return $this->render('SlothBundle:Default:delation.html.twig', array(
'photos' => $photos,
));
}
}
and there is the fonctions in my entity User
/**
* @var \DateTime
*
* @ORM\Column(name="lastPost", type="time", length=255)
*/
private $lastPost;
/**
* Get lastPost
*
* @return DateTime
*/
public function getLastPost()
{
return $this->lastPost;
}
/**
* Update lastPost
*
* @return DateTime
*/
public function updateLastPost()
{
$this->lastPost = new \DateTime("now");
return $this;
}
public function __construct()
{
$this->lastPost = new \DateTime("now");
}
every time the var_dump returns:
"object(DateTime)#344 (3) {
["date"]=> string(26) "1970-01-01 14:46:31.000000"
["timezone_type"]=> int(3)
["timezone"]=> string(12) "Europe/Paris"
}"
回答1:
I highly suspect your annotation is wrong.
You are using type time whereas it should most likely be datetime.
Try:
/**
* @var \DateTime
*
* @ORM\Column(name="lastPost", type="datetime", length=255)
*/
private $lastPost;
来源:https://stackoverflow.com/questions/33673123/updating-datetime-in-doctrine-impossible