Many To One: The King of Relationships
…we see a handy getParts() method.
But instead of setParts(), we've been gifted addPart() and
removePart() methods:
These will come in handy when you work with Foundry, the form system or if you're
building an API with Symfony's serializer.
In the constructor…
Part Entity
…we need a migration!
symfony console make:migration
Then migrate:
symfony console doctrine:migrations:migrate
In the last tutorial, we used a cool library called Foundry to quickly
create a bunch of dummy data. We're going to do the same for
StarshipPart. Step 1 …
Cascade Persist
…again:
Guess what? They work!
Behind the scenes, Foundry is calling addDroid() on each Starship for
each droid. And we just proved that addDroid() is back in action.
The creation of the StarshipDroid join entity is now hidden
from our entire codebase!
But, what if…
Single Table Inheritance
…because not all entities will have those properties, and need to be null
if this is the case.
Let's reload our fixtures! Run:
symfony console foundry:load-fixtures
Awesome, no changes needed for our fixtures.
To see what our starship table looks like, run:
…
Accessing Data on a ManyToMany
…separated string.
Now that we've got a getDroidNames() method, we can say
{{ ship.droidNames ?: 'none' }}.
We're done! Refresh... and enjoy the droid names on the homepage.
Next: let's use Foundry to set the ManyToMany relationship in the fixtures.
Another place Foundry shines!
Relating Resources
…new owner field to getDefaults() set to UserFactory::new().
I'm not going to go into the specifics of Foundry - and Foundry has great docs
on how to work with relationships - but this will create a new User each time
it creates a new DragonTreasure…
Creating a User Entity
…migrate
Sweet! Though, I think our new entity deserves some juicy data fixtures. Let's
use Foundry like we did for DragonTreasure. Start by running
php bin/console make:factory
to generate the factory for User.
Like before, in the src/Factory/ directory, we have…
Class Table Inheritance
…cargo_capacity.
The starship table has all the common properties, and the scout table has just an id
and sensor_range.
Let's load our fixtures to ensure everything still works.
symfony console foundry:load-fixtures
Looks good. Jump over to the app and refresh…
The Two Sides of a Relation: Owning vs Inverse
…Our favorite error pops up
starship_id cannot be null
Totally expected.
To demonstrate the owning vs inverse issue, add _real() to the end of $starship:
When you create an entity via foundry, it actually wraps it in a little gift
called a proxy object…
Emails Assertions in Functional Tests
…before, this
may look a tad different because I'm using some rocking helper libraries. zenstruck/foundry gives
us this ResetDatabase trait which wipes the database before each test. It also
gives us this Factories trait which lets us create database fixtures
in our tests…
Doctrine Inheritance and Relationships
… starshipParts is good. We don't need another property
so just hit enter to exit.
Next, create the Foundry factory for this entity.
symfony console make:factory
1 for all (and all for 1). Sweet, let's take a look at the generated code.
In…
Translatable Mapping
…when we switch to French on our site, we'll display
some mock French data, just like we currently have mock English data.
We need to create a factory using Foundry. So, run:
symfony console make:factory
Create a factory for the Translation entity: 0…
JSON Test Assertions & Seeding the Database
…the database is empty. To make our test real, we need data: we
need to seed the database with data at the start of the test.
Fortunately, Foundry makes that dead-simple. At the top, call
DragonTreasureFactory::createMany() and let's create 5 treasures. Now…
Resetting the Database
…log file instead:
logFile=var/log/deprecations.log.
Close that up. Now when we run the tests:
Clean and tidy! And the deprecations are still waiting for us in the log file.
Next: let's leverage Foundry Factories to make seeding our database an absolute
delight!
Writing an Integrational Test for Webhooks
…the test environment.
Our test is failing because we need to set up a test database. We
could do this manually using Doctrine console commands, but let's take
advantage of Zenstruck Foundry, which we already have installed.
In the test class, add use ResetDatabase…
Generating the API Token & Fixtures
… So let's add some API tokens to the database. At your
terminal, run
php ./bin/console make:factory
so we can generate a Foundry factory for ApiToken. Go check out the new class:
src/Factory/ApiTokenFactory.php. Down in getDefaults():
This looks mostly fine…
The Repository Test Helper
…Foundry comes with a nice
trick for this.
After we call the method, say LockDownFactory. Normally, we would call things like
create or createMany, but this also has a method on it named repository.
This returns an object from Foundry that wraps the repository - much…
App & Test Setup
…to use Symfony 5.1 and
API platform 2.5:
I also added Foundry to our system, which
is a new library that makes it really easy to create dummy data:
We're using Foundry inside of our data fixtures - so src/DataFixtures/AppFixtures.php -…
Simple Doctrine Data Fixtures
…stuff.
But for developing, I want a really rich set of data fixtures, like... maybe 25
mixes. We could add those by hand here... or even create a loop. But there's a
better way, via a library called "Foundry". Let's explore it next!
Hashing Plain Passwords & PasswordCredentials
…property... but then the password property
would stay null... and it would explode in the database because that column is
required.
So after Foundry has finished instantiating the object, we need to run some extra
code that reads the plainPassword and hashes it. We can…
x
201