Skip to content

Commit 0e31ea6

Browse files
authored
Merge pull request #63 from sherlockode/fix/deprecations_om
Replace ObjectManager by EntityManagerInterface
2 parents 67684df + b699903 commit 0e31ea6

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

Form/Type/TemporaryUploadedFileType.php

+18-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Sherlockode\AdvancedFormBundle\Form\Type;
44

5-
use Doctrine\Common\Persistence\ObjectManager;
5+
use Doctrine\ORM\EntityManagerInterface;
66
use Sherlockode\AdvancedFormBundle\Form\DataTransformer\TemporaryUploadFileTransformer;
77
use Sherlockode\AdvancedFormBundle\Model\TemporaryUploadedFileInterface;
88
use Symfony\Component\Form\AbstractType;
@@ -19,13 +19,19 @@ class TemporaryUploadedFileType extends AbstractType
1919
private $tmpFileClass;
2020

2121
/**
22-
* @var ObjectManager
22+
* @var EntityManagerInterface
2323
*/
24-
private $om;
24+
private $em;
2525

26-
public function __construct(ObjectManager $om, $tmpFileClass)
26+
/**
27+
* TemporaryUploadedFileType constructor.
28+
*
29+
* @param EntityManagerInterface $em
30+
* @param string $tmpFileClass
31+
*/
32+
public function __construct(EntityManagerInterface $em, $tmpFileClass)
2733
{
28-
$this->om = $om;
34+
$this->em = $em;
2935
$this->tmpFileClass = $tmpFileClass;
3036
}
3137

@@ -40,17 +46,22 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4046
->add('token', HiddenType::class)
4147
;
4248

43-
$builder->addViewTransformer(new TemporaryUploadFileTransformer($options['temporary_path'], $this->om->getRepository($this->tmpFileClass)));
49+
$builder->addViewTransformer(new TemporaryUploadFileTransformer(
50+
$options['temporary_path'],
51+
$this->em->getRepository($this->tmpFileClass)
52+
));
4453
$builder->addViewTransformer(new CallbackTransformer(function ($data) {
4554
if (!$data instanceof TemporaryUploadedFileInterface) {
4655
return [];
4756
}
57+
4858
return ['key' => $data->getKey(), 'token' => $data->getToken()];
4959
}, function ($data) {
5060
if (!is_array($data)) {
5161
return null;
5262
}
53-
$tmpFile = $this->om->getRepository($this->tmpFileClass)->findOneBy($data);
63+
$tmpFile = $this->em->getRepository($this->tmpFileClass)->findOneBy($data);
64+
5465
return $tmpFile;
5566
}));
5667
}

Manager/UploadManager.php

+16-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Sherlockode\AdvancedFormBundle\Manager;
44

