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!
21.
Deleting an Item from a Collection: orphanRemoval
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
Which doctrine annotation should I use if I really wanted to assign a $genusScientist to another $genus instead of completely removing it from the database? Thanks.
Gey Gustavo,
I'm not sure Doctrine has such annotation :) How will Doctrine know to whom assign it? It's only possible to set genus to NULL on deleting. I *think* you can add a listener to listen to preRemove event and implement a custom logic in it to assign it to another Genus.
Cheers!
Thank you very much for your response. What I was referring to wasn't to assign the $genusScientist to another $genus when performing the delete action. I just want to free the $genusScientist so that another $genus can add it from the edit action, for example. When I use OrphanRemoval the $genusScientist is completely removed, so if a different $genus wants to add it, it needs to re-enter all the $ genusScientist information again. I apologize for my English, I'm learning it too.
but you have given me the answer. In the $genusScientist entity, I only have to write *@ORM\JoinColumn(nullable=true) in the field $genus, and that's it! again, thank you very much for your time
I get an error when I added 'by_reference' =>false to the GenusFormType
->add('genusScientists', CollectionType::class, [
'entry_type' => GenusScientistEmbeddedForm::class,
'allow_delete' => true,
'by_reference' =>false
])
CRITICAL - Uncaught PHP Exception Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException: "Neither the property "genusScientists" nor one of the methods "addGenusScientist()"/"removeGenusScientist()", "setGenusScientists()", "genusScientists()", "__set()" or "__call()" exist and have public access in class "AppBundle\Entity\Genus"." at /var/www/aqua_note/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 614
The genus entity has the method:
public function removeGenusScientist(GenusScientist $genusScientist)
{
if (!$this->genusScientists->contains($genusScientist)) {
return;
}
$this->genusScientists->removeElement($genusScientist);
// Needed to update the owning site of the relationship
$genusScientist->setGenus(null);
}
What could be wrong?
Hey Hank!
Do you also have the method "addGenusScientist()"? you need both if you are going the "remove" way, instead of using a setter
Cheers!
Hi Diego, You were right again I made a typo in the addGenusScientist() and typed addGenusScientists() with an extra s at the end. Thanks!
NP!
I'm glad to hear you could fix your problem :)
Cheers!
This solution to remove a genusScientist from the Genus will only remove ONE genusScientist when the form is submitted, regardless of how many you removed from the form.
Hey Tony C!
Are you sure? I just backed up my code to this exact step in the tutorial and tried it - I removed 4 genus scientists of the 7 on a page and saved - all 4 I selected were removed. Are you seeing something different?
Cheers!
"Houston: no signs of life"
Start the conversation!