So I have this simple form in symfony where I only have 3 fields. All ids working to here one entity that is not on form but it's in the DB is column df_date and it's type DATE in DB.
Here is that entity:
/**
* @ORM\Column(type="date")
* @ORM\Id
*/
protected $df_date;
/**
* Set df_date
*
* @param \DateTime $dfDate
* @return WeatherSpecials
*/
public function setDfDate($dfDate)
{
$this->df_date = $dfDate;
return $this;
}
/**
* Get df_date
*
* @return \DateTime
*/
public function getDfDate()
{
return $this->df_date;
}
Controller:
public function ajax_weather_specialsAction(Request $request, $main_id)
{
$params = array();
if (!$main_id) {
/*
$repository = $this->getDoctrine()
->getRepository('AppBundle:WeatherSpecials');
$weather_specials = $repository->find($main_id);
*/
} else {
$weather_specials = new WeatherSpecials;
}
$form = $this->createFormBuilder($weather_specials)
->add('df_weather', 'choice', array(
'choices' => array(
null, 'SU', 'PC', 'CL', 'RN'
),
'label' => false, 'required' => true,
))
->add('df_temptur', 'number', array('label' => false, 'required' => true))
->add('df_specday', 'text', array('label' => false, 'required' => false))
->add('save', 'submit', array('attr' => array('class' => 'btn btn-primary')))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($weather_specials);
// How to set df_date here???
$em->flush();
}
}
$data['status'] = 'success';
$data['html'] = $this->render('sales_tabs/weather_specials.twig', array('form' => $form->createView()))->getContent();
return new JsonResponse($data);
}
And the question is, how to set the df_date before I persist that form to the DB?
Assuming that WeatherSpecials
is the entity that you want to set df_date
on either set it before you persist;
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$weather_specials->setDfDate(new \DateTime());
$em->persist($weather_specials);
$em->flush();
}
or use the doctrine HasLifecycleCallbacks;
/**
* WeatherSpecials
*
* @ORM\Table(name="weather_specials")
* @ORM\Entity(repositoryClass="AppBundle\Entity\WeatherSpecialsRepository")
* @ORM\HasLifecycleCallbacks
*/
class WeatherSpecials
{
// other stuff
/**
* @ORM\PrePersist
*/
public function prePersist(\Doctrine\ORM\Event\LifecycleEventArgs $event)
{
$this->df_date = new DateTime();
}
/**
* @ORM\PreUpdate
*/
public function preUpdate(\Doctrine\ORM\Event\LifecycleEventArgs $event)
{
$this->df_date = new DateTime();
}
}
You can set the date manually before persisting the entity.
Consider the following piece of code
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$weather_specials->setDfDate(new \DateTime());
$em->persist($weather_specials);
$em->flush();
}
}
This will set the date to the current date and time. You can change the datetime values according to your needs.
You can even use Doctrine's preUpdate
or prePersist
callbacks.
UPDATE Your entity definition is incorrect. You should define $df_date
as a \Datetime
parameter. Change this in your code
/**
* @var \datetime
* @ORM\Column(type="date")
* @ORM\Id
*/
protected $df_date;
//the rest of your stuff
I found the issue, and Doctrine can kiss my ... with it's recommendations. It took me 3 days to figure this one out, it wold take me 5 minutes to figure that in PHP alone. enter link description here