doulu7921 2015-12-03 16:02
浏览 43

Symfony 2表单以日期为关键但不在表单上

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?

  • 写回答

3条回答 默认 最新

  • dongsuishou8039 2015-12-03 16:30
    关注

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算