php


Symfony 3 orphanRemoval=true doesn't delete removed records from form


I need t ocreate a bus adding form. Bus can have many amenities and that's why I created OneToMany relationship between bus and bus amenities.
What I want to achieve is to let Doctrine to remove (or update) data from OneToMany table without looping or doing anything particular in controller. I read somewhere that you can use orphanRemoval=true annotation on Entity that will take care of that but for some reason it doesn't work...
I can update existing and add new amenities (new data) but I can't delete records that I previously removed from Edit form.
Can it be done using orphanRemoval=true or does someone know where is the problem with my code:
Here is the code:
Bus Entity (shorted version):
/**
* #ORM\OneToMany(targetEntity="BusVehiclesAmenities", mappedBy="bus", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $busVehiclesAmenities;
public function __construct()
{
$this->busVehiclesAmenities = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add busVehiclesAmenities
*
* #param \AppBundle\Entity\BusVehiclesAmenities busVehiclesAmenities
* #return BusVehicles
*/
public function addBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
/* if ($this->busVehiclesAmenities->contains($busVehiclesAmenities)) {
return;
}*/
$this->busVehiclesAmenities->add($busVehiclesAmenities);
$busVehiclesAmenities->addBus($this);
return $this;
}
/**
* Remove busVehiclesAmenities
*
* #param \AppBundle\Entity\BusVehiclesAmenities $busVehiclesAmenities
*/
public function removeBusVehiclesAmenities(BusVehiclesAmenities $busVehiclesAmenities)
{
$this->busVehiclesAmenities->removeElement($busVehiclesAmenities);
$busVehiclesAmenities->addBus(null);
}
/**
* Get busVehiclesAmenities
*
* #return ArrayCollection
*/
public function getBusVehiclesAmenities()
{
return $this->busVehiclesAmenities;
}
/**
* Set busVehiclesAmenities
*
* #return ArrayCollection
*/
public function setBusVehiclesAmenities($busVehiclesAmenities)
{
$this->busVehiclesAmenities = $busVehiclesAmenities;
}
Amenities Entity:
private $bus;
/**
*
* #ORM\ManyToOne(targetEntity="Amenities", inversedBy="amenities")
* #ORM\JoinColumn(name="amenities_id", referencedColumnName="id")
*
*/
private $amenities;
/**
* #var float
* #ORM\Column(name="price", type="float", scale=2)
*/
protected $price;
public function __construct()
{
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get busId
*
* #return integer
*/
public function getBus()
{
return $this->bus;
}
/**
* Add bus
*
* #param \AppBundle\Entity\BusVehicles
*
* #return BusVehiclesAmenities
*/
public function addBus(BusVehicles $bus)
{
$this->bus = $bus;
//$bus->setBusVehiclesAmenities($this);
return $this;
}
Controller:
public function editAction($id, Request $request)
{
$busRepository = $this->getDoctrine()->getRepository('AppBundle:BusVehicles');
$bus = $busRepository->find($id);
$editForm = $this->createForm(BusVehiclesType::class, $bus);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $editForm->getData();
dump ($entity);
$em->persist($entity); //this should take care of everything saving the manyToMany records
$em->flush();
// Adding flash message to our user
$this->addFlash('success', 'New vehicle successfully added');
// return $this->redirectToRoute('bus_add');
}
return [
'form' => $editForm->createView(),
];
}
I'm having the same problem but looking at your code I notice that you're calling
$busVehiclesAmenities->addBus(null);
instead of
$busVehiclesAmenities->setBus(null);

Related Links

Sort PHP Array based on another Array (using substr(“String”,-3,1) function)
Insert data in mysql table
How can I search for post's 'title','content'&'meta_query' without WP_Query 's'?
file_get_contents(): stream does not support seeking PHP
Cannot start service php-fpm: invalid header field value "oci runtime error: container_linux.go:247: starting container
Symfony3 “The option ”placeholder“ does not exist.”
Wordpress wp-login redirecting to wrong url when using Forward Rewrite
Magento admin dashboard issue graph is working but revenue, shipping, order is zero
imagemagic composite and convert annotate in one command
cant displaying operator in php
Not able to upload multiple files in php
Request not found error in laravel controller
Android login with Arabic values failed
Correct way to use multiple AJAX actions?
Inserting multiple generated student records from an HTML5 form into MySQL database
merge several strings with a separator in one variable, and seperate them once the variable is called

Categories

HOME
openshift
mod-rewrite
apache-nifi
list
eclipse-plugin
exchange-server
add-on
operating-system
openlayers-3
android-emulator
websocket
consul
bing-search
nsis
google-sheets-query
biztalk-2010
openfoam
minecraft
native-base
scheduler
responsive-design
fireloop
graphdb
orc
amazon-elb
mahout-recommender
supervisord
android-5.1.1-lollipop
c#-7.0
mixpanel
exec-maven-plugin
el
fido-u2f
candlestick-chart
pyopencl
function-points
data-collection
tracing
hevc
android-service
running-object-table
oracleforms
source-insight
opnet
john-the-ripper
om-next
cloudera-manager
nest
busboy
variable-variables
hidden-markov-models
linq.js
right-click
classnotfoundexception
docker-ucp
adblock
libreoffice-writer
brightscript
uiautomatorviewer
sigsegv
nslayoutconstraint
stateless-session-bean
sesame
case-when
decoder
spring-mongo
msbuild-4.0
eclemma
ajaxmin
codecharge
utf-16
gulp-uglify
enunciate
boost-proto
textpattern
blank-line
sequence-sql
gflags
rfc5545
camus
phonegap-facebook-plugin
famo.us
zend-mail
xml-signature
dmake
lumia-imaging-sdk
tabletools
boolean-algebra
strcmp
hyphen
notifyjs
eager-loading
oscilloscope
email-verification
nsmutablestring
gamesalad
java.lang.class
liveconnect
differentiation
firefly-mv
fork-join
sql-parametrized-query
nsconnection
mongrel2
regression-testing
bass
fuzzy-comparison

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App