19.
Weird Endpoint: The tagline as a Resource?
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
5 Comments
Hi Ryan,
How does Doctrine know to query for the Programmer using the `nickname` value? Does it check that the nickname property is part of the Programmer class and then does findOneBy(array('nickname' => $nickname)), or does it need an existing method (findOneByNickname($nickname)) in ProgrammerRepository?
Thank you!
Hey, Vlad!
I can help you with it. This feature called <a href="http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html">ParamConverter</a> in Symfony. Actually, in this case triggered the <a href="http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter">Doctrine Converter</a>. This guy do all that magic:
Programmerin our case);find()method on its repository, otherwise it try to find entity by criteria usingfindOneBy()('{nickname}' in our case)NotFoundHttpExceptionexceptionSo you don't need to have
findOneByNickname()method in your entity repository class.BTW, you can control mapping of route placeholders to the entity properties and even create your own converter.
Cheers!
Thank you for the explanation, Victor!
How do you control mapping of route placeholders to the entity properties and even create your own converter?
Hey, Vlad!
Actually, I don't create my own converters, for something complex I manually query an entity from database. It's more quickly than create a custom converter and is more obvious for complex code.
The simple mapping controlling looks like:
You should explicitly use
ParamConverterannotation. In this example we have an ID property for Post entity, but placeholder's name ispost_id, so we need to use a custom mapping here (options={"id" = "post_id"}).Search for 'mapping' on <a href="http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter">Doctrine Converter</a> page to find more mapping examples.
Cheers!
Thank you, Victor. I will make a not of that. Useful trick to have.
"Houston: no signs of life"
Start the conversation!