2

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.

1 Answer 1

1

First of all, if you are still learning Symfony, I would suggest not to use any CRUD generator. Better doing things manually at the first time, than use some sort of helper. Anyways, if I understand your question, you should set the current logged user as the article user:

// Get logged user and set it as article user
$loggedUser = $this->get('security.context')->getToken()->getUser();
$entity->setUser($loggedUser);

// Create edit form
$editForm = $this->createForm(new EditStaticPageType(), $entity);

// Bind the request if POST
if('POST' === $request->getMethod()) {
    $editForm->bind($request);
}

// GET or invalid POST form
if('GET' === $request->getMethod() || !$editForm->isValid()) {
    return $this->render('BlogBundle:StaticsManagement:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
    ));
}

// POST with valid form so persist
$em->persist($entity);            
$em->flush();

Then remove the user field from the article edit form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.