I am developing a blog in Symfony2. I have two entities in doctrine:
- The first one is called Articles contains a list with the articles of the blog: title, text, date and user, where the user is the author of the post.
- The second one is Users and is actually the users provider. Contains the name, the email,
etc.
I generate the CRUD of the Article entity with:
php app/console doctrine:generate:crud
I get the following updateAction in the controller file:
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('BlogBundle:Staticpage')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Staticpage entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createForm(new EditStaticPageType(), $entity);
$editForm->bind($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('static_edit', array('id' => $id)));
}
return $this->render('BlogBundle:StaticsManagement:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
));
}
When I open the form to create a new article or edit an existing one, a box that contains the users of my User entity is displayed, so I can select one of them, since both tables are linked in the Entity with the ManyToOne annotation. However, I don't want to select any user. I'd like to write in the Articles table the name of the user (the ID actually, since they are both linked) that is already logged in, and remove the list of the available users.
I already set up the security environment and it works properly, so this code will be always executed only if an user is logged in. I guess I have to edit the updateAction method, but I don't know how.