Up & Running With Symfony 4 – Part 2: Database & Doctrine ORM

alright guys so in the last video we talked about what Symphony is we set it up we installed it we set up xampp and all that stuff and then we created an

article controller we created some views we created a nav bar with some links what else we created our index route with our index method so now what I want to do is just start to work with the

database because um up until now we just have some hard-coded data displaying in a view so we're going to be using doctrine which is the ORM that that symphony uses and it's used to connect

and to interact with the database inside of our application as well as within the console and if you want to read more about it aside from this tutorial you can go to the the documentation and

there's quite a bit here that you can you can look at as I said in the last video the documentation is pretty good there's just a lot of it alright so we need to actually install this with

composer so we're gonna go down here and say composer require doctrine maker all right and there's quite a bit to this so it takes a couple seconds all right

so that should be done all right now it's telling us to to modify our database URL config in the dot env file so we want to open up dot E and V and it actually added this part right here so

we what we want to change here is the database URL ok so we're using MySQL so we want to keep that we want you want your MySQL user which I'm using my root user and then password so I'm gonna put

in my password which is just 1 through 6 and then we want to change this right here DB name to whatever you want to call your database and I'm just gonna call mine simp what did I call this simp

art okay and that should be it that should be all we need to do here and we're gonna go ahead and save that okay now we can actually generate this database using our console so I'm gonna

go down here and again this clearing isn't working for some reason hopefully when I update vs code it works but down here we can say PHP bin slash console and we're gonna say Doctrine : database

: create and what that's gonna do is it's gonna look at this database this name here and our parameters and it's gonna try to create that database for us so let's run that so create a database

simp art so we didn't have to go into the SQL shell or a PHP myadmin or anything like that although I am gonna go into PHP myadmin just to show you and xampp comes with PHP myadmin as well so

it's another reason I like to use it whoops PHP myadmin okay so let's see here it is right here sim fart and there's nothing in it it's

completely blank but it did create the database for us now the next thing we want to do is create an entity for our articles which is basically a model so I'm going to I'm just going to open up a

new terminal here and we're gonna go PHP bin slash console and we want to use make you want to use the make command and we want to do : entity and we want to call it article capital a all right

so we got success and now if we go over here and the entity folder you'll see we have an article dot PHP file and by default it brings in doctrine brings in our as ORM and then it creates a class

and it has feet and ID field okay which is of type integer now what we want to do is just add a couple more fields so it says add your own fields we're going to go ahead and put in a comment here

and we want to specify at ORM slash column and then the type that we want is going to be text so this isn't going to be a text field I'm going to save public title

actually let's make that private so private title so this is just say it's telling it that we want a title field in our database and it's going to be type of text or varchar' rather all right so

that's that now we also want a body so we want to title in a body so let's again do Oh our M ORM slash column and this one is going to be type and we're gonna set it to text okay actually the

title we can actually give per like length parameters so I'm gonna say length equals 100 for the title and then this we want to be private body and that those are the only fields that I'm gonna

add so now we can save this and we want to go down to our terminal and we want to do two things we want to create our schema and then we want to create our migration which is actually going or

actually run the migration which is going to add our table it's going to create an article table with these fields so to do that we do PHP bin slash console and to create the schema we're

going to use doctrine so we say doctrine colon migrations colon diff di FF so you can see generated new bite migration class now we want to run the migration so we do that with our console

and we do doctrine : migrations and then : migrate and let's see so we'll just say yes and there we go the migration is finished so now I'm just gonna go to or is it PHP myadmin

and let's reload our database and there we go now it also creates a table for the migrations and then you can see we have our article table if I click that and go to structure we have an ID which

is a primary key auto increment and then a title and a body okay we didn't have to go in and create this manually we can simply do it from our console and then if we wanted to let's say let's say we

changed our mind we wanted to add something else like I don't know an author or something like that we could change this file and then just run those two commands again run the diff and then

run the migrate and it'll it'll change it for us so next thing I want to do is just add a couple getters and setters now this is so that we can that we can set field get fields and set fields

within our controller so right below private body I'm going to create let's just put a comment here so I'm gonna create a function called get ID and we're going to return this ID

all right we're not gonna have to set the ID at all so I'm not going to create a setup for that but I am for the title on the body actually I don't need a comment let's just say public function

get title and we're just going to return this title now the reason we're doing this is because we set these properties to private which means we can't access these outside of this class so we need

public methods or public functions to actually access them and set them and this is this has nothing to do with Symphony this is just basic Pete object-oriented PHP so let's also create

a setter for the title so we'll say set title and that's actually going to take in whatever we want to set it to so title and then we can simply do we're gonna set this title

equals two title okay so that's our setter and we're going to do the same thing for the body so we'll copy both of those paste that in let's change this to get body and we'll return this body and

then let's do set body and there we go so that should be all we need to do here let's save this and close it up so we have our entity set up or our model set up so now what I'm going to do

is I'm actually going to set up a method in our controller to to save to the database and we're not going to actually use this in the application it's just to kind of get some stuff in there and just

to show you how it works so let's create a route and we'll call this slash article slash save and let's see in here the first thing we need to do obviously is create a function so let's say public

function save and it's just gonna be a get request and that's one thing you don't want to do is have a get request actually update data on your server on your database so this is just just for

now we're not going to use it later on but to to use doctrine to save an article we're gonna have to use the create an entity manager so we're gonna say entity manager equals this

