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

时间:2015-05-20 20:37:46

标签: php symfony

我有一个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()
        ;
    }
}

1 个答案:

答案 0 :(得分:2)

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

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)运行并且您的估计阴影设置完成时,会发生什么。但是,如果您将表单中的字段留空,则$this->setEstimatedHatchStart($date)$this->estimatedHatchEnd($date)会被调用并可能设置为NULL。至少这是我的猜测。尝试从表单中删除字段,看看是否有效。