在symfony2中保存到数据库之前如何操作日期数据?


How can i manipulate date data before saving to the database in symfony2

我有一个这样的Clutch实体:

<?php
namespace Breedr'ClutchBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
 * Clutch
 *
 * @ORM'Table()
 * @ORM'Entity
 */
class Clutch
{
    /**
     * @var integer
     *
     * @ORM'Column(name="id", type="integer")
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var Breedr'BreedingPairsBundle'Entity'Pairs
     *
     * @ORM'ManyToOne(targetEntity="Breedr'BreedingPairsBundle'Entity'Pairs")
     * @ORM'JoinColumn(name="breeding_pair", referencedColumnName="id")
     */
    private $breedingPair;
    /**
     * @var 'DateTime
     *
     * @ORM'Column(name="laid_date", type="date")
     */
    private $laidDate;
    /**
     * @var 'DateTime
     *
     * @ORM'Column(name="estimated_hatch_start", type="date")
     */
    private $estimatedHatchStart;
    /**
     * @var 'DateTime
     *
     * @ORM'Column(name="estimated_hatch_end", type="date")
     */
    private $estimatedHatchEnd;
    /**
     * @var 'DateTime
     *
     * @ORM'Column(name="hatch_date", type="date")
     */
    private $hatchDate;
    /**
     * @var integer
     *
     * @ORM'Column(name="egg_amount", type="integer")
     */
    private $eggAmount;
    /**
     * @var Breedr'UserBundle'Entity'User
     *
     * @ORM'ManyToOne(targetEntity="Breedr'UserBundle'Entity'User")
     * @ORM'JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set breedingPair
     *
     * @param integer $breedingPair
     * @return Clutch
     */
    public function setBreedingPair($breedingPair)
    {
        $this->breedingPair = $breedingPair;
        return $this;
    }
    /**
     * Get breedingPair
     *
     * @return integer 
     */
    public function getBreedingPair()
    {
        return $this->breedingPair;
    }
    /**
     * Set laidDate
     *
     * @param 'DateTime $laidDate
     * @return Clutch
     */
    public function setLaidDate($laidDate)
    {
        $this->laidDate = $laidDate;
        return $this;
    }
    /**
     * Get laidDate
     *
     * @return 'DateTime 
     */
    public function getLaidDate()
    {
        return $this->laidDate;
    }
    /**
     * Set estimatedHatchStart
     *
     * @param 'DateTime $estimatedHatchStart
     * @return Clutch
     */
    public function setEstimatedHatchStart($estimatedHatchStart)
    {
        $this->estimatedHatchStart = $estimatedHatchStart;
        return $this;
    }
    /**
     * Get estimatedHatchStart
     *
     * @return 'DateTime 
     */
    public function getEstimatedHatchStart()
    {
        return $this->estimatedHatchStart;
    }
    /**
     * Set estimatedHatchEnd
     *
     * @param 'DateTime $estimatedHatchEnd
     * @return Clutch
     */
    public function setEstimatedHatchEnd($estimatedHatchEnd)
    {
        $this->estimatedHatchEnd = $estimatedHatchEnd;
        return $this;
    }
    /**
     * Get estimatedHatchEnd
     *
     * @return 'DateTime 
     */
    public function getEstimatedHatchEnd()
    {
        return $this->estimatedHatchEnd;
    }
    /**
     * Set hatchDate
     *
     * @param 'DateTime $hatchDate
     * @return Clutch
     */
    public function setHatchDate($hatchDate)
    {
        $this->hatchDate = $hatchDate;
        return $this;
    }
    /**
     * Get hatchDate
     *
     * @return 'DateTime 
     */
    public function getHatchDate()
    {
        return $this->hatchDate;
    }
    /**
     * Set eggAmount
     *
     * @param integer $eggAmount
     * @return Clutch
     */
    public function setEggAmount($eggAmount)
    {
        $this->eggAmount = $eggAmount;
        return $this;
    }
    /**
     * Get eggAmount
     *
     * @return integer 
     */
    public function getEggAmount()
    {
        return $this->eggAmount;
    }
    /**
     * Set user
     *
     * @param 'Breedr'UserBundle'Entity'User $user
     * @return Clutch
     */
    public function setUser('Breedr'UserBundle'Entity'User $user = null)
    {
        $this->user = $user;
        return $this;
    }
    /**
     * Get user
     *
     * @return 'Breedr'UserBundle'Entity'User 
     */
    public function getUser()
    {
        return $this->user;
    }
}

