WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:00.976 --> 00:00:04.646 align:middle
We have a Movie class, but its
missing all of its annotations!

00:00:04.906 --> 00:00:08.266 align:middle
Yikes! I really don't feel
like typing all of that.

00:00:08.266 --> 00:00:12.636 align:middle
Let me introduce you to another
really important shortcut: generate.

00:00:12.636 --> 00:00:15.346 align:middle
You can get to it with command+n.

00:00:16.216 --> 00:00:18.626 align:middle
Or just click the code menu at
the top, and select generate.

00:00:19.996 --> 00:00:26.126 align:middle
Either way in this class, go down to "Generate
ORM Class": and just like all menus in PhpStorm,

00:00:26.126 --> 00:00:27.746 align:middle
you can just start typing to search for it.

00:00:28.686 --> 00:00:33.846 align:middle
Sweet - this automatically added all the
Entity annotation stuff on top for us.

00:00:34.226 --> 00:00:40.886 align:middle
But it looks different: usually we'd
just have the @ORM\Entity stuff on top,

00:00:41.326 --> 00:00:44.656 align:middle
and it added it this way because we
don't have the use statement yet.

00:00:45.516 --> 00:00:46.766 align:middle
There's a simple solution to that.

00:00:47.066 --> 00:00:54.296 align:middle
Just copy the full Entity annotation, say
use, paste that and then delete the last part

00:00:54.396 --> 00:00:59.466 align:middle
and instead stay as ORM;: That's the
standard use statement that you see

00:00:59.466 --> 00:01:01.256 align:middle
at the top of all Doctrine entities.

00:01:01.346 --> 00:01:05.306 align:middle
Let's get rid of these lines here,
then get back inside of the class

00:01:05.306 --> 00:01:08.266 align:middle
because context] is important with command+n.

00:01:08.326 --> 00:01:14.036 align:middle
Hit command n and search for 'ORM' and
select "Generate ORM Class": Perfect!

00:01:14.036 --> 00:01:21.076 align:middle
Let's add our properties, private $id;,
private $title; for the movie title,

00:01:21.846 --> 00:01:24.976 align:middle
private $samsCharacterName for Sam's character,

00:01:25.486 --> 00:01:31.916 align:middle
private $rating to give the movie appearance a
rating, private $isMainCharacter so we can see

00:01:31.956 --> 00:01:37.096 align:middle
if Sam was the lead in that movie,
and lastly private $releasedAt

00:01:37.456 --> 00:01:39.456 align:middle
which will tell us exactly
when the movie came out.

00:01:40.446 --> 00:01:46.076 align:middle
Easy! The warning here is that each of these is
an unused private field, which is true so far.

00:01:46.306 --> 00:01:49.116 align:middle
But good to know, in the future
that could just be extra code.

00:01:49.116 --> 00:01:54.716 align:middle
Now that we have these we can
go back to command n, search ORM

00:01:54.716 --> 00:01:57.146 align:middle
and select "Generate ORM annotations".

00:01:57.636 --> 00:02:02.156 align:middle
Select all of the fields that are
in the menu and hit ok: Awesome!

00:02:02.356 --> 00:02:04.476 align:middle
Each field has all its annotations!

00:02:04.736 --> 00:02:09.956 align:middle
Even better than that, it recognizes that id is
our primary key so it set that up and it noticed

00:02:09.956 --> 00:02:14.096 align:middle
that $isMainCharacter is probably a boolean,
because of the 'is' in the beginning.

00:02:14.826 --> 00:02:17.496 align:middle
And it also saw that $releasedAt is a datetime.

00:02:19.406 --> 00:02:22.326 align:middle
This isn't perfect, I would prefer
$releasedAt to just be a date,

00:02:24.206 --> 00:02:27.046 align:middle
and rating up here is not
a string, but an integer:

00:02:28.036 --> 00:02:31.456 align:middle
But I do love getting autocomplete
on all of those different types.

