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!
03.
Saving 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
}
}
23 Comments
Hey @Rayen, hey there ,
Doctrine encourage to take over and control transaction demarcation ourself! So how to deal with it? Can you tell me a "good" use-case when to you use it?
Big thanks!
Hey Houssem!
Are you referring to database transactions? If so, excellent question!
First, Doctrine automatically wraps its operations in a transaction. You can actually see this in one of our videos - https://symfonycasts.com/sc... - just hit "play" and you'll see how Doctrine has wrapped the INSERT query in a transaction. So if you're inserting 2 related objects at once, and one fails, both will be rolled back. You don't even need to think about it.
But, you *can* manually start and commit a transaction if you want. That's useful if, for example, you need to save something to the database AND send some data to an external API... and if the API call fails, you want to "roll back" your database query. We don't have any videos on this, but it's fairly straightforward: https://www.doctrine-projec...
Let me know if that helps!
Cheers!
Hey guys, I'm a little bit confused about two things. Why we persist $comment and not persist $article ? The second stuff is about this use($manager) I didn't know this syntax, where can I have more details about this use inside a method .
Hey Mastou,
Why persist($comment)? Because we just created that Comment object. So, basically, if you create a new entity, to say Doctrine to store that object into DB you need to call persist on that object first, otherwise that object just won't be stored in the DB, Doctrine will just ignore them. So, the strategy is the next: create a new entity object, fill in some data on it, them call persist() on it to say Doctrine to save this object on next flush() call, and actually call flush() so doctrine execute queries and save everything that was modified into the DB.
To know more about that "use($manager)" syntax - google anonymous PHP functions, or just see this link to PHP docs: https://www.php.net/manual/... . Basically, it helps to pass some context to that anonymous function, i.e. in our case we allow to use that $manager object *inside* that function.
I hope this is clearer for you now!
Cheers!
I have a user and user_account tables, the id of the user is a foreign key in the user_account, basically a join table (one user to many user accounts). I have mapped the relationships in my two entities (user, useraccount), correctly I believe. Before persisting, in my current setup a user id does not yet exist. What I'm trying to do is create the user then create the useraccount with the id of the user just created in one persist/flush. I keep getting an error that id is null when persisting the useraccount. If my these entities are related properly can I create both at the same time? I hoping what I am asking makes sense. Thank you!
Hey brentmtc,
First of all, you can check if your mapping is correct with "bin/console doctrine:schema:validate" command. Suppose, it is correct, then it should just work. You just need to persist both entities, i.e. call "->persist($user)" and "->persist($userAccount)" and only then call one "->flush()" that will save both entities to the DB.
If you still have this problem, hm, then what exactly id is null in the error? UserAccount::id or UserAccount::userId? Please, check your id property declaration, does it looks like this in both User and UserAccount entities?
Cheers!
Thank you. I was able to get this working by adding a user_id field to my user_account table.
I believe you should update the codebase you provide for this tutorial - folder 'start'
ArticleFixtures::loadData() does not use faker for generating article content, but uses hard-coded text instead. This might be by purpose, but using faker for all properties would be much nicer, if you ask me.
Not critical, but still confusing a bit.
Hey again Yaroslav Y.!
Can you explain a bit more? Do you just mean that it's a bit inconsistent to use Faker for many of the properties, but use hardcoded text for the article content field? If so, you're right - but it was also done on purpose - we include some markdown formatting in that string, so we can see that markdown processing is working, and also include a few words that we check for later for a feature. Basically, we wanted to control the article content... but the downside is that (you're right) it's not random anymore.
Cheers!
This tutorial worked well. But there is one small issue by selecting the data from the database with php bin/console doctrine:query:sql 'SELECT * FROM comment'
I am getting this error: Too many arguments, expected arguments "command" "sql".
Hey Symfonybeard ,
I suppose you're on Windows OS. Try to use double quotes instead, i.e.:
php bin/console doctrine:query:sql "SELECT * FROM comment"
Does it help? :)
Cheers!
Great, this works! Thanks for your help. :)))
Glad it works! Yeah, a tricky thing about Windows console I learned from other users :)
Cheers!
I really appreciate how the end of each video kind of teases the subject of the next video. It piques my curiosity and motivates me to continue to the next topic. It's a nice touch!
Ha! Perfect! That's why we do it... but it's nice to have feedback that it's working! Keep going!!!! :D
Cheers!
Hello guys,
do you also get the deprecation log message "User Deprecated: Doctrine\Common\ClassLoader is deprecated." ?
Cheers
Hey fahani!
Yep! It's nothing to worry about. There is some deprecation things happening right now... which should go away when doctrine/orm releases their next version. You can happily ignore this ;).
Cheers!
Variable $count here is not used and can be removed.
Hey toporovvv ,
You're totally right! Thanks for letting us know. Probably not a big deal, we've kept it so you can easily modify our example and see that there's an extra $count argument which you can use if you need.
Cheers!
Anyone trying this... I had to add a use to my callback function:
> $this->createMany(Article::class, 10, function(Article $article, $count) use ($manager) {
Hey GDIBass
If you have to use a local variable inside a callback function, then you have to specify it by doing what you did. The compiler requires to keep track of the reference of that variable some how :)
Cheers!
Yup. I didn't see another way of adding comments referencing the article while also having $manager to persist.
I know it's not obvious yet, because (at this moment) the video & code blocks aren't released yet, but yes, we do this exact thing - the script contains:
So, you're doing it totally right :). Later, we'll cleanup things a bit and put Comments into their own fixture class. But for right now, that use is needed to get the
$managervariable in scope so we can persist the comments.Cheres!
"Houston: no signs of life"
Start the conversation!