这是我的ClutchType形式:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $currentUser = $this->currentUser;
    $builder
        ->add('breedingPair', 'entity', array(
            'class' => 'Breedr'BreedingPairsBundle'Entity'Pairs',
            'property' => 'getPairAsString',
            'placeholder' => 'Choose a breeding pair',
            'query_builder' => function(EntityRepository $er) use ($currentUser) {
                return $er->createQueryBuilder('br')
                    ->where('br.user = :currentUser')
                    ->orderBy('br.breedingPairDate')
                    ->setParameter('currentUser', $currentUser);
            }
        ))
        ->add('laidDate')
        ->add('estimatedHatchStart')
        ->add('estimatedHatchEnd')
        ->add('hatchDate')
        ->add('eggAmount')
    ;
}

现在,我想做的是在前端不显示estimatedHatchStartestimatedHatchEnd,并使用laidDate的数据分别添加30天和40天,以给我开始和结束孵化日期,然后将其保存到数据库中。

这样做的最佳实践是什么?我刚刚读了关于FormEvents::PRE_SUBMIT事件,但我不确定这是否是正确的方式去做它?

编辑:

setLaidDate功能:

/**
 * Set laidDate
 *
 * @param 'DateTime $laidDate
 * @return Clutch
 */
public function setLaidDate($laidDate)
{
    $this->laidDate = $laidDate;
    $estimatedHatchStart = $laidDate->modify('+30days');
    $estimatedHatchEnd = $laidDate->modify('+40days');
    $this->setEstimatedHatchStart($estimatedHatchStart);
    $this->setEstimatedHatchEnd($estimatedHatchEnd);
    return $this;
}

ClutchController:

<?php
namespace Breedr'ClutchBundle'Controller;
use Symfony'Component'HttpFoundation'Request;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Method;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Route;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Template;
use Breedr'ClutchBundle'Entity'Clutch;
use Breedr'ClutchBundle'Form'ClutchType;
/**
 * Clutch controller.
 *
 */
