Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Query for a List of Genuses

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.

Start your All-Access Pass
Buy just this tutorial for $12.00

Woh guys, we can already create new tables, add columns and insert or update data. There's just one big piece left: querying.

Let's create a new page that will show off all the genuses. Create public function listAction() and give it a route path of /genus:

... lines 1 - 30
/**
* @Route("/genus")
*/
public function listAction()
{
... lines 36 - 40
}
... lines 42 - 88

Querying? Get the Entity Manager

Remember, everything in Doctrine starts with the all-powerful entity manager. Just like before, get it with $em = $this->getDoctrine()->getManager():

... lines 1 - 33
public function listAction()
{
$em = $this->getDoctrine()->getManager();
... lines 37 - 40
}
... lines 42 - 88

To make a query, you'll always start the same way: $genuses = $em->getRepository(). Pass this the class name - not the table name - that you want to query from: AppBundle\Entity\Genus. This gives us a repository object, and hey! He's really good at querying from the genus table. In fact, it's got a bunch of useful methods on it like findAll() and findOneBy. Use findAll():

... lines 1 - 33
public function listAction()
{
$em = $this->getDoctrine()->getManager();
$genuses = $em->getRepository('AppBundle\Entity\Genus')
->findAll();
... line 40
}
... lines 42 - 88

What does this return exactly? Um... I don't know - so let's find out! Dump $genuses to see what it looks like:

... lines 1 - 37
$genuses = $em->getRepository('AppBundle\Entity\Genus')
->findAll();
dump($genuses);die;
... lines 41 - 88

Back to the browser! Go to /genus... and there's the dump! Ah, it's an array of Genus objects. That makes sense - Doctrine is obsessed with always using objects. And sure, you can make queries that only return some columns, but that's for later.

The AppBundle:Genus Alias

Back to the controller! Now change the Genus class name to just AppBundle:Genus:

... lines 1 - 37
$genuses = $em->getRepository('AppBundle:Genus')
->findAll();
... lines 40 - 88

Wait, what? Didn't I say this should be the class name? What is this garbage? It's cool - this is just a shortcut. Internally, Doctrine converts this to AppBundle\Entity\Genus. You can use either form, but usually you'll see the shorter one.... ya know, because programmers are efficient... or maybe lazy.

Leave a comment!

14
Login or Register to join the conversation
Default user avatar
Default user avatar rchdevel | posted 4 years ago

Should we use $em->getRepository(Genus::class) to get the repository? I think this is the new sf3 way. (?)

2 Reply

Hey!

Either way is Ok! You'll start to see this ::class syntax more in Symfony 3 simply because this syntax is new to PHP 5.5, and Symfony 3 is the first version that required PHP 5.5 (so we couldn't show this syntax in the documentation before Symfony 3). But yea, I kinda like this syntax - I think it's a little more "obvious" or understandable than AppBundle:Genus.

Cheers!

1 Reply
Default user avatar

Yeah, using the Genus::class is much better than the 'AppBundle:Genus' magic string.

Reply

Totally! and the best part (at least to me) is, it works with refactoring tools

Reply
Default user avatar
Default user avatar Maksym Minenko | posted 4 years ago

It's genera, not genuses. :P

1 Reply

we know :) - but probably most people won't. I did "sneak in" one use of "genera" in the fixtures like :) http://knpuniversity.com/sc...

1 Reply
Default user avatar

hi weaverryan
can u tell me how can i connect my symfony3 with mongodb i am new to this technologies

Reply

Hey Honey,

There's a third-party bundle: DoctrineMongoDBBundle. It helps you to use Symfony with Mongo DB. You have to install this bundle with Composer. Unfortunately, we have not had a screencast about this bundle yet - it's only in our plans, but you can take a look at their official docs to get started with it quickly: https://symfony.com/doc/cur...

Cheers!

Reply
Default user avatar
Default user avatar Luka Sikic | posted 4 years ago

I actually had to use "return new Response(dump($genuses));" to make it working.

Reply

Hey Luka,

Well, it's in case your want to print the dump output, but... you probably don't :) Because it's printed not on the page but in the Symfony Web Debug Toolbar - look for the "target" icon. Or, you can click on this icon and you will be redirected to the "Dumped Contents" tab of profiler where your dump output will be expanded full page. I bet you just missed it, that's why you think it doesn't work. Dump's output is printed on the page only when you use it in Twig templates.

Cheers!

Reply
Default user avatar
Default user avatar James Davison | posted 4 years ago

Sorry, just can't make it work, here is the error below, what does it mean?

Attempted to load class "GenusRepository" from namespace "AppBundle\Repository".

Did you forget a "use" statement for another namespace?

Reply

Hey James,

There is a problem of mapping between your directories and namespaces. You should double-check the namespace of "GenusRepository" class. Is it really has "namespace AppBundle\Repository;" namespace? Is this file really located in "src/AppBundle/Repository/GenusRepository.php" path?

Also look closely for spelling, maybe you missed some letter or just make a typo in it? Let me know if it doesn't help you.

Cheers!

Reply
Default user avatar
Default user avatar James Davison | victor | posted 4 years ago

Thanks, I really tried everything and even looked at the final file result and everything seems alright. I don't know why it is trying to get this namespace as I am not even calling it...
Here is a quick print screen:
http://jamesdavison.io/prin...

Reply

Ah, I think I know what's problem.

Open "Genus" entity, most probably you have a repository class specified there, right?

/**
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Repository\GenusRepository")
*/
class Genus

If no - it's weird, then, in PhpStorm, right click on "src/" folder, choose "Find in path" and search for "GenusRepository". PhpStorm will find all usage of GenusRepository class in your code in the src directory and you could remove it.

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*", // v3.1.4
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.6.4
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.3.11
        "symfony/monolog-bundle": "^2.8", // 2.11.1
        "symfony/polyfill-apcu": "^1.0", // v1.2.0
        "sensio/distribution-bundle": "^5.0", // v5.0.22
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "knplabs/knp-markdown-bundle": "^1.4", // 1.4.2
        "doctrine/doctrine-migrations-bundle": "^1.1" // 1.1.1
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.0.7
        "symfony/phpunit-bridge": "^3.0", // v3.1.3
        "nelmio/alice": "^2.1", // 2.1.4
        "doctrine/doctrine-fixtures-bundle": "^2.3" // 2.3.0
    }
}