I'd like to know if there is possibility to exclude some form fields from persisting/flushing to database (but keep having 'mapped' => true
).
I want to achive effect that user adds one row (Device
) with it's specs but with multiple serial numbers and after aubmit splits it and persists
The problem is form data, because when I have mapped my data to from, calling $em->flush()
will persist all mapped data to form, so with all pasted serialnubmers
as one device. (And in this specyfic code below it will persist all SN as separate device + N device with all sn-s)
$data = $deviceAddForm->getData();
foreach ($data['devices'] as $key => $device) {
$device->setAgreement($a);
$snList = array_filter(preg_split('/[\s,~]+/', $device->getSerialNumber()), 'strlen');
if($device->isAccessory() != true){
foreach ($snList as $key => $sn) {
$d = new Device();
$d->setAgreement($a);
$d->setSerialNumber($sn);
$d->setQuantity(1);
$d->setAccessory(false);
$d->setName($device->getName());
$d->setPartNumber($device->getPartnumber());
$d->setModyfications($device->getModyfications());
$d->setWarrantyEnds($device->getWarrantyEnds());
$d->setSla($device->getSla());
$d->setDeviceCategory($device->getDeviceCategory());
$em->persist($d);
}
} else {
$em->persist($device);
}
}
$em->flush()
$em->flush() flushes additionaly devices which are in array $data['devices'].
I'd like to somewhat prevent $data['devices'] from flushing but still have mapped devices
field in form.
Idea to flush each $d
significantly slows dows as there it multiple database calls
You're looking to detach the original Device so it's not immediately persisted, you can read more about it in the Doctrine docs: https://www.doctrine-project.org/projects/doctrine-orm/en/3.3/reference/working-with-objects.html#detaching-entities
Detaching means that whatever changes you're making, are no longer automatically persisted to the database.
$data = $deviceAddForm->getData();
foreach ($data['devices'] as $key => $device) {
$device->setAgreement($a);
$em->detach($device);
$snList = array_filter(preg_split('/[\s,~]+/', $device->getSerialNumber()), 'strlen');
//etc.