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!
// 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
}
}
25 Comments
Hi,
I am having an issue using QueryBuilder.
I have a Entity\Social (id, name, icon), Entity\UserSocial (id, user[ManyToOne with User entity], social[ManyToOne with Social Entity]) and then a Entity\UserSocial (id, user, [ManyToOne with User entity], social[ManyToOne with Social Entity], url).
In the SocialRepository I created a new method called findAllWithUserValues, but I cannot seems to be able to get the result I want. If ran a raw query (
select social.*, us.url from social LEFT JOIN user_social us on social.id = us.social_id AND us.user_id = 1) I get exactly what I need. When I am editing a user profile, I want all social media to be listed there. Out of all, some are associated with the user, having it filled with the user url.This is my method:
`public function findAllWithUserValues()
{
}`
If I try to get the DQL, I get the query I want:
"<blockquote>SELECT s., us.url FROM App\Entity\Social s LEFT JOIN App:UserSocial us ON s.id = us.social_id AND us.user_id = 1</blockquote>" but when I try ->getQuery()->getResult(), I get an error which doesn't make any sense: <blockquote>[Syntax Error] line 0, col 9: Error: Expected Doctrine\ORM\Query\Lexer::T_IDENTIFIER, got ''</blockquote>
Any Idea?
Hey Alessandro D.
I think you only have to remove the
.*part. DQL, if you want to select all fields of an entity table you do so by saying->select('alias');. Please try that and let me know if it worked :)Cheers!
Hey Diego,
I realised that s.* was causing the issue, but when I removed it, Docrine gave me an issue in the “ON” conditionType. If I replaced the ON with “WITH”, the error was gone but I didn’t get the reault I wanted.
Would you mind giving some real working example of DQL leftJoin that works? Perhaps an example that select specific fields from joined table, so that I can study it, test it and compare it with what I had,
Thanks,
Alessandro
Hey Alessandro D.
I think what you need to do is this
Notice how I change
selecttoaddSelectso it will select everything from "s" plus the "us.url" field. Also notice how I added theWHEREclause by usingandWhere()methodI hope it helps. Cheers!
Hi,
I have a problem to start the project. I update my Linux Ubuntu and after launching
<b>php bin/console server:run</b>
I had
`
Extension DOM is required.
`
So, I preferred to delete the project and downloading and unzipping a new project and launching composer install in the start directory. But, now I have this message:
`
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Your requirements could not be resolved to an installable set of packages.
Problem 1
Problem 2
Problem 3
Problem 4
To enable extensions, verify that they are enabled in your .ini files:
You can also run
php --iniinside terminal to see which files are used by PHP in CLI mode.`
Can you help me please?
Hey @stephansav
Looks like you should enable required php extensions: php-xml, php-mbstring.
Cheers
My boss like 5 years ago said its bad to select all fields from the query (select * from table) and I always had to specify them. And still trying doing so, to not be "bad" programmer. Also back then when I searched in the internet, also found saying this is bad. And instead said to select each field which you need, even if you need most of them. But I now see in doctrine you and also other coders select all columns without specifying one by one. I would think this could improve performance a bit, by selecting only columns which you need. So I guess my boss was wrong? not sure why in all cases can it be bad to select all fields. If its not a performance issue, it should not be bad? And when there is performance issue, then can specify which ones to select.
Hey Lijana Z.
If you will hydrate your results as objects, then, there is no way to select only a few of the fields but if you are writing a raw SQL or working with an array, then it's totally fine to only select the fields that you are going to use. Let's be pragmatic first than perfect :)
Cheers!
The boss is a little right. But. Think about how to create an entity if you select only a Name without Ids, Dates ... Objects in Symfony will not be created, but you can work with an array in raw code
Yep, that's why Doctrine doesn't allow to hydrate objects partially
I have the exact same problem, the n+1 problem.
I have a category.php entity, which is self referencing:
{% for category in categories %}
the getChildren method creates a new query for every category item.
The solution seems to be a join with addSelect, BUT this is not possible because it is self referencing, both the mapped_by and inversed_by side is the same table/entity (categories).
<b>How can I prevent doctrine from creating new querys for every item on a ManyToOne relation for a self-referencing table?</b>
<u><b>UPDATE:
</b></u>The join works now, but the <u>problem still exists</u>, doctrine creates a new query for every getChildren Call.
Current source code:
Controller:
Category Repository:
Category Entity:
Template:
Hey Mike P.
You made a join to the "parent" field but it should be to the "children" field, isn't it? Give it a try and let me know if it works
Cheers!
It worked flawlessly! Thanks!
Hello,
I was wondering why I can't perform joins from different functions. Let's say, I want to make a selection based on different search parameters, not all of which are mandatory, but they do all require the same join. I want to build this query using several functions which I puzzle together based on the search parameters.
For example, these two functions:
So I have two functions. I call this one first:
and then this one:
This triggers an error, saying 'est' is not defined. It seems like the only way to fix this, is to join equipmentSubtype again, but this causes the eventual query to have 2 joins with the same table.
Is there a way to pass those table aliasses?
Hey Marlies,
Hm, but it should work! So, if I understand you right, you have those 2 methods, and somewhere in your application you call them both, something like:
Am I right? Because it should work well. The only reasons I can see why it may fail is when you call those methods in the incorrect order, i.e. call search() first and only then addEquipmentSubType().
Oh, it probably not important, but just in case it is try to call leftJoin() first and only then that where() clause:
And please, double check your alias name, because it's easy to misprint it like "ets" instead of "est".
If nothing helps, please, show me the code where you call them exactly.
Cheers!
Again nice course, but have small problem. I try to do similar search function but for Article with Comment (you have Comments with Article).
But, query is just try to search in articles, how have any comments. Exp. if I didn't search any string, I must see my all articles, but now I see article just how has any comments. How to fix it?
public function SearchString(?string $term)
{
$qb = $this->createQueryBuilder('b')
->innerJoin('b.comments', 'c')
->addSelect('c')
;
if ($term) {
$qb->andWhere('b.text LIKE :value OR c.text LIKE :value')
->setParameter('value', '%' . $term . '%')
;
}
return $qb
->orderBy('b.id', 'DESC')
->getQuery()
->getResult()
;
}
Hey @Student
If I got you right, you want to find all comments that may contain a string but filtered by an Article as well. Then you will have to do a query like this:
Cheers!
If I have search input in another "block.html.twig" not in "index.html.twig", how I can load {{ app.request.query.get('q') }} ? "app" is empty.
Hey Student,
Are you sure the "app" variable is empty? Because this variable is global and available in all templates. Or are you trying to customize your form with a custom form theme? The question is: How are you using that "block.html.twig" in "index.html.twig"? Did you include it somehow?
Cheers!
How do you avoid additional queries on a list of entities with their relationships? Say, related to this example, I want to list some of the comments on the index of posts, similar to the Facebook news feed. For each post, it performs a separate query on the comments table for each post to get the comments. So if a show 10 posts, it's doing 11 queries.
Eloquent handles this using the 'with' method, which uses an array of post IDs to get all related comments in one query, then it somehow hydrates the relevant post with it's comments. So only 2 queries are needed. Is there a similar way to do this with Doctrine? I'm not sure how I would optimize this manually, since the foreign key ID fields are hidden.
Hey Shaun M.
Good question, but first of all remember the rule - Do not optimize prematurely.
Now, if you still need to do it, or you are just curious then what you have to do is to create a custom repository method that will fetch all posts but adding a "JOIN" (with a SELECT) to the desired relationship. In this case you will end up with only one query
Cheers!
Is there a reason you're using PHPDoc annotations in PHP7? I see that they're more expressive :array vs @return Comments[], but I'm wondering if there's some benefit, ie. from PHPStorm or static analysis or something.
Hey Greg,
Yeah, there's definitely a benefit in using more expressive annotations in addition with PHP 7 return types, and as you said PhpStorm add more autocompletion for it, i.e. when you use "@return Comment[]" - PhpStorm will know that it's not just an array with unknown values but an array of Comment objects, and when you will iterate over those comments, PhpStorm will suggest you autocompletion for single comment in that foreach.
Cheers!
I've been getting that with a /** @var Comment $comment */ right above all of my foreach loops. I guess it's an extra line or two in one place vs. another... if only PHPStorm were even more magical!
Hey Greg,
Yes, the "/** @var Comment $comment */" line may work too, but if you need to iterate over those comments in a few spots - you'll have a few lines. Or you can do it once above the method, and in all spots you'll have autocompletion which is much better, and easier to refactor in the future, you'll need to tweak only one spot :)
Cheers!
"Houston: no signs of life"
Start the conversation!