Chapters
36 Chapters
|
1:50:44
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: >=5.3.3
Subscribe to download the code!Compatible PHP versions: >=5.3.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
11.
Adding Dynamic Roles to each User
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
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.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.4", // v2.4.2
"doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
"doctrine/doctrine-bundle": "~1.2", // v1.2.0
"twig/extensions": "~1.0", // v1.0.1
"symfony/assetic-bundle": "~2.3", // v2.3.0
"symfony/swiftmailer-bundle": "~2.3", // v2.3.5
"symfony/monolog-bundle": "~2.4", // v2.5.0
"sensio/distribution-bundle": "~2.3", // v2.3.4
"sensio/framework-extra-bundle": "~3.0", // v3.0.0
"sensio/generator-bundle": "~2.3", // v2.3.4
"incenteev/composer-parameter-handler": "~2.0", // v2.1.0
"doctrine/doctrine-fixtures-bundle": "~2.2.0", // v2.2.0
"ircmaxell/password-compat": "~1.0.3", // 1.0.3
"phpunit/phpunit": "~4.1" // 4.1.0
}
}
9 Comments
Instead of keeping the roles in an json array, I would like to store them in an own role entity. I've tried it with an ManyToMany relation between User and Role entity. but if I try to login, symfony yells at me like "Type error: Argument 3 passed to Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken::__construct() must be of the type array, object given, called in /var/www/html/vendor/symfony/symfony/src/Symfony/Component/Security/Guard/AbstractGuardAuthenticator.php on line 38"
I don't know how I could handle this...
Hey Daniel Larusso!
Ah, I know the problem :). When you finish authentication, the guard authenticator calls
$user->getRoles()on your User object. This must return an array of roles. But with Doctrine, a relationship returns anArrayCollectionobject. Here's what you should do:A) Move your relationship to some other property - e.g.
$roleObjects(I can't think of a better name). Obviously, also change your getter & setter to be getRoleObjects/setRoleObjectsB)Add a new
getRoles()method that returns a true array:Cheers!
Hey weaverryan it was super effective (add "super effective" pokémon meme here)
I moved the relationship to a new $roleCollection property. (also added add, get & remove method)
at least I've changed getRoles() to
Thank you ryan :)
Woohoo! So happy to hear it! And thanks for posting your final code here for others :). I'm just sad I didn't *actually* get a pokémon meme ;).
Cheers!
Hi, i got an issue with the tutorial,
When you insert into the User entity the following code :
Doctrine can't let me update the database because it says :
Hey,
That is strange.. Probably you are trying to pass pretty old course on newer Doctrine version. For fast fixing use
jsontype instead ofjson_arrayCheers!
Hey!
I wondered why setter-functions always end with a "return $this" statement and thus return the whole object again? $this->roles = $roles seems to be sufficient to set the property. Thanks in advance :)
Yo Max!
Another great question! It's optional, but for chaining if you're into that sort of thing!
So, it's for syntactic sugar! I don't typically do this, mostly because I don't often find myself calling many setters in a row like this. But that doesn't make it wrong - just a preference :).
Cheers!
Wow, I never would have thought about this one :D Another amazing answer - thank you!!
"Houston: no signs of life"
Start the conversation!