Chapters
19 Chapters
|
2:14:20
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
04.
Fetching Relations
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
The course is built on Symfony 4, but the principles still apply perfectly to Symfony 5 - not a lot has changed in the world of relations!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knplabs/knp-paginator-bundle": "^2.7", // v2.7.2
"knplabs/knp-time-bundle": "^1.8", // 1.8.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"sensio/framework-extra-bundle": "^5.1", // v5.1.4
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.0.4
"symfony/console": "^4.0", // v4.0.14
"symfony/flex": "^1.0", // v1.21.6
"symfony/framework-bundle": "^4.0", // v4.0.14
"symfony/lts": "^4@dev", // dev-master
"symfony/twig-bundle": "^4.0", // v4.0.4
"symfony/web-server-bundle": "^4.0", // v4.0.4
"symfony/yaml": "^4.0", // v4.0.14
"twig/extensions": "^1.5" // v1.5.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.0.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.4
"fzaninotto/faker": "^1.7", // v1.7.1
"symfony/debug-bundle": "^3.3|^4.0", // v4.0.4
"symfony/dotenv": "^4.0", // v4.0.14
"symfony/maker-bundle": "^1.0", // v1.4.0
"symfony/monolog-bundle": "^3.0", // v3.1.2
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.0.4
"symfony/stopwatch": "^3.3|^4.0", // v4.0.4
"symfony/var-dumper": "^3.3|^4.0", // v4.0.4
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.0.4
}
}
44 Comments
Offtopic: I want to chain objects. I got OneToMany relations.
I want to go from Object1->Object2->Object3.
But when I dd(Object1) it says that Object3 is: +__isInitialized__: false
And other columns of Object3 are null, while in the database they have values.
How do I fix this?
Hey @Farry
It's because Entities are proxied this allows to delay running sql query and the data will be loaded when you try to access this data. But if you want to see all data you can add
fetch="EAGER"to the@ORM\OneToMany()annotation. It may cause performance issues.Cheers!
So there is no valid way of doing this without risk of performance issues? I don't understand why this problem is caused specificly here, while chaining objects is a normal thing right?
Lets say you want user images and you only have the userID in the url. Then you write something like this:
$user->getPosts()->getImages();
On other places on my website, it works just fine. So I cant understand why I am getting this error on a specific part which is about machines in the factory.
$machine->getProduct()->getControlPlan();
Its not showing the controlplan for some reason.
oh that is tricky! You can try to make custom queries with QueryBuilder using joins and select all data you need explicitly. Than data you not selected will be queried lazyly, but everything mentioned in
->select()will be abvailableThis will allow you to bypass perrformance issues, but it really depends on you project and amount of data you trying select
Cheers!
Off topic. I get the following error and I dont understand how to fix it. Last thing I remember I did was updating composer.
Uncaught PHP Exception Doctrine\Common\Annotations\AnnotationException: "[Semantical Error] The annotation "@Gedmo\Mapping\Annotation\Slug" in property App\Entity\Controlplan\CPControlplan::$id was never imported. Did you maybe forget to add a "use" statement for this annotation?" at C:\xampp\htdocs\manapp\manufacturing-application\vendor\doctrine\annotations\lib\Doctrine\Common\Annotations\AnnotationException.php line 39
Hey @Farry7!
I'm not sure why a composer update would cause this, but on a high level, this error means:
However, the fact that it lists the entire annotation class name - Gedmo\Mapping\Annotation\Slug - (and it's correct), leads me to think that the
usestatement IS there. In that case, it simply can't find this class in your project! Did you somehow uninstall the doctrine-extensions library (this class comes from https://github.com/doctrine-extensions/DoctrineExtensions, which is a dependency of the stof doctrine extensions bundle)?Cheers!
I added 'use Timestampable' on my Entity. Then I did make:migration. But it says it's already in sync. I thought it would make a new migration file which adds 'date_added' and 'date_modified' in the table on the database. How do I do this?
Hey Farry,
Which trait did you use? There is one specific for entities, I believe it's called "TimestampableEntity". Give it a try and let me know
Cheers!
Thanks, the migration worked. But now when I am adding an input to the database, it gives an error saying that:
"An exception occurred while executing 'INSERT INTO measurement (measurement, created_at, updated_at, measurement_meetstaat_id) VALUES (?, ?, ?, ?)' with params [34, null, null, 125]:"
I also generated Getters and Setters on the Entity. Do I also need to do something else?
I believe you forgot to enable the doctrine extension. Here you can see how to do it https://symfony.com/doc/cur...
I did it. Now it says the following:
The Doctrine connection "other" referenced in service "stof_doctrine_extensions.listener.timestampable" does not exist. Available connections names: "default".
Do I have to manually add setCreatedAt(), setUpdatedAt() in the controller? I thought the trait would take care of some of it.
my stof_doctrine_extensions.yaml:
EDIT: I just added a $date = new \DateTime(); and passed it through. Now it works. At first, I thought that Symfony might handle some of it in the background.
The extension bundle should take care of it. It works via Doctrine listeners. The problem is the
otherconnection configuration is just an example, you should put it on the "default" one (unless you're using multiple databases)Cheers!
Hey Farry,
It should come from the Controller which rendered the template
Cheers!
Off topic: I am getting an error: App\Entity\Measurement object not found by the @ParamConverter annotation.
I just cant seem to understand how to fix this.
Hey Farry,
The ParamConverter grabs the query parameter from the URL and try to fetch an object based on it. You can read more about it here https://symfony.com/doc/cur...
Cheers!
Hey @Farry7
The "article" string is the name of the class' property
Cheers!
So following this I find that at no time have we added the createdAt date in the code supplied! - scratch that, retraced and sorted!!!
Hey Joe L.
We added the
TimestampableEntitytrait to the Comment class in the first video, you may have lost it but here it is :)https://symfonycasts.com/screencast/doctrine-relations/comment-entity?playAt=237
Cheers!
Yeah - I retraced my steps and saw I missed one!!!! Thanks anyway!!!
How to make Lazy Loading if you havent (Article $article) just normal repository? Not everyone take params from route
Hey Szymon Z.
What you won't is to not fetch for the Article record automatically? If that's the case just change your
Article $articleargument tostring $slugCheers!
Hi,
I did not succeed the dump($comments);die; I have this error:
App\Entity\Article object not found by the @ParamConverter annotation.
Can you help me please?
Hey Stephansav,
So, Doctrine was not able to find the Article object. Please, make sure that an Article exist with the slug you specified in the URL. For this, you can check your article table in MySQL DB. Most probably you just have a typo in your slug
Cheers!
Hello!
About caching relations,
I want to cache entity and some of its relations.
1)
I suppose I need to serialize to json.
Will it be better to cache serialized json objects or other policy?
2)
I can use fetch="EAGER" for relation OneToMany, let's say Patient => Employers.
Then I want to serialize so eager fetched relations objects will be serialized as well.
In my test, I have $patient has 2 employers,
also used group to focus on "firstName" field and "employers" relation only.
$json = $this->m_serializer->serialize(
$patient,
'json', ['groups' => 'demographics']
);
Yet, I get empty relation objects.
{"firstName":"Sarah","employers":[[],[]]}
3)
Is it possible without fetch="EAGER" ?
4)
Trying to create custom normalizer as in
https://symfony.com/doc/cur...
public function normalize($data...)
$data is $patient entity comes with relations yet after
$res = $this->normalizer->normalize($data, $format, $context);
$res is {"firstName":"Sarah","employers":[[],[]]}
with [] instead employer relation array.
Thanks!
Hey Avraham M.
If you want to cache queries, you can use Doctrine cache to do it but I don't recommend it because you would have to purge the cache whenever your object changes and it can be very problematic. What you can do is to use the
app.cacheservice and cache computed data. What I mean is if you want to cache the serialized version of an object, you can cache the result and probably add a TTL to it, or do whatever you need to be sure that you are not using old cached dataCheers!
I started to use TagAWareCacheInterface cache, very interesting!
Thanks!
hi everyone
I get empty comments, I don't understand why when Im using the find() methods in Entity Repository I got empty array
Thank you for your help..
Hey hanen
If you already load the fixtures (or manually added some comments), you should see the comments. So, first double check the database records and if you see comments there, then maybe you are not fetching from the right repository. Double check you are injecting the CommenRepository.
Cheers!
Hi Diego
I already loaded my comments and when I use php bin/console doctrine:query:sql 'SELECT * FROM comment' it show up normally but in my controller when I fetching for comments it doesn't work
ArticleController.php on line 80:
[]
I think my problem is that I migrate comment table it produced following error ::
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'comment' already exists
And you are using the CommentRepository in your controller, right? Can you double check that you are on the dev environment? or that you loaded the fixtures for the dev environment
Another thing you can check is the profiler at the "Doctrine" tab, so you can see which queries are being executed
Thank you for your response
I think findBy method is not working but findAll returns records of comments
Ohh, the
findBy()method expects a criteria, in other words, your WHERE clause, so it can filter the records. Probably you where passing something that matches nothing in your table? That's why I recommend you to inspect the executed queries via the profiler :)Hi Diego
the doctrine connection had the access to database and in symfony profiler there are two queries that were executed
#â–¼ Time Info
2 1.00 ms
SELECT t0.id AS id_1, t0.author_name AS author_name_2, t0.content AS content_3, t0.created_at AS created_at_4, t0.updated_at AS updated_at_5, t0.article_id AS article_id_6 FROM comment t0 WHERE t0.article_id = ?
Parameters:
[â–¼
64
]
Hide formatted query Hide runnable query Explain query
SELECT
t0.id AS id_1,
t0.author_name AS author_name_2,
t0.content AS content_3,
t0.created_at AS created_at_4,
t0.updated_at AS updated_at_5,
t0.article_id AS article_id_6
FROM
comment t0
WHERE
t0.article_id = ?
SELECT t0.id AS id_1, t0.author_name AS author_name_2, t0.content AS content_3, t0.created_at AS created_at_4, t0.updated_at AS updated_at_5, t0.article_id AS article_id_6 FROM comment t0 WHERE t0.article_id = 64;
1 4.00 ms
SELECT t0.id AS id_1, t0.title AS title_2, t0.slug AS slug_3, t0.content AS content_4, t0.published_at AS published_at_5, t0.price AS price_6, t0.name AS name_7, t0.author AS author_8, t0.heart_count AS heart_count_9, t0.image_filename AS image_filename_10, t0.created_at AS created_at_11, t0.updated_at AS updated_at_12 FROM article t0 WHERE t0.slug = ? LIMIT 1
Parameters:
Hey hanen, sorry for the late reply
Your queries looks fine to me. The first one is trying to get all comments for given Article ID (64), and the second one is trying to find an Article from its slug field
If you think you have a problem with your DB, you can recreate it from scratch like this:
Cheers!
Hi Diego
when I run this command php bin/console:schema:create I got this error:
Schema-Tool failed with Error 'An exception occurred while executing 'CREATE TABLE migration_versions (version VARCHAR(255) NOT NULL, PRIMARY KEY
(version)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB':
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes' while executing DDL: CREATE TABL
E migration_versions (version VARCHAR(255) NOT NULL, PRIMARY KEY(version)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = Inno
DB
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes..
And thank you for your assistance :)))
Ohh, that's because of your MySql version, please upgrade to 5.7+ or if that's not possible you will have to limit the length of your fields to 181 characters I believe.
Hello.
How can I fetch comments using "eager loading". Lazy loading it's not always best option.
Hey Dominik!
You have two options for this:
1) You can add a
fetch="EAGER"option to your OneToMany annotation. But that will fetch them "eager" in ALL cases.2 Create a custom query that JOINs to the comments and fetches them. We do that here: https://symfonycasts.com/screencast/doctrine-relations/join-n-plus-one#solving-the-n-1-extra-queries-problem. Well, that is an example of starting with a Comment and joining back to an Article. You would do the opposite: start with an Article, then leftJoin over to comments.
Let me know if this helps!
Cheers!
I watch that video about join and it works. Thank you.
Hello I get a error with:
An exception has been thrown during the rendering of a template ("An exception occurred while executing 'SELECT t0.id AS id_1, t0.author_name AS author_name_2, t0.content AS content_3, t0.created_at AS created_at_4, t0.updated_at AS updated_at_5, t0.article_id AS article_id_6 FROM comment t0 WHERE t0.article_id = ?' with params [49]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.created_at' in 'field list'"). Please help me out
Hey Frank K.
I believe your DB schema is out of sync, I mean, you added a new property to your entity but didn't update the schema
How can I check that the PersistentCollection is empty?
$comments = $article->getComments();
empty($comments)
Always return false.
I find it's
$article->getComments()->isEmpty()
Hey Dmitriy,
Yeah, you nailed it! That's exactly the best way to check it. The "empty($comments)" always returns false because you check that $comments variable is empty, but that's not true, it's always an object. And thanks for sharing your solution with others!
Cheers!
"Houston: no signs of life"
Start the conversation!