Question

How do you query for a specific post that has an id and also has a slug in the route? This is for updating a specific post. I've tried adding in the slug into update route to no avail (the not found exception triggers), what is the proper way of finding a post with a id and a slug value?

Routes:

Show

 BloggerBlogBundle_blog_show:
    path:  /{id}/{slug}
    defaults: { _controller: BloggerBlogBundle:Blog:show }
    requirements:
        methods:  GET
        id: \d+

Update

BloggerBlogBundle_blog_update:
path:  /{id}/{slug}/update
defaults: { _controller: BloggerBlogBundle:Blog:update, id: $id }
requirements:
    id: \d+

Controller:

BlogController(update action)

  public function updateAction($id, Request $request)
{
    $em = $this->getDoctrine()
        ->getManager();

    $blog = $em->getRepository('BloggerBlogBundle:Blog')
        ->find($id);

    if (!$blog) {
        throw $this->createNotFoundException(
            'No blog was found for this id' . $id
        );
    }

    $form = $this->createFormBuilder($blog)
        ->add('title', 'text')
        ->add('author', 'text')
        ->add('blog', 'text')
        ->add('image', 'text')
        ->add('tags', 'text')
        ->add('save', 'submit')
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $em->flush();

        // return new Response('Blog was updated successfully');
        return $this->redirect($this->generateUrl('BloggerBlogBundle_homepage'));
    }

    $build['form'] = $form->createView();

    return $this->render('BloggerBlogBundle:Blog:create.html.twig', $build);
}
Was it helpful?

Solution

Ok, figured out what I was doing wrong. Needed to pass in $slug into the updateAction parameter and use findOneBy(array) per nifr (thanks for the assist).

Updated route:

BloggerBlogBundle_blog_update:
path:  /{id}/{slug}/update
defaults: { _controller: BloggerBlogBundle:Blog:update }
requirements:
    id: \d+

Updated controller:

public function updateAction($id, $slug, Request $request)
{
    $em = $this->getDoctrine()
        ->getManager();

    $blog = $em->getRepository('BloggerBlogBundle:Blog')
        ->findOneBy(array(
            'id'   => $id,
            'slug' => $slug
        ));

    if (!$blog) {
        throw $this->createNotFoundException(
            'No blog was found for this id/slug: ' . $id. $slug
        );
    }

    $form = $this->createFormBuilder($blog)
        ->add('title', 'text')
        ->add('author', 'text')
        ->add('blog', 'text')
        ->add('image', 'text')
        ->add('tags', 'text')
        ->add('save', 'submit')
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $em->flush();

        // return new Response('Blog was updated successfully');
        return $this->redirect($this->generateUrl('BloggerBlogBundle_homepage'));
    }

    $build['form'] = $form->createView();

    return $this->render('BloggerBlogBundle:Blog:create.html.twig', $build);
}

OTHER TIPS

You can query for an entry matching id and slug at the same time using doctrine's findOneBy(array $criteria) method.

$request    = $this->getRequest()->request;
$em         = $this->getDoctrine()->getManager();
$repository = $em->getRepository('AcmeYourBundle:Entity'):

$entity = $repository->findOneBy(array(
    'id'   => $request->get('id'),
    'slug' => $request->get('slug'),
));

The documentation chapter Working with objects # By simple conditions covers this topic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top