5-
use Doctrine\Common\Persistence\ObjectManager;
5+
use Doctrine\ORM\EntityManagerInterface;
66
use Sherlockode\AdvancedFormBundle\Event\RemoveUploadEvent;
77
use Sherlockode\AdvancedFormBundle\Event\UploadEvent;
88
use Sherlockode\AdvancedFormBundle\Model\TemporaryUploadedFileInterface;
@@ -15,9 +15,9 @@
1515
class UploadManager
1616
{
1717
/**
18-
* @var ObjectManager
18+
* @var EntityManagerInterface
1919
*/
20-
private $om;
20+
private $em;
2121

2222
/**
2323
* @var MappingManager
@@ -44,20 +44,20 @@ class UploadManager
4444
/**
4545
* UploadManager constructor.
4646
*
47-
* @param ObjectManager $om
47+
* @param EntityManagerInterface $em
4848
* @param MappingManager $mappingManager
4949
* @param EventDispatcherInterface $eventDispatcher
5050
* @param StorageInterface $tmpStorage
5151
* @param string|null $tmpUploadedFileClass
5252
*/
5353
public function __construct(
54-
ObjectManager $om,
54+
EntityManagerInterface $em,
5555
MappingManager $mappingManager,
5656
EventDispatcherInterface $eventDispatcher,
5757
StorageInterface $tmpStorage,
5858
$tmpUploadedFileClass = null
5959
) {
60-
$this->om = $om;
60+
$this->em = $em;
6161
$this->mappingManager = $mappingManager;
6262
$this->eventDispatcher = $eventDispatcher;
6363
$this->tmpStorage = $tmpStorage;
@@ -87,7 +87,7 @@ public function upload(UploadedFile $uploadedFile, $type, $id = null)
8787
if ($id === null) {
8888
$subject = new $entityClass();
8989
} else {
90-
$subject = $this->om->getRepository($entityClass)->find($id);
90+
$subject = $this->em->getRepository($entityClass)->find($id);
9191
if (null === $subject) {
9292
throw new \Exception(sprintf('Cannot find object of type "%s" with id %s.', $type, $id));
9393
}
@@ -108,8 +108,8 @@ public function upload(UploadedFile $uploadedFile, $type, $id = null)
108108
$handler->upload($subject, $mapping->fileProperty, $uploadedFile);
109109
$this->eventDispatcher->dispatch('afb.post_upload', new UploadEvent($subject, $mapping, $uploadedFile));
110110

111-
$this->om->persist($subject);
112-
$this->om->flush();
111+
$this->em->persist($subject);
112+
$this->em->flush();
113113

114114
return $subject;
115115
}
@@ -135,8 +135,8 @@ public function uploadTemporary(UploadedFile $uploadedFile)
135135
$obj->setToken(rand());
136136
$obj->setFilename($uploadedFile->getClientOriginalName());
137137

138-
$this->om->persist($obj);
139-
$this->om->flush($obj);
138+
$this->em->persist($obj);
139+
$this->em->flush($obj);
140140

141141
return $obj;
142142
}
@@ -147,8 +147,8 @@ public function uploadTemporary(UploadedFile $uploadedFile)
147147
public function removeTemporary(TemporaryUploadedFileInterface $fileInfo)
148148
{
149149
$this->tmpStorage->remove($fileInfo->getKey());
150-
$this->om->remove($fileInfo);
151-
$this->om->flush($fileInfo);
150+
$this->em->remove($fileInfo);
151+
$this->em->flush($fileInfo);
152152
}
153153

154154
/**
@@ -162,7 +162,7 @@ public function remove($type, $id, $deleteObject = false)
162162
{
163163
if (null !== $type && null !== $id) {
164164
$mapping = $this->mappingManager->getMapping($type);
165-
$subject = $this->om->getRepository($mapping->fileClass)->find($id);
165+
$subject = $this->em->getRepository($mapping->fileClass)->find($id);
166166
if (null === $subject) {
167167
throw new \Exception(sprintf('Cannot find object of type "%s" with id %s.', $type, $id));
168168
}
@@ -171,9 +171,9 @@ public function remove($type, $id, $deleteObject = false)
171171
$handler->remove($subject, $field);
172172
$this->eventDispatcher->dispatch('afb.post_remove_upload', new RemoveUploadEvent($subject, $mapping));
173173
if ($deleteObject || $mapping->multiple) {
174-
$this->om->remove($subject);
174+
$this->em->remove($subject);
175175
}
176-
$this->om->flush();
176+
$this->em->flush();
177177
}
178178
}
179179

Resources/doc/multiple_files_upload.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,13 @@ namespace AppBundle\Controller;
240240

241241
use App\Entity\Product;
242242
use AppBundle\Form\ProductType;
243-
use Doctrine\Common\Persistence\ObjectManager;
243+
use Doctrine\ORM\EntityManagerInterface;
244244
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
245245
use Symfony\Component\HttpFoundation\Request;
246246

247247
class ProductController extends Controller
248248
{
249-
public function indexAction(Request $request, ObjectManager $om, Product $product = null)
249+
public function indexAction(Request $request, EntityManagerInterface $em, Product $product = null)
250250
{
251251
if (!$product instanceof Product) {
252252
$product = new Product();
@@ -255,8 +255,8 @@ class ProductController extends Controller
255255
$form = $this->createForm(ProductType::class, $product);
256256
$form->handleRequest($request);
257257
if ($form->isSubmitted() && $form->isValid()) {
258-
$om->persist($product);
259-
$om->flush();
258+
$em->persist($product);
259+
$em->flush();
260260

261261
return $this->redirectToRoute('product_form', ['product' => $product->getId()]);
262262
}

Resources/doc/single_file_upload.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ namespace AppBundle\Controller;
217217

218218
use App\Entity\People;
219219
use AppBundle\Form\PeopleType;
220-
use Doctrine\Common\Persistence\ObjectManager;
220+
use Doctrine\ORM\EntityManagerInterface;
221221
use Symfony\Component\Routing\Annotation\Route;
222222
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
223223
use Symfony\Component\HttpFoundation\Request;
@@ -227,7 +227,7 @@ class PeopleController extends Controller
227227
/**
228228
* @Route("/people/{people}", name="people_form")
229229
*/
230-
public function indexAction(Request $request, ObjectManager $om, People $people = null)
230+
public function indexAction(Request $request, EntityManagerInterface $em, People $people = null)
231231
{
232232
if (!$people instanceof People) {
233233
$people = new People();
@@ -236,8 +236,8 @@ class PeopleController extends Controller
236236
$form = $this->createForm(SimpleImageType::class, $people);
237237
$form->handleRequest($request);
238238
if ($form->isSubmitted() && $form->isValid()) {
239-
$om->persist($people);
240-
$om->flush();
239+
$em->persist($people);
240+
$em->flush();
241241

242242
return $this->redirectToRoute('people_form', ['people' => $people->getId()]);
243243
}

0 commit comments

Comments
 (0)