1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Command; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Sonata\Doctrine\Model\ManagerInterface; |
18
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @final since sonata-project/media-bundle 3.21.0 |
25
|
|
|
*/ |
26
|
|
|
class MigrateToJsonTypeCommand extends BaseCommand |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var EntityManagerInterface |
30
|
|
|
*/ |
31
|
|
|
private $entityManager; |
32
|
|
|
|
33
|
|
|
public function __construct(ManagerInterface $mediaManager, Pool $pool, ?EntityManagerInterface $entityManager = null) |
34
|
|
|
{ |
35
|
|
|
parent::__construct($mediaManager, $pool); |
36
|
|
|
|
37
|
|
|
$this->entityManager = $entityManager; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function configure(): void |
41
|
|
|
{ |
42
|
|
|
$this->setName('sonata:media:migrate-json'); |
43
|
|
|
$this->addOption('table', null, InputOption::VALUE_OPTIONAL, 'Media table', 'media__media'); |
44
|
|
|
$this->addOption('column', null, InputOption::VALUE_OPTIONAL, 'Column name for provider_metadata', 'provider_metadata'); |
45
|
|
|
$this->addOption('column_id', null, InputOption::VALUE_OPTIONAL, 'Column name for id', 'id'); |
46
|
|
|
$this->setDescription('Migrate all media provider metadata to the doctrine JsonType'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int |
50
|
|
|
{ |
51
|
|
|
if (null === $this->entityManager) { |
52
|
|
|
throw new \LogicException( |
53
|
|
|
'This command could not be executed since one of its dependencies is missing.' |
54
|
|
|
.' Is the service "doctrine.orm.entity_manager" available?' |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$count = 0; |
59
|
|
|
$table = $input->getOption('table'); |
60
|
|
|
$column = $input->getOption('column'); |
61
|
|
|
$columnId = $input->getOption('column_id'); |
62
|
|
|
$medias = $this->entityManager->getConnection()->fetchAll("SELECT * FROM $table"); |
63
|
|
|
|
64
|
|
|
foreach ($medias as $media) { |
65
|
|
|
// if the row need to migrate |
66
|
|
|
if (0 !== strpos($media[$column], '{') && '[]' !== $media[$column]) { |
67
|
|
|
$media[$column] = json_encode(unserialize($media[$column])); |
68
|
|
|
$this->entityManager->getConnection()->update($table, [$column => $media[$column]], [$columnId => $media[$columnId]]); |
69
|
|
|
++$count; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$output->writeln("Migrated $count medias"); |
74
|
|
|
|
75
|
|
|
return 0; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.