Reputation: 8087
I have a Clutch
entity like this:
<?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;
}
}
And here is my ClutchType
form:
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')
;
}
Now, what I would like to do is not show estimatedHatchStart
or estimatedHatchEnd
on the front end, and use the data from laidDate
to add 30 days and 40 days respectfully to give me the start and end hatch dates which will then be saved to database.
What is the best practice to do this? I have just read about the FormEvents::PRE_SUBMIT
event, but i am unsure if this is the correct way to go about it?
EDIT:
setLaidDate
function:
/**
* 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()
;
}
}
Upvotes: 1
Views: 788
Reputation: 869
There's nothing wrong with doing it straight in the entity. For example
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;
}
It would just mean that anytime $this->setLaidDate()
is called the other two fields will also be updated. You could also do this manually in the controller when you handle the form submission and before actually persisting the entity to the database.
Or my favorite way is making an entity manager. A service you use to help you create new empty entities and persist existing ones back to the database.
#services.yml
services:
my_custom_manager:
class: AppBundle\Entity\MyEntityManager
arguments: [ "@doctrine.orm.entity_manager" , 'AppBundle\Entity\MyEntity']
AppBundle/Entity/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;
}
}
And then in the controller
<?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
}
Or another easy way would be to just set it all by hand in the controller action. There's nothing wrong with that as well. All up to you really.
EDIT
If I'm to believe your FormType, you still have
->add('estimatedHatchStart')
->add('estimatedHatchEnd')
So what's happening when the form submits in the background $this->setLaidDate($date)
runs and your estimated hatches get set. But then $this->setEstimatedHatchStart($date)
and $this->estimatedHatchEnd($date)
are getting called and likely set to NULL
if you're leaving those fields in your form blank. At least this is my guess. Try removing the fields from the form and see if that works.
Upvotes: 2