1000 search results

Update Query & Rich vs Anemic Models

…whenever we need to save something in Doctrine, we need the entity manager. Add another argument: EntityManagerInterface $entityManager. Then, below, replace the dd($question) with $entityManager->flush(). Done! Seriously! Doctrine is smart enough to realize that the Question object already exists in the database and…

7:58
Custom Repository Class

…here. We'll see this idea of custom entity methods later. At the bottom, there's one more link: question.slug. Done! Doctrine makes it easy to query for data and Twig makes it easy to render. Go team! At the browser, refresh and... cool…

8:10
"5 Minutes Ago" Strings

…That's not going to work!". And... you're right: Object of class DateTime could not be converted to string We know that when we have a datetime type in Doctrine, it's stored in PHP as a DateTime object. That's nice because DateTime…

3:15
Fetching Data & The Repository

… Refresh and... woohoo! This gives us a Question object. Doctrine finds the matching row of data and uses that to populate an object, which is beautiful. The repository has a number of other methods on it. For example, findOneBy() returns a single object and findBy…

5:28
docker-compose Env Vars & Symfony

…For example, because this service is called database - we technically could have called it anything - the Symfony binary is already exposing an environment variable called DATABASE_URL: the exact environment variable that Doctrine is looking for. I'll show you exactly what I mean. First…

4:59
Migrate Password Hashing

…Got it! It's also kinda fun to see how this looks in the database. Find your terminal and run: php bin/console doctrine:query:sql 'SELECT email, password FROM user' Interesting: every hashed password starts with the same $2y thing. That's no accident…

9:46
Validation Auto-Mapping

…How the heck did that work? The system - validation auto-mapping - automatically adds sensible validation constraints based off of your Doctrine metadata. The Doctrine Column annotation has a nullable option and its default value is nullable=false: In other words, the title column is required in…

8:49
Spotting Heavy Object Instantiation

…to instantiate Doctrine's EntityManager object. I know, we don't often think about how much time or how much memory it takes to instantiate an object, but it can sometimes be a problem. The next function call is for the instantiation of Doctrine's…

6:06
Reordering the Files

…the usual: php bin/console make:migration and go to the src/Migrations directory so we can make sure it doesn't contain any surprises. Looks perfect! Close that and run: php bin/console doctrine:migrations:migrate Ok, the database is ready! For the frontend…

5:10
Priming cache.app

…production. Add framework, cache and app set to cache.adapter.redis: cache.adapter.redis is the id of a service that Symfony automatically makes available. You can also use cache.adapter.filesystem - which is the default - doctrine, apcu, memcached or create your own service. If…

7:00
Safe Migrations

…back a deploy before. But, let's update it to be safe: SET image = poster, and then ALTER TABLE to drop poster: This is a safe migration. First, try it locally: ./bin/console doctrine:migrations:migrate Perfect! And now... deploy! Right? No! Stop that…

7:10
Symfony Console Commands

…we only want to run if this is a development machine, because it resets the database. We'll talk about controlling that later. For this command, use hautelook_alice:doctrine:fixtures:load --no-interaction: Ok! The 3 commands are ready! Head back to the terminal…

5:38
Saving Relations

…passing the ID to the question property, we're setting the entire Question object onto the property. Doctrine will be smart enough to save these in the correct order: it'll save the question first, grab its new id, and use that to save the…

3:58
Relations in Foundry

…Notice that, in getDefaults(), we are not setting the question property. And so, if you spin over to your terminal and run: symfony console doctrine:fixtures:load ... we get our favorite error: question_id column cannot be null. To fix this, in AppFixtures, pass a…

4:41
Relation OrderBy & fetch=EXTRA_LAZY

…six answers, eight answers. But check out the web debug toolbar. Woh! We suddenly have a lot of queries. Click to open Doctrine's profiler. The first query is still for all of the question objects. But then, one-by-one it selects FROM answer…

7:22
Filtering to Return only Approved Answers

…doesn't contain any surprises It looks good: ALTER TABLE answer ADD status. Close that, spin back to your terminal and execute it: symfony console doctrine:migrations:migrate Because we have exactly three possible statuses, I'm going to add a constant for each one…

9:40
Collection Criteria for Custom Relation Queries

… Fortunately, no! These Doctrine Collection objects have a few tricks up their sleeves, including a special "criteria" system for just this situation. It allows us to describe how we want to filter the answers and then it uses that when it queries! Remove the filter…

6:33
Doing Crazy things with Foundry & Fixtures

…database. Instead, the new QuestionTag object will be related to whatever Question is currently being created. Foundry does this by reading the Doctrine relationship and smartly overriding the question attribute on QuestionTagFactory. But... I did say that there was a problem with this. And... we…

8:59
JOINing Across Multiple Relationships

…question_tag data that's between Question and Tag. Doing that is legal in SQL... but it messes up how Doctrine creates the objects, so it doesn't allow it. The solution is easy enough: select both. You can actually pass an array to addSelect…

3:52
Property Metadata

… Integers? Aliens? API Platform gets metadata about each property from many different places, like by reading Doctrine metadata, PHPDoc, looking at the return types of getter methods, looking at the argument type-hint on setters, PHP 7.4 property types and more. What's really…

6:20