WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.046 --> 00:00:04.386 align:middle
Ok back to work, let's finish
up this MovieController.

00:00:04.746 --> 00:00:08.246 align:middle
Command+O, MovieController, and
now we've got that file open.

00:00:09.066 --> 00:00:11.626 align:middle
We need to save the POST'ed
movie to the database.

00:00:11.706 --> 00:00:17.166 align:middle
Start with getting our entity manager with
$em = $this-&gt;getDoctrine()-&gt;getManager();.

00:00:19.276 --> 00:00:26.726 align:middle
Then $em-&gt;persist($movie);, and
$em-&gt;flush();: Nice and simple!

00:00:26.726 --> 00:00:30.126 align:middle
While we're here, I'll copy the
getDoctrine() line and paste it

00:00:30.126 --> 00:00:32.206 align:middle
into listAction() so we can make that work.

00:00:35.036 --> 00:00:38.516 align:middle
Here we'll query for the movies
with $em -&gt;getRepository()

00:00:38.516 --> 00:00:40.276 align:middle
and the first cool thing here is that...

00:00:40.476 --> 00:00:41.506 align:middle
you guessed it!

00:00:41.506 --> 00:00:42.916 align:middle
We get autocomplete on this.

00:00:43.476 --> 00:00:48.946 align:middle
Type the entity name, select the right
one and hit tab: The second cool thing is

00:00:48.946 --> 00:00:51.416 align:middle
that it autocompletes the
methods on the repository,

00:00:51.416 --> 00:00:53.836 align:middle
and this includes any custom
methods you have inside of there.

00:00:54.606 --> 00:00:56.106 align:middle
We'll use the normal findAll();.

00:00:57.286 --> 00:01:02.276 align:middle
Down below, we'll return
$this-&gt;render('movies/list.html.twig');

00:01:02.346 --> 00:01:09.176 align:middle
which doesn't exist yet, with an array
and we'll pass it movies: Awesome!

00:01:09.236 --> 00:01:16.606 align:middle
Let's go create that file: list.html.twig.

00:01:17.186 --> 00:01:20.136 align:middle
And to save some time, I'll just
paste in some code for this template:

00:01:20.486 --> 00:01:24.396 align:middle
it's really straight forward: The
only interesting thing here is

00:01:24.396 --> 00:01:26.946 align:middle
that you do get autocomplete
on the different properties

00:01:26.946 --> 00:01:29.486 align:middle
for movie, Which really is pretty amazing.

00:01:30.646 --> 00:01:36.376 align:middle
The reason that works is because PHPStorm knows
that the MovieRepository returns Movie objects,

00:01:36.406 --> 00:01:38.676 align:middle
so that cascades all the way down into Twig.

00:01:39.766 --> 00:01:44.856 align:middle
So as long as you have good PHPDoc that
says what types of objects functions return,

00:01:45.066 --> 00:01:47.896 align:middle
that should connect down to how
things auto-complete in Twig.

00:01:49.376 --> 00:01:50.466 align:middle
Let's try this whole thing out.

00:01:51.336 --> 00:01:53.496 align:middle
But before we do that we
need to setup our database.

00:01:54.216 --> 00:01:59.146 align:middle
Head over to parameters.yml by using our
shortcut command+shift+O to open it up.

00:02:02.236 --> 00:02:06.246 align:middle
Everything seems to be in order here, so
let's just change our database to be phpstorm.

00:02:07.336 --> 00:02:10.146 align:middle
And just when you thought I couldn't get
any lazier with my shortcuts I'm going

00:02:10.146 --> 00:02:12.326 align:middle
to use the built in terminal from PHPStorm.

00:02:15.406 --> 00:02:21.166 align:middle
This drops me right into the correct directory
so I can run: Hide this little terminal,

00:02:21.886 --> 00:02:24.516 align:middle
head back and refresh our browser.

00:02:24.516 --> 00:02:25.296 align:middle
And we see...

00:02:25.296 --> 00:02:34.016 align:middle
oops we have a small rendering problem,
which is probably in our form template.

00:02:34.016 --> 00:02:42.936 align:middle
Back to PHPstorm, command+shift+O
and search _form.html.twig.

00:02:42.936 --> 00:02:50.746 align:middle
And yep there it is, change form-control to
form-group - that's the correct Bootstrap class.

00:02:50.746 --> 00:02:53.086 align:middle
Refresh again.

00:02:54.296 --> 00:02:55.606 align:middle
That looks way better.

00:02:55.606 --> 00:02:57.756 align:middle
Now we can fill out the form.

00:02:58.266 --> 00:03:03.346 align:middle
Let's see, which one of our favorite Samuel
L Jackson movies should we choose first -

00:03:03.346 --> 00:03:05.346 align:middle
so many choices.

00:03:06.006 --> 00:03:10.346 align:middle
Let's just go with the obvious best: "Snakes
on a Plane", with our character, Neville Flynn.

00:03:10.986 --> 00:03:14.906 align:middle
Neville is the main character, and
clearly this film was a 10 out of 10.

00:03:14.906 --> 00:03:16.816 align:middle
I mean come on, this is Sam at his best!

00:03:17.456 --> 00:03:23.736 align:middle
And I think everyone remembers the fateful
day this film was released: August 18, 2006.

00:03:23.796 --> 00:03:25.646 align:middle
Let's save this small piece of history.

00:03:26.236 --> 00:03:27.066 align:middle
Beautiful!

00:03:27.416 --> 00:03:28.636 align:middle
Now for a little refactoring.

