Usage:
Simple usage, without annotation:
/** * @Route("/blog/{id}") * -- This annotation is optionnal, this is the default value: * @ParamConverter("post", class="SensioBlogBundle:Post") */ public function showAction(Post $post) { } ... in the code: $id = $request->attributes->get("id"); $this->registry->getRepository($class, $options['entity_manager'])->find($id);
A bit more advanced, if the name of the route parameter isn't "id" (usefull for multiple converter in the same route):
/** * @Route("/blog/{id}/{id_tag}") * @ParamConverter("post", class="SensioBlogBundle:Post") * @ParamConverter("tag", class="SensioBlogBundle:Tag", options={"parameter" = "id_tag"}) */ public function showAction(Post $post, Tag $tag) { } ... in the code: $id = $request->attributes->get("id"); $this->registry->getRepository($class, $options['entity_manager'])->find($id); // then $id = $request->attributes->get("id_tag"); $this->registry->getRepository($class, $options['entity_manager'])->find($id);
If you want to use an explicit attribute to map the object you can use the attribute option:
/** * @Route("/blog/{title}") * @ParamConverter("post", class="SensioBlogBundle:Post", options={"attribute" = "title"}) */ public function showAction(Post $post) { } ... in the code: $attribute = "title"; // this is the attribute of the object $parameter = $attribute; // this is the name of the route parameter $value = $request->attributes->get($attribute); $critera = array($attribute => $value); $this->registry->getRepository($class, $options['entity_manager'])->findOneBy($criteria);
You can use you own method to retrieve the object:
/** * @Route("/blog/{title}") * @ParamConverter("post", class="SensioBlogBundle:Post", options={"method" = "myOwnFind", "parameter" = "title"}) */ public function showAction(Post $post) { } ... in the code: $parameter = "title"; $value = $request->attributes->get($parameter); $repository = $this->registry->getRepository($class, $options['entity_manager']); call_user_func(array($repository, $method), $value, $options); // you can optionnaly add some values in the options array in the annotation, it will be passed to your method through the second argument $options
Finaly, you can use them all:
/** * @Route("/blog/{post_title}/{tag_title}") * @ParamConverter("post", class="SensioBlogBundle:Post", options={"parameter" = "post_title", "method" = "myOwnFind", "myOwnOption" = "myOwnValue"}) * @ParamConverter("tag", class="SensioBlogBundle:Tag", options={"parameter" = "tag_title", "attribute" = "title_canonical") */ public function showAction(Post $post, Tag $tag) { } ... in the code: $parameter = "post_title"; $value = $request->attributes->get($parameter); $repository = $this->registry->getRepository($class, $options['entity_manager']); call_user_func(array($repository, $method), $value, $options); // then $attribute = "title_canonical"; $parameter = "tag_title"; $value = $request->attributes->get($parameter); $critera = array($attribute => $value); $this->registry->getRepository($class, $options['entity_manager'])->findOneBy($criteria);