Symfony 3とbootstrap-datepickerで日付を保存しています。日付のオブジェクトが間違った日付と形式で保存される
私のフォームに記入すると、この場合、2012年4月25日の日付を保存することが期待されます。
私のデータシートで欲しいものは、04/25/2017です。
は、代わりに私は私のダンプにこれを取得する:
2017年1月25日00:04.000000
と私のデータベースで:
2017- - 25
ダンプ結果:
データベース値:
PlayLogController:
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\PlayLog;
use AppBundle\Entity\Game;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Playlog controller.
*
* @Route("playlog")
*/
class PlayLogController extends Controller
{
/**
* Lists all playLog entities.
*
* @Route("/", name="playlog_index")
* @Method("GET")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$playLogs = $em->getRepository('AppBundle:PlayLog')->findAll();
return $this->render('playlog/index.html.twig', array(
'playLogs' => $playLogs,
));
}
/**
* Creates a new playLog entity.
*
* @Route("/{gameId}/new", name="playlog_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request, $gameId)
{
$playlog = new PlayLog();
$em = $this->getDoctrine()->getManager();
$game = $em ->getRepository(Game::class)->find($gameId);
$playlog->setGame($game);
$form = $this->createForm('AppBundle\Form\PlayLogType', $playlog);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/* @var $playLog PlayLog */
$playlog = $form->getData();
// echo $playlog->getGame()->getId() .'!';
$em->persist($playlog);
$em->flush();
}
return $this->render('playlog/new.html.twig', array(
'playLog' => $playlog,
'form' => $form->createView(),
));
}
/**
* Finds and displays a playLog entity.
*
* @Route("/{id}", name="playlog_show")
* @Method("GET")
*/
public function showAction(PlayLog $playLog)
{
$deleteForm = $this->createDeleteForm($playLog);
return $this->render('playlog/show.html.twig', array(
'playLog' => $playLog,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing playLog entity.
*
* @Route("/{id}/edit", name="playlog_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, PlayLog $playLog)
{
$deleteForm = $this->createDeleteForm($playLog);
$editForm = $this->createForm('AppBundle\Form\PlayLogType', $playLog);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('playlog_edit', array('id' => $playLog->getId()));
}
return $this->render('playlog/edit.html.twig', array(
'playLog' => $playLog,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a playLog entity.
*
* @Route("/{id}", name="playlog_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, PlayLog $playLog)
{
$form = $this->createDeleteForm($playLog);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($playLog);
$em->flush();
}
return $this->redirectToRoute('playlog_index');
}
/**
* Creates a form to delete a playLog entity.
*
* @param PlayLog $playLog The playLog entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(PlayLog $playLog)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('playlog_delete', array('id' => $playLog->getId())))
->setMethod('DELETE')
->getForm()
;
}
}
PlayLogType:
<?php
namespace AppBundle\Form;
use AppBundle\Entity\PlayLog;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PlayLogType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('date', DateType::class, array(
'widget' => 'single_text',
'html5' => false,
'attr' => ['class' => 'js-datepicker'],
'format' => 'mm/dd/yyyy'
)
);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => PlayLog::class
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_playlog';
}
}
私は日付ピッカーのために使用するスクリプト:
<script type="text/javascript">
$(document).ready(function() {
$('.js-datepicker').datepicker({
format: 'mm/dd/yyyy'
});
});
</script>
素早く答えてくれてありがとうございます。しかし、私はまだこの結果を得られません:04/25/2017、 :2017-04-25、それはほとんどの年を除いてほしいです、私は最後にそれが欲しいです。 – Ducky
これは同じ日付ですが、フォーマットが異なります。私は表示された日付形式を変更する可能性のあるmysqlクライアントの設定があると思います。 –