00:03:30.436 --> 00:03:33.836 align:middle
First, we have $em =
$this-&gt;getDoctrine()-&gt;getManager();

00:03:33.836 --> 00:03:34.716 align:middle
in a couple of places.

00:03:34.716 --> 00:03:37.806 align:middle
So I'm going to refactor that: select that line

00:03:37.806 --> 00:03:40.826 align:middle
and we'll see another important
shortcut, control+t.

00:03:40.866 --> 00:03:42.766 align:middle
This is the refactor shortcut.

00:03:42.836 --> 00:03:45.846 align:middle
If you ever forget it, just go
to the Refactor menu at the top

00:03:45.846 --> 00:03:48.316 align:middle
and select "Refactor This" from the list.

00:03:49.446 --> 00:03:51.506 align:middle
We've got our line selected, press control+t.

00:03:52.776 --> 00:03:55.636 align:middle
There's a lot of options in here,
but we want the one called method.

00:03:55.636 --> 00:04:01.486 align:middle
In the "Extract Method" form,
add getEm as the name,

00:04:01.806 --> 00:04:05.346 align:middle
make sure that the private radio
button is selected and refactor!

00:04:06.156 --> 00:04:12.386 align:middle
Boom! It puts this on the bottom and it
changes the original code to $this-&gt;getEm();.

00:04:13.336 --> 00:04:17.776 align:middle
Copy this line, pressing control+c will select
the whole line and we'll paste it right up here:

00:04:22.276 --> 00:04:25.106 align:middle
This shortcut is going to be useful
in more than just this controller,

00:04:25.186 --> 00:04:28.806 align:middle
so this is the perfect time to create
a BaseController for my whole project.

00:04:29.296 --> 00:04:33.596 align:middle
Click the Controller directory at the top
of your IDE, and from there press command+n,

00:04:33.896 --> 00:04:37.896 align:middle
select Controller from the menu
and name it BaseController.

00:04:39.306 --> 00:04:44.016 align:middle
To start, remove that public function
and add abstract before the class:

00:04:44.516 --> 00:04:46.606 align:middle
This is now the spot for our own shortcuts.

00:04:47.906 --> 00:04:50.936 align:middle
In MovieController update
it to extend BaseController.

00:04:53.446 --> 00:04:56.276 align:middle
This use statement is showing
up in a darker gray to indicate

00:04:56.276 --> 00:05:00.676 align:middle
that it isn't being used any more and
the same for the Response use statement.

00:05:00.676 --> 00:05:04.566 align:middle
Delete both of those: With
the BaseController in action,

00:05:04.636 --> 00:05:07.296 align:middle
let's refactor getEm() into the BaseController.

00:05:08.616 --> 00:05:12.126 align:middle
Select the method, one cool
way to do that is hit option+up

00:05:12.256 --> 00:05:14.696 align:middle
which will select larger and larger contexts.

00:05:17.006 --> 00:05:17.996 align:middle
Now hit control+t.

00:05:19.446 --> 00:05:23.256 align:middle
Select "Pull Members Up" from the menu,
which will pull them up to the parent class.

00:05:23.836 --> 00:05:28.436 align:middle
It already recognizes that BaseController is the
destination, and at the bottom it's warning us

00:05:28.466 --> 00:05:31.886 align:middle
that the access will be changed from
private to protected, which is awesome!

00:05:32.856 --> 00:05:37.886 align:middle
Hit refactor and we can see it's gone from
this file, because now it lives inside

00:05:37.886 --> 00:05:42.166 align:middle
of BaseController: That there is a
really fast way to refactor things!

00:05:43.506 --> 00:05:48.266 align:middle
Now that I have this here I'm realizing that
getEm() is not that great of a name choice.

00:05:48.376 --> 00:05:53.416 align:middle
So back to command+t, select rename from
the menu and change it to getEntityManager

00:05:53.416 --> 00:05:54.656 align:middle
to make my code a little clearer.

00:05:56.206 --> 00:05:59.206 align:middle
When we do that I get a summary
down here of all the spots

00:05:59.206 --> 00:06:02.346 align:middle
in our project where PHPStorm sees getEm().

00:06:02.346 --> 00:06:07.146 align:middle
Click the "Do Refactor" button to
rename this and all the other spots

00:06:07.146 --> 00:06:10.436 align:middle
in the code that need updating: Sah-weet!

00:06:10.766 --> 00:06:14.766 align:middle
There are a lot of other things you can
do with the refactor feature in PHPStorm.

00:06:16.496 --> 00:06:19.066 align:middle
For example, we can extract
this out to a variable.

00:06:19.596 --> 00:06:22.396 align:middle
This could add a level of
clarity to what things are.

00:06:24.676 --> 00:06:27.816 align:middle
Or, say you get into a spot where you messed
up your formatting in some terrible way.

00:06:27.846 --> 00:06:31.676 align:middle
Oof that looks just awful.

00:06:31.676 --> 00:06:32.126 align:middle
Make it stop!

00:06:32.126 --> 00:06:36.896 align:middle
At any point, you can go up to the code
menu at the top and select reformat code,

00:06:37.326 --> 00:06:41.326 align:middle
which is also command+option+L and
it will tidy that back up for you.

00:06:41.926 --> 00:06:47.036 align:middle
So let's hit command+A to select all the code
I want to reformat, then command+option+L

00:06:47.446 --> 00:06:49.466 align:middle
and now everything is back into place based

00:06:49.466 --> 00:06:52.216 align:middle
on our coding standards, which
you can of course control.

