I have a table with domains and their start dates. I want when I click on a domain, a form for edit(update) to be shown with its fields filled with the current information. I have made this so far:
class Domains
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $main_domain;
/**
* @ORM\Column(type="date", nullable=true)
*/
protected $start_date;
… with setters and getters for each property.
I create a class EditType for building the form:
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('start_date', 'date', array('widget' => 'single_text', 'format' => 'yyyy-MM-dd'));
}
with a getName() method and here comes the problem with the EditController:
class EditController extends Controller
{
/**
* @Route("/fc/edit/{id}")
*/
public function editAction($id, Request $request)
{
$domain = $this->getDoctrine()
->getRepository('AcmeAbcBundle:Domains')
->find($id);
if (!$domain)
{
throw $this->createNotFoundException('No domians found');
}
$form = $this->createForm(new EditType(), $domain);
if($request->getMethod() != 'POST')
{
return $this->render('AcmeAbcBundle:Edit:form.html.twig', array(
'id'=>$id, 'domain'=>$domain, 'form' => $form->createView() ));
}
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
print_r($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getEntityManager();
$domain = $em->getRepository('AcmeAbcBundle:Domains');
if (!$domain)
{
throw $this->createNotFoundException('There is no such domain');
}
$domain->setStartDate($request->getStartDate());
$em->flush();
}
return $this->redirect($this->generateUrl('homepage'));
}
I don’t have an idea how it should be 🙁 I tried some things but they didn’t seem to work.
$domain->setStartDate($request->getStartDate());
Here it doesn’t find the method, if it is $request->start_date it does’t find the property …
To get the start_date POST parameter, you need to do:
Also notice that these lines
after
are unneccessary as you already got the domain object further up in the function. In fact they are wrong as you are not getting any domain object, you are getting the entityrepository instead.