API Pagination Done Easily
…m just trying to keep
things simple.
If I were doing this in a real project, I'd probably use a library called
Pagerfanta. It helps you paginate
and has built-in adapters already for Doctrine, and Elastic Search. You can
give it things like…
Customizing the Contentful Slugger
…can run:
symfony console doctrine:migrations:migrate current-1
That will reverse the most recent migration, causing the contentful and route
tables to be dropped. Put them back with:
symfony console doctrine:migrations:migrate
Re-sync the content again:
And now check the routes:
Yes…
Content Browser: Returning the Items
…web debug toolbar, we have a 400 error.
Dang. When we open that up, we see:
Value loader for doctrine_recipe value type does not exist.
There's just one final piece we need: A very simple class called the "value
loader". That's next.
Hello Layouts+ Setup!
…putting into them. We'll
see all of that in the Layouts admin section in a minute. To add the needed tables,
scroll up and copy this nifty doctrine:migrations:migrate line.
This is kind of cool. The layouts packages comes with migrations... and this…
Migrating framework Config
…Great! Delete the validation line!
What's really interesting is that enable_annotations is set to true because
it detected that we have the Doctrine annotations package installed. This is the
flow with Flex: install a package and you're done.
Ok! It might not…
Removing Items from a Collection
…the $cheeseListings
property, add orphanRemoval=true.
This means, if any of the CheeseListings in this array suddenly... are not
in this array, Doctrine will delete them. Just, realize that if you try to
reassign a CheeseListing to another User, it will still delete that
CheeseListing. So…
Creating the User Entity
…make:migration
Move over... and double-check the SQL: CREATE TABLE user - looks good!
Run it with:
php bin/console doctrine:migration:migrate
Perfect! We have a gorgeous new Doctrine entity... but as far as API Platform
is concerned, we still only have one API…
Serialization Groups
…false.
By the way, if you're using Symfony 4.3, instead of a Doctrine error, you may
have gotten a validation error. That's due to a new feature where Doctrine database
constraints can automatically be used to add validation. So, if you see…
@SerializedName & Constructor Args
…Symfony 4.3, you may already see a validation error!
That's because of a new feature that can automatically convert your database
rules - the fact that we've told Doctrine that title is required in the
database - into validation rules. Fun fact, this feature…
Validating Who/When Can Publish
…new data has been
deserialized into it. The failure is from our first test case: we currently
allow this item to be published, even though the description is short. Next,
let's prevent this. How? It's back to our "original data" trick from Doctrine.
Custom Resource GET Item
…Next, let's talk about
pagination. Because if we go back to /api/daily-stats.jsonld and refresh...
there are a lot of items here. Since we're not using Doctrine, we no longer
get pagination for free. If we need it, we have to…
Custom Resource PUT Operation
…But what if the built-in filters aren't enough? What if we need to add some
custom filtering logic? Let's do that next by first creating a custom filter
for a Doctrine entity and later creating a custom filter for our DailyStats
resource.
Building really fast applications (Tobias Nyholm)
…they're only heavy in when you actually using them. Makes sense. Lovely. So this is cool and all. Let's move on to doctrine. Look at this five lines of code. It's a form. For yourself, think about potential issues with this code…
Filtering Related Collections
…method:
public function getPublishedCheeseListings() that will also return a Collection.
Inside, return $this->cheeseListings->filter(), which is a method on Doctrine's
collection object. Pass this a callback function(){} with a single CheeseListing
argument. All that function needs is return $cheeseListing->getIsPublished().
If you're…
Logging in Inside the Test
…ids. Try this:
$em = self::$container - a parent class sets the container on this nice property -
->get() and the service id. Use doctrine then say ->getManager().
You can also use the type-hint you use for autowiring as the service id. In
other words…
Leverage the power of Symfony components within ApiPlatform
…composer require api
is in fact, um, launching a Flex recipe that will install API
Platform, but also the doctrine-bundle, CORS support - Cross Origin Resource
Management - and everything you need to basically have an API started. So these
three steps, and I'm done…
When Testing Makes No Sense
…you
TDD everything? Yes. I was really interested because I found TDD to be very
wrong for Symfony, the Symfony part, like using the Forms, using the Doctrine
and stuff like that. And I was wondering, okay, how do you do that? Oh, we
don…
x
1000+