get doctrine so get doctrine and then it has a method called get manager okay so we want to do that okay so now we have the entity manager we want to create a variable called article we're gonna set

it to new article actually should have parenthesis now right right now it's not gonna know what this is it's we need to bring in the article entity so I'm gonna go up here and say use app slash entity

slash article like that okay so now let's go ahead and we'll take this article variable and we want to call remember we created set title and set body so we'll do article set title and

let's say article 1 say set body and we'll just say this is the body for article 1 and then once we do that we want to take our entitymanager

and we want to persist the data so we want to call persist and then just pass in the article okay once we do that we want to execute the query this what what persist does is it tells us that we want

to eventually save it to actually execute it we need to call flush so we want to call entitymanager flush okay so we take the dump here and then we actually flush it here and then

once we do that we want to I guess we're just going to return a response this isn't going to be actual part of our application it's just to get some data in there and show you how it works so

new response saved an article with the ID of and then we can concatenate on let's concatenate article and remember we have that get ID get ID and that should do it so let's go ahead

and save this file and then let's go to the route article slash save hopefully it works all right save saves an article of ID of one now if we go to our database and we don't even actually have

to go to like PHP myadmin or anything we can check it with the console if we want so and the only reason I keep deleting my consoles because I can't clear it for some reason ctrl L is not working clear

isn't working but we can do PHP bin console and we can use doctrine and we can say doctrine : SQL and then we can put in the query so we can say select all from article wait a minute

doctrine SQL is not defined oh it's not it's not doctrine SQL its doctrine query query SQL there we go so now you can see there's an ID of one the title of article one and a body of that so let's

go ahead and add one more so I'm just going to change this to article two change that change that to saved alright so let's go ahead and run this route again and now saves an article with the

idea of two and this time I'll go ahead and check it with PHP myadmin so if we go to browse there's our two articles alright so we now have these stored in the database now like I said

we're not gonna actually use this in our application because we know if someone goes to this route they can simply add an article we don't want that so let's um let's comment this out but keep it

here just for reference alright I'm actually gonna get rid of this so now that we have these articles in the database let's go ahead and replace this this hard-coded data with

the articles that are actually in the database so this is actually pretty easy we're just going to replace this hard-coded array with this get Doctrine and we're gonna call get repository so I

know some of this vocabulary is is strange but we want to do article class okay I want to get it from the article class or the article entity and we want to just call find all okay find all

we'll grab everything and then simply pass it in just like we're doing all right and then go to our view the index view and instead of just article right here it's gonna be article dot title so

let's save that let's go back to our home home page and now these are coming from the database now instead of doing articles slash one for these shows see how it goes to article one so does this

one we wanted to go to the ID of the article so we can replace this with article dots ID and then the first one you can see is one second one is two

so it's actually putting the correct ID in the link so let's make this route happen let's make it so that this shows the single article so we're gonna go under let's see we'll go back to our

controller let's go right under the index and let's create a row so this route is gonna be slash article slash and then we're gonna do curly braces ID so this is just a placeholder and we can

also name our routes so if we put a comma here we can say name equals and we'll say article show let's name this one up here as well so we'll say name article list alright

so for article show it's gonna be function or public function show and you can call these whatever you want I'm showing it's gonna take in an ID because we need to know which article so what

this ID this is gonna basically pop into here okay and then we can use that so just like we did above we're gonna use doctrine we're gonna create a variable called just article a single article and

say this get doctrine and again we won't get repository and we're gonna pass in article double colon class and instead of find all we want just find and we want to pass in the ID so we want to

find it by the ID and once we do that we want to render a view so we want to return this render and let's render articles slash and then it's gonna be called show dot HTML dot twig which we

don't have yet but we'll create and we want to pass along an array which has the single article like that alright so let's create that view or that template so inside articles will say new

file show dot HTML dot twig and in here let's actually copy what we have in index because we need to extend the base and all that but we don't need the yeah we don't need anything it'll just clear

out the whole body like that hand let's put an h1 with the article title and a paragraph with the article body and let's put an HR and then I just want to go back link so

we'll go back to slash like that alright let's try it out we'll reload we'll hit show and there we go article one hit show here and we get article two so that's how we can show single posts or

single articles good so hopefully you guys are getting the hang of this and you're understanding how to create your routes how to create your methods and your controller how to render your views

things like that how to fetch from the database using doctrine so I think what I want to do in the next video is create our form and we're gonna use the form help or we can actually generate the

form from our controller rather than put in the actual mark-up into a view and I'm going to show you how to do that and also show you how to submit it so that we can add an article from our

application so we'll do that next

In the second part of the Symfony 4 series we will implement the Doctrine ORM to create and interact with our MySQL database. We will add some records, fetch them and setup our routes/annotations

Code: Github Repo
https://github.com/bradtraversy/symphart

💖 Become a Patron: Show support & get perks!
http://www.patreon.com/traversymedia

Website & Udemy Courses
http://www.traversymedia.com

Follow Traversy Media:
http://www.facebook.com/traversymedia

http://www.instagram.com/traversymedia

The Developer Hangout: Public Discord Server
https://discord.gg/developers

    php symfony 4 symfony symfony 4 symfony 4 forms symfony 4 framework symfony doctrine symfony framework symfony mysql symfony php symfony tutorial symphony 4 up and running with symfony