Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Lucky you! You found an early release chapter - it will be fully polished and published shortly!

User Entity

This Chapter isn't
quite ready...

Rest assured, the gnomes are hard at work
completing this video!

Browse Tutorials

Coming soon...

We won't talk about security in this tutorial, but even still we do need the concept of a user because each treasure in the database is going to be owned by a user. And we'll be able to see which, which of course by user I mean Dragon. And then we should be able to see like which treasures belong to which user and maybe later, maybe even users can message other users about their treasures. So let's create a user class, find your terminal and run bin console. Make user, I could use make entity for this, but instead I'll may use make user just so it sets up a little bit of the security stuff that will need a future tutorial. So let's use user for the security class. Yes, we are gonna store users in the database and then for the use email for the main field. And then it asks us if we need to hash and check user passwords. If the hashed user password is actually stored and checked in your system, you do. If you have a system where your user submits a password but you validate on another server, you don't need that. That means it's the other server that's gonna be handling passwords. But I'm gonna say yes to this.

So this didn't do much. Fern Get Status created, the user entity, the repository class, and then a small update in security of that yammel. If you open config packages. The ammo, it's really basic in here. It just kind of set up our user provider. Nothing special. And again, we'll talk about that in the future. Tutorial. Inside the source entity directory, we have our U new user entity glass with ID email rolls and password, and then the getters and setters below. So nothing too fancy. This implements two interfaces that we need, but nothing that's, but those aren't going to be important for us right now. Now I wanna add one more feel to my user class, which is gonna be a username so that if dragons are talking to each other, they have these cool usernames that we can show. So let's spin back over and let's this time run, make entity. We'll update the user field. I'll add a username property. There'll be a string. 2 55 is good. Nodding on the database is good and done. Hit enter. One more time to exit. Awesome. And over here. Perfect. There's our username field. And while I'm here, I'm actually gonna add a little unique true that just makes it unique in the database.

Cool. So we have our new user entity class, so we need a migration for it back here, terminal run, symphony console, make migration. Perfect. Then I'll spin over and open that new migration file and yep, no surprises. It creates the user table. So close that up and run the migration with Symphony Console Doctrine. Migrations migrate. Beautiful. All right, so if we're gonna have this user entity, we probably want to have some nice F fixtures data for it. So let's use Foundry like we did for our dragon treasure. So run bin console, make factory, and we'll generate the factory for user. So just like before in the source factory directory, we now have a U new class user factory, which is really good at creating user objects. The only thing we really need to tweak in here is get defaults. I'm actually going to paste in new contents for this class, which you can copy from the code block on this page. All this said was update get defaults with some nice defaults. So password will be our password. And then it added a little after instantiation hook to hash that password. Finally, to actually create some fixtures with this. Open up at fixtures class

And we'll add user factory, colon, colon, create many, and let's just create 10. All right, let's see if that worked. Spin over and run. Symphony console doctrine, fixtures, load, and cool. No errors. All right, so we have a user class, we've got the migration for it. We've even got data fixtures for it, but it is not yet part of our api. If you refresh the documentation, there's still only treasure here. So let's make this part of our API next.

Leave a comment!

Login or Register to join the conversation
Cat in space

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

What PHP libraries does this tutorial use?

// composer.json
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "api-platform/core": "^3.0", // v3.0.8
        "doctrine/annotations": "^1.0", // 1.14.2
        "doctrine/doctrine-bundle": "^2.8", // 2.8.0
        "doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
        "doctrine/orm": "^2.14", // 2.14.0
        "nelmio/cors-bundle": "^2.2", // 2.2.0
        "nesbot/carbon": "^2.64", // 2.64.1
        "phpdocumentor/reflection-docblock": "^5.3", // 5.3.0
        "phpstan/phpdoc-parser": "^1.15", // 1.15.3
        "symfony/asset": "6.2.*", // v6.2.0
        "symfony/console": "6.2.*", // v6.2.3
        "symfony/dotenv": "6.2.*", // v6.2.0
        "symfony/expression-language": "6.2.*", // v6.2.2
        "symfony/flex": "^2", // v2.2.4
        "symfony/framework-bundle": "6.2.*", // v6.2.3
        "symfony/property-access": "6.2.*", // v6.2.3
        "symfony/property-info": "6.2.*", // v6.2.3
        "symfony/runtime": "6.2.*", // v6.2.0
        "symfony/security-bundle": "6.2.*", // v6.2.3
        "symfony/serializer": "6.2.*", // v6.2.3
        "symfony/twig-bundle": "6.2.*", // v6.2.3
        "symfony/ux-react": "^2.6", // v2.6.1
        "symfony/validator": "6.2.*", // v6.2.3
        "symfony/webpack-encore-bundle": "^1.16", // v1.16.0
        "symfony/yaml": "6.2.*" // v6.2.2
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
        "symfony/debug-bundle": "6.2.*", // v6.2.1
        "symfony/maker-bundle": "^1.48", // v1.48.0
        "symfony/monolog-bundle": "^3.0", // v3.8.0
        "symfony/stopwatch": "6.2.*", // v6.2.0
        "symfony/web-profiler-bundle": "6.2.*", // v6.2.4
        "zenstruck/foundry": "^1.26" // v1.26.0