08.
Tricks with ArrayCollection
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.
13 Comments
I was afraid you were not going to talk about the right way to do it :p
Loading all the notes in memory, just to count them afterward is really not ideal
Hey orys
Ryan likes to save the best to the end :D
Hello!!
I have a problem... i`m trying select from a field where is stored as array...
sample.. i Have the [ roles ] field in database.
Now i need select all users where roles = "ROLE_USER"
But some of they has: "ROLE_USER", "ROLE_VENDOR", "ROLE_FINANCIAL"
how can i query for all where contains ROLE_USER?
Hey Raphael!
This is the big disadvantage of using Doctrine's array type (array or json_array). You can't really query on this - it's ultimately just a string in the database. However, you can technically query a "fuzzy" query - but it's a little ugly:
If all you need are simple queries - this works in practice. However, if you need to do more complex query, then you'll may need another solution. In this case, I would create a Role entity (it would basically just have a string "role" property) and make a ManyToMany relationship from User to Role. Your User class would look something like this:
As you can see, you create a normal ManyToMany relationship, then simply loop over that relationship to return the array of string roles in getRoles() (which is what Symfony's security system wants). Then, you can properly query for exact matches.
Cheers!
Yeah.. its really ugly... but it worked... !!! cheers!!!
i`ll try the second way later... it seems to be good!!
Thank you for answering me daily!!! i asked around 10 questions... you`re rock!!!
Hi,
I'm wondering why my autocomplete doesn't work when typing $genus->getNotes()->filter in showAction. I also cannot go to the declaration when clicking on getNotes(). It did work in getNotesActions, though.
Hey Helene Shaikh!
Hmm, so I don't know the answer exactly - I would expect you to get autocomplete there. However, I can tell you a bit about what's going on :). In
showAction, we query for the$genususing$em->getRepository(...). If you have the Symfony plugin installed and enabled for this project, it should be smart enough to know that thefindOneBymethod will return aGenusobject. And so, it should auto-complete when you call$genus->getNotes(). The fact that you can't go to the declaration ofgetNotes()tells me that PhpStorm - for some reason - doesn't realize that$genusis aGenusobject. You can always hint it if you want to:In
getNotesAction, we relied on the automatic query mechanism so that we havegetNotesAction(Genus $genus)where we've type-hinted the$genusargument. In this case, thanks to the type-hint, PhpStorm 100% knows that$genusis aGenusobject and so you can happy autocompletion.Cheers!
Same for me
Hey Ruben,
Does Ryan's comment below help you? I think "@var Genus $genus" annotation should do the trick. Also, please, ensure you have a proper return annotation for Genus::getNotes() method to get further autocompletion. And I'd recommend to upgrade PhpStorm and its plugins if you don't yet.
Cheers!
What is this get('logger') line about? There wasn't any in the previous chapter.
Yo Maksym,
`$this->get('logger')` or `$this->container->get('logger')`, which is the same, is a service call. Service container returns a Logger object which you can use in order to make some logs in your app. Actually, this course is the 4th in the Symfony 3 track. Please, check the previous one out where we explain some basic things about Service Container and Dependency Injection.
Cheers!
I just wonder how it has appeared miraculously between the chapters...
Well, some Symfony-related courses imply basic Symfony background.
"Houston: no signs of life"
Start the conversation!