00:02:32.316 --> 00:02:35.646 align:middle
Pressing control+space gives you a list of
all the different types you have access to.

00:02:39.186 --> 00:02:44.106 align:middle
Ok, let's give $isMainCharacter a default
value just in case it's ever not set.

00:02:44.186 --> 00:02:49.766 align:middle
We'll make $releasedAt optional since a movie
might not be released yet: set nullable=true:

00:02:50.226 --> 00:02:54.376 align:middle
more autocomplete: Up here
for $samsCharacterName,

00:02:54.666 --> 00:02:58.496 align:middle
well this probably won't be too long so
we can give it a length of 100 instead

00:02:58.496 --> 00:03:03.796 align:middle
of the default 255: Alright,
this is all looking really nice.

00:03:04.906 --> 00:03:08.466 align:middle
At this point we just have private
properties so we need our getters and setters.

00:03:08.466 --> 00:03:11.096 align:middle
Back to generate!

00:03:11.536 --> 00:03:17.796 align:middle
Use our favorite shortcut, command+n, select
getters and then $id: Then back to generate

00:03:17.796 --> 00:03:20.676 align:middle
and select getters and setters
and select everything else.

00:03:21.666 --> 00:03:25.646 align:middle
Before I finish this I want to pause and
say that you don't necessarily need a getter

00:03:25.686 --> 00:03:27.826 align:middle
and setter for every field in Doctrine.

00:03:28.206 --> 00:03:30.286 align:middle
Sometimes you might want
to wait to add the getters

00:03:30.286 --> 00:03:32.246 align:middle
and setters until you actually need them.

00:03:32.986 --> 00:03:35.746 align:middle
Then when the need arises you have
these awesome shortcuts available.

00:03:37.196 --> 00:03:40.606 align:middle
If I press command+, to get into
preferences, you'll find that the templates

00:03:40.606 --> 00:03:42.216 align:middle
that generate these methods are editable.

00:03:42.746 --> 00:03:44.576 align:middle
Search for "templates", and you'll see the area

00:03:44.576 --> 00:03:47.166 align:middle
where you can modify the
getter and setter templates.

00:03:48.236 --> 00:03:53.016 align:middle
I've already done this: usually they generate
with some PHPDoc, which to me is kind

00:03:53.016 --> 00:03:56.686 align:middle
of meaningless, so I've already
removed it for nice clean rendering.

00:03:59.566 --> 00:04:00.336 align:middle
One last thing here!

00:04:00.826 --> 00:04:02.836 align:middle
This entity needs a repository.

00:04:03.956 --> 00:04:06.866 align:middle
Back to the action shortcut, which is...

00:04:07.056 --> 00:04:07.746 align:middle
alt+enter!

00:04:08.816 --> 00:04:13.006 align:middle
This opens up a menu to add the doctrine
repository, which as you may have guessed,

00:04:13.006 --> 00:04:18.956 align:middle
adds a repository class here: In my case I
prefer to have these in a Repository directory.

00:04:19.366 --> 00:04:22.406 align:middle
So that's nice that it helped me
create that, but I'll move it manually.

00:04:25.756 --> 00:04:33.436 align:middle
A quick copy and paste will do that, then
update the namespace to end with Repository:

00:04:35.236 --> 00:04:38.006 align:middle
Even though I had to move that
manually, what's really cool is

00:04:38.006 --> 00:04:39.766 align:middle
that it's highlighting and saying "Yo!

00:04:39.916 --> 00:04:41.516 align:middle
Your repository Class is messed up!

00:04:41.866 --> 00:04:45.376 align:middle
You can't use the short class name because
now it's in a different namespace."

00:04:45.486 --> 00:04:50.176 align:middle
I can delete what's there and type
MovieRepository and I get autocomplete

00:04:50.176 --> 00:04:54.636 align:middle
on the entire name: And there's more
autocompleting goodness I won't show here

00:04:54.876 --> 00:04:57.896 align:middle
for when you're building relationships
and using the Query Builder.

