Editing
Databases and Symfony
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Repository classes == Repository classes isolate custom queries so the can be more easily and reliably reused and tested.<ref>[http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes Custom Repository Classes], Symfony documentation</ref> === Mapping repository classes === To map an entity class to a repository class, add the name of the repository class to the mapping definition in the entity class: <syntaxhighlight lang="php" highlight="7"> // src/AppBundle/Entity/Product.php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="AppBundle\Entity\ProductRepository") */ class Product { //... } </syntaxhighlight> === Automatically generating repository classes === Doctrine can generate the repository class for you by running the same command used earlier to generate the missing getter and setter methods: <syntaxhighlight lang="bash"> $ php app/console doctrine:generate:entities AppBundle </syntaxhighlight> '''''Note''' that this command uses the project's database settings to connect. If the server is set to `localhost` in `parameters.yml` and the command is run from another machine, the database connection won't be correct.'' === Adding custom queries to repository classes === Add methods to the repository class for each custom query: <syntaxhighlight lang="php" highlight="5-9"> class ProductRepository extends EntityRepository { public function findAllOrderedByName() { return $this->getEntityManager() ->createQuery( 'SELECT p FROM AppBundle:Product p ORDER BY p.name ASC' ) ->getResult(); } } </syntaxhighlight> Retrieve the results of the custom query in a controller: <syntaxhighlight lang="php"> $em = $this->getDoctrine()->getManager(); $products = $em->getRepository('AppBundle:Product') ->findAllOrderedByName(); </syntaxhighlight>
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information