class ClutchController extends Controller
{
    /**
     * Lists all Clutch entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();
        $user = $this->getUser();
        $entities = $em
            ->getRepository('BreedrClutchBundle:Clutch')
            ->findBy(array('user' => $user), array()
        );
        return $this->render('BreedrClutchBundle:Clutch:index.html.twig', array(
            'entities' => $entities,
        ));
    }
    /**
     * Creates a new Clutch entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity = new Clutch();
        $entity->setUser($this->getUser());
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();
            return $this->redirect($this->generateUrl('clutch_show', array('id' => $entity->getId())));
        }
        return $this->render('BreedrClutchBundle:Clutch:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }
    /**
     * Creates a form to create a Clutch entity.
     *
     * @param Clutch $entity The entity
     *
     * @return 'Symfony'Component'Form'Form The form
     */
    private function createCreateForm(Clutch $entity)
    {
        $currentUser = $this->getUser()->getId();
        $form = $this->createForm(new ClutchType($currentUser), $entity, array(
            'action' => $this->generateUrl('clutch_create'),
            'method' => 'POST',
        ));
        $form->add('submit', 'submit', array('label' => 'Create'));
        return $form;
    }
    /**
     * Displays a form to create a new Clutch entity.
     *
     */
    public function newAction()
    {
        $entity = new Clutch();
        $form   = $this->createCreateForm($entity);
        return $this->render('BreedrClutchBundle:Clutch:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }
    /**
     * Finds and displays a Clutch entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('BreedrClutchBundle:Clutch')->find($id);
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Clutch entity.');
        }
        $deleteForm = $this->createDeleteForm($id);
        return $this->render('BreedrClutchBundle:Clutch:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        ));
    }
    /**
     * Displays a form to edit an existing Clutch entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('BreedrClutchBundle:Clutch')->find($id);
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Clutch entity.');
        }
        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);
        return $this->render('BreedrClutchBundle:Clutch:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    /**
    * Creates a form to edit a Clutch entity.
    *
    * @param Clutch $entity The entity
    *
    * @return 'Symfony'Component'Form'Form The form
    */
    private function createEditForm(Clutch $entity)
    {
        $currentUser = $this->getUser()->getId();
        $form = $this->createForm(new ClutchType($currentUser), $entity, array(
            'action' => $this->generateUrl('clutch_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));
        $form->add('submit', 'submit', array('label' => 'Update'));
        return $form;
    }
    /**
     * Edits an existing Clutch entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('BreedrClutchBundle:Clutch')->find($id);
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Clutch entity.');
        }
        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);
        if ($editForm->isValid()) {
            $em->flush();
            return $this->redirect($this->generateUrl('clutch_edit', array('id' => $id)));
        }
        return $this->render('BreedrClutchBundle:Clutch:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    /**
     * Deletes a Clutch entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('BreedrClutchBundle:Clutch')->find($id);
            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Clutch entity.');
            }
            $em->remove($entity);
            $em->flush();
        }
        return $this->redirect($this->generateUrl('clutch'));
    }
    /**
     * Creates a form to delete a Clutch entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return 'Symfony'Component'Form'Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('clutch_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
}

直接在实体中做并没有错。例如

public function setLaidDate($date)
{
    $this->laidDate = $laidDate;
    $hatchDate = date('Y-m-d', strtotime($date . "+30 days"));
    $hatchDateEnd = date('Y-m-d', strtotime($date . "+40 days"));
    $this->setEstimatedHatchStart($hatchDate);
    $this->setEstimatedHatchEnd($hatchDateEnd);
    return $this;
}

这只是意味着任何时候$this->setLaidDate()被调用,其他两个字段也将被更新。您也可以在处理表单提交时,在实际将实体持久化到数据库之前,在控制器中手动执行此操作。

或者我最喜欢的方法是创建一个实体管理器。一个服务,用于帮助您创建新的空实体并将现有实体持久化回数据库。

#services.yml
services:
    my_custom_manager:
        class: AppBundle'Entity'MyEntityManager
        arguments: [ "@doctrine.orm.entity_manager" , 'AppBundle'Entity'MyEntity']

AppBundle/实体/MyEntityManager

namespace AppBundle'Entity;
use Doctrine'Common'Persistence'ObjectManager;
use AppBundle'Entity'MyEntity;

class MyEntityManager
{
    protected $class;
    protected $orm;
    protected $repo;
    public function __construct(ObjectManager $orm , $class)
    {
        $this->orm = $orm;
        $this->repo = $orm->getRepository($class);
        $metaData = $orm->getClassMetadata($class);
        $this->class = $metaData->getName();
    }
    public function create()
    {
        $class = $this->getClass();
        $myEntity = new $class;
        return $myEntity;
    }
    public function updateMyEntity(MyEntity $entity, $flush = true)
    {
        $date = $entity->getLaidDate();
        $hatchStart = date('Y-m-d', strtotime($date . "+30 days"));
        $hatchEnd = date('Y-m-d', strtotime($date . "+40 days"));
        $entity->setHatchStart($hatchStart);
        $entity->setHatchEnd($hatchEnd)
        $this->orm->persist($entity);
        if($flush)
        {
            $this->orm->flush();
        }
    }
    public function getClass()
    {
        return $this->class;
    }
}
然后在控制器

<?php
public function createAction(Request $request)
{
    $manager = $this->get('my_custom_manager');
    $newEntity = $manager->create();
    $form = $this->createForm(new MyType() , $newEntity);
    $form->handleRequest($request);
    if($form->isValid())
    {   
        $manager->updateMyEntity($newEntity);
    }
    //The rest of the code
}

或者另一种简单的方法是在控制器操作中手动设置。这也没什么错。一切都取决于你。

编辑

如果我相信你的FormType,你还有

->add('estimatedHatchStart')
->add('estimatedHatchEnd')

那么,当表单在后台提交时,$this->setLaidDate($date)运行并设置估计的hatch时,会发生什么呢?但然后$this->setEstimatedHatchStart($date)$this->estimatedHatchEnd($date)被调用,并可能设置为NULL,如果你离开这些字段在你的表格空白。至少这是我的猜测。尝试从表单中删除字段,看看是否有效。