Chapters
28 Chapters
|
2:24:59
|
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!
17.
Join Entity App Refactoring
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!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This course is built on Symfony 3, but most of the concepts apply just fine to newer versions of Symfony.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"symfony/symfony": "3.4.*", // v3.4.49
"doctrine/orm": "^2.5", // 2.7.5
"doctrine/doctrine-bundle": "^1.6", // 1.12.13
"doctrine/doctrine-cache-bundle": "^1.2", // 1.4.0
"symfony/swiftmailer-bundle": "^2.3", // v2.6.7
"symfony/monolog-bundle": "^2.8", // v2.12.1
"symfony/polyfill-apcu": "^1.0", // v1.23.0
"sensio/distribution-bundle": "^5.0", // v5.0.25
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.29
"incenteev/composer-parameter-handler": "^2.0", // v2.1.4
"composer/package-versions-deprecated": "^1.11", // 1.11.99.4
"knplabs/knp-markdown-bundle": "^1.4", // 1.9.0
"doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.2
"stof/doctrine-extensions-bundle": "^1.2" // v1.3.0
},
"require-dev": {
"sensio/generator-bundle": "^3.0", // v3.1.7
"symfony/phpunit-bridge": "^3.0", // v3.4.47
"nelmio/alice": "^2.1", // v2.3.6
"doctrine/doctrine-fixtures-bundle": "^2.3" // v2.4.1
}
}
10 Comments
Wouldn't it be better to have a composite primary key for the GenusScientist entity, consisting of two foreign keys (genus_id, user_id), since this way, with surrogate primary key (id), theoretically we can end up with the same two combinations of foreign keys (genus_id, user_id) under two different primary keys (id)?
Hey @Mirko
That's a good point and it actually depends on your data constraints, if you have decided that you don't want to have duplicated combinations then you can set up unique constraints at the DB level. Check this out: https://www.doctrine-projec...
Cheers!
Ok, so I can put a unique constraint on the genus_id-user_id combination if I want to. But, as I understand, GenusScientist table (entity) must not have the same combination of genus_id and user_id more than once, since this table acts like an intermediary table in many-to-many relationship between Genus and User.
My question is, if we make a combination of genus_id and user_id a primary key, then we don't need the surrogate id nor do we need to put a unique constraint on genus_id-user_id combination, since primary key is unique by default?
Ohh, you have a good point and probably from a DB point of view that would be the way to go but to be honest I haven't done that on Doctrine, so, I'm not totally sure if it's supported or not (Maybe yes?). If you find more info about it please let me know :)
Cheers!
Yes, I managed to do it with Doctrine by following this: https://www.doctrine-projec...
Down below are the annotations I used for the GenusScientist entity's primary key. For the remainder of the tutorial I just needed to do some minor adjustments, but otherwise everything went well.
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
* @ORM\ManyToOne(targetEntity="Genus", inversedBy="genusScientist")
* @ORM\JoinColumn(nullable=false)
*/
private $genus;
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
* @ORM\ManyToOne(targetEntity="User", inversedBy="studiedGenuses")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
Cool! Thanks for sharing it :)
Do we have to keep field: id as a primary key for GenusScientist? What implictaions will be here if I will remove it and add composite primary key?
Hey Krzysztof K.
Very interesting question! Honestly, I haven't had the need (or obligation) of using a composite primary key for my entities, but I found a nice article about how to do it: https://www.doctrine-projec...
I hope it helps. Cheers!
MolloKhan thanks :) I will do some tests to see hoe it works, maybe I will comeback here with more questions.
Excellent, I'm ready! (cracking knuckles)
"Houston: no signs of life"
Start the conversation!