更新DateTime在原则不可能 [英] Updating DateTime in doctrine impossible
本文介绍了更新DateTime在原则不可能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在控制器中使用Doctrine更新 DateTime
。
页面的目的是上传数据库上的照片只有一次用户上传是允许的。
一切正常,但$ code> DateTime 不会保留在数据库中。 / p>
这是我的控制器:
public function delationAction(Request $ request )
{
$ _SESSION ['AriseID'] =boucas2013;
$ finder = new Finder(); ('/ var / www / html / separatiiste / web / uploads')中的
$ finder->
$ 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('risingID'=> $ _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'=> $照片,
));
}
else {
return $ this-> render('SlothBundle:Default:delation.html.twig',array(
'photos'=> $照片,
));
}
}
我的实体用户
/ **
* @var \DateTime
*
* ORM\Column(name =lastPost,type =time,length = 255)
* /
private $ lastPost;
/ **
* Get lastPost
*
* @return DateTime
* /
public function getLastPost()
{
return $ this-> lastPost;
}
/ **
*更新lastPost
*
* @return DateTime
* /
public function updateLastPost )
{
$ this-> lastPost = new \DateTime(now);
return $ this;
}
public function __construct()
{
$ this-> lastPost = new \DateTime(now);
}
每次 var_dump
返回:
object(DateTime)#344(3){
[date] => string(26)1970-01-01 14:46:31.000000
[timezone_type] => int(3)
[timezone] => string(12) /巴黎
解决方案
怀疑你的注释是错误的。
你使用的类型是 time
,而最有可能是 datetime
。
尝试:
/ **
* @var \DateTime
*
* @ ORM\Column(name =lastPost,type =datetime,length = 255)
* /
private $ lastPost;
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"
}"
解决方案
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;
这篇关于更新DateTime在原则不可能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文