[11/38] - Doctrine Fixtures Tutorial - Part 2
22K views
Feb 14, 2023
https://codereviewvideos.com/course/let-s-build-a-wallpaper-website-in-symfony-3/video/doctrine-fixtures-part-2-relating-wallpapers-with-categories - full code and course With our Wallpaper and Category entities in place we can go ahead and create some starting data for each. Beware a few gotchas which we cover in this video.
View Video Transcript
0:00
Hi I'm ChristianCode review videos.com and in this video we're continuing on with our fixtures implementation
0:10
Towards the end of the previous video we were looking at how our data is related from a database perspective
0:16
in our case MySQL or MySQL and now we're going to continue our implementation from the perspective of doctrine entities
0:23
Okay so knowing this what we can do is duplicate our load wallpaper data fixture and we can stay instead, let's call it
0:29
this load category data. Yes, I want to add that, making sure that our file name matches up
0:36
with our class name. We won't need wallpaper, so we'll get rid of that. In fact, we'll get rid of all
0:40
that. We'll say category here. And category is going to be a new category, setting the name. In this case
0:47
abstract. And so when we load this now, when we reload these fixtures, we should expect to see a
0:53
category and also a wallpaper. So let's do that. PHP bin console, doctrine fixtures load
1:00
Data will be purged. Now things get interesting. We're going to say yes. We don't really care about that data at this point
1:06
If we look inside our database, we can see abstract background pink is there as expected
1:11
Category 1, that's the new thing. But notice the ID here is now number 2
1:16
So before we purged the database, this row already existed and it had the idea of 1
1:21
And then we ran the fixtures again just now. And it's still there, but now it's got the idea of 2
1:25
So it been deleted and recreated just to be absolutely clear but because it using the auto incrementing value it now ID number two So that presents a bit of a problem even though you may not have thought of this one just yet
1:37
if you're brand new to fixtures, but it means that we can't rely on those ID fields
1:41
being always known, in other words. We can't suddenly create ourselves a query inside our code
1:48
For our wallpaper, we couldn't have like a query here that says, you know, EM, grab from repository
1:54
for categories, find me by ID 1. or whatever, because it might not be ID1
1:58
Now, of course, this is a solved problem, and the solution to this problem is to create references
2:03
and a reference is essentially a way for us to assign objects from fixtures into strings that we can then pull in elsewhere
2:10
So in our case, what we're trying to do is have a wallpaper, in this case, it's an abstract wallpaper
2:15
We want to associate it with the abstract category. If it was a summer wallpaper, we'd want to associate it with the summer category and whatnot
2:22
But again, this creates another problem, And this other problem is that in order for us to be able to reference a category from our wallpaper, we know that that category has to already exist
2:33
But as of yet, we have no way of controlling which fixtures are loaded first or which fixtures come before other fixtures when we run that doctrine fixtures load command
2:42
It just does whatever comes first. I'm guessing alphabetically. I've not tested that, but I had a guess it would be alphabetically
2:49
And it would be a real shame if we have to name our fixtures in such a weird way
2:53
But of course we don't. Instead what we need to do is go into our fixtures and we going to extend abstract fixture and implement ordered fixture interface It means we can get rid of fixture interface there And again because we implementing this interface I going to do a command N on the Mac to implement a method which is to get order
3:12
And all this needs to do is return an integer value. The lower the value, the sooner that that fixture gets loaded
3:18
So in this case, I'm going to use hundreds. And the reason I use hundreds, just to be clear, category would be 100
3:23
Wallpaper would be 200, because the lower the number, the sooner that fixture gets loaded
3:27
loaded. So I use hundreds because a little later on, if you've just used like
3:31
1, 2, 3, 4, whatever, then you create a new entity and it messes up
3:36
the order somehow and you end up having to go through every single one and add them
3:40
on like by 1 or whatever. There's a little bit of a pain whereas if you've used hundreds or
3:43
thousands, then you can just go through and set 1 to like 150 or
3:47
175. It just gives you that little bit of extra granularity or freedom
3:51
I need to make sure that I do the same thing inside load wallpaper data as well
3:55
So again, extends abstract fixture. implements order fixture interface. Command then, implement the method, get order, return 200
4:05
That just ensue us that inside our wallpaper, we're always going to have category data to have been created
4:10
Just to prove that, let's reload our fixtures now. Yes, we don't care about that
4:14
And you can see our orderings are now applied on the console output
4:18
So we'd like to be able to, inside our load wallpaper data fixture, set the category
4:23
We don't have a category as of yet, but we know that the category data at this point will have been created
4:27
So how do we get access to it Well after our category object has been created we can do a this add reference we need to give it a name The name can be anything as long as it unique So we shall use category Dot abstract and then we need to give the object that we want to reference
4:43
So we'll say category is the object that we want to reference. So we can see there, basically
4:47
we're adding a reference with some unique string. I'm using a sort of a dot notation, just to say
4:53
you know, category dot abstract, category dot summer, category dot winter, whatever, just makes it that little
4:58
bit easier when you come to reference them a little later on, which we're going to do straight
5:01
away actually. In here, when we set the category now, we can just simply this, get the reference
5:07
by that string. And there we go. When we reload these fixtures, we should find that these are
5:14
related. So you can see category ID3. This is now on four, and as I say, it's because we keep
5:20
auto-incrementing every time we reload the fixtures. But three means abstract, and three is the category
5:25
that we're related to. So depending on which way you're doing this
5:29
depends on whether you've got a bunch of typing ahead of you or not. If you're using my code base, then I'm going to have done all this for you
5:35
And if you look inside the show notes, I would just suggest do a few yourself
5:39
just to get your fingers used to doing this. But if you don't want to do all 202 images
5:44
then by all means just pull out the fixtures from the repository. Otherwise, if you're using your own images, unfortunately
5:50
this is a little bit of a long-winded manual process. So maybe don't do them all
5:54
It's up to you. In the very next video, we're going to move on to creating ourselves an admin panel
5:58
And for that, we're going to use the easy admin bundle
#Data Management
#Enterprise Technology
#Public Records
#Reference