Author: @christianbeeznestpull/4726/head
parent
27f9d965c7
commit
6e84e2adbe
@ -0,0 +1,31 @@ |
||||
<?php |
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
class Version20230204145530 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Session changes'; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
$table = $schema->getTable('session'); |
||||
if (false === $table->hasColumn('image_id')) { |
||||
$this->addSql("ALTER TABLE session ADD image_id BINARY(16) DEFAULT NULL COMMENT '(DC2Type:uuid)'"); |
||||
$this->addSql("ALTER TABLE session ADD CONSTRAINT FK_D044D5D43DA5256D FOREIGN KEY (image_id) REFERENCES asset (id) ON DELETE SET NULL"); |
||||
$this->addSql("CREATE INDEX IDX_D044D5D43DA5256D ON session (image_id)"); |
||||
} |
||||
} |
||||
|
||||
public function down(Schema $schema): void |
||||
{ |
||||
} |
||||
} |
@ -0,0 +1,85 @@ |
||||
<?php |
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Asset; |
||||
use Chamilo\CoreBundle\Entity\ExtraFieldValues; |
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
use ExtraField; |
||||
use Symfony\Component\HttpFoundation\File\UploadedFile; |
||||
|
||||
class Version20230204150030 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Move extrafield session image to asset'; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
$container = $this->getContainer(); |
||||
$doctrine = $container->get('doctrine'); |
||||
$em = $doctrine->getManager(); |
||||
|
||||
$kernel = $container->get('kernel'); |
||||
$rootPath = $kernel->getProjectDir(); |
||||
|
||||
$batchSize = self::BATCH_SIZE; |
||||
$counter = 1; |
||||
$dql = "SELECT v FROM Chamilo\CoreBundle\Entity\ExtraFieldValues v"; |
||||
$dql .= " JOIN v.field f"; |
||||
$dql .= " WHERE f.variable = :variable AND f.itemType = :itemType"; |
||||
$q = $em->createQuery($dql); |
||||
$q->setParameters([ |
||||
'variable' => 'image', |
||||
'itemType' => ExtraField::SESSION_FIELD_TYPE, |
||||
]); |
||||
|
||||
$sessionRepo = $container->get(Session::class); |
||||
/** @var ExtraFieldValues $item */ |
||||
foreach ($q->toIterable() as $item) { |
||||
$path = $item->getFieldValue(); |
||||
if (empty($path)) { |
||||
continue; |
||||
} |
||||
$filePath = $rootPath.'/app/upload/'.$path; |
||||
if ($this->fileExists($filePath)) { |
||||
$fileName = basename($path); |
||||
$mimeType = mime_content_type($filePath); |
||||
$file = new UploadedFile($filePath, $fileName, $mimeType, null, true); |
||||
$asset = (new Asset()) |
||||
->setCategory(Asset::SESSION) |
||||
->setTitle($fileName) |
||||
->setFile($file) |
||||
; |
||||
$em->persist($asset); |
||||
$em->flush(); |
||||
$item->setAsset($asset); |
||||
$em->persist($item); |
||||
|
||||
$sessionId = $item->getItemId(); |
||||
/** @var Session $session */ |
||||
$session = $sessionRepo->find($sessionId); |
||||
$session->setImage($asset); |
||||
$sessionRepo->update($session); |
||||
} |
||||
|
||||
if (($counter % $batchSize) === 0) { |
||||
$em->flush(); |
||||
$em->clear(); // Detaches all objects from Doctrine! |
||||
} |
||||
$counter++; |
||||
} |
||||
$em->flush(); |
||||
$em->clear(); |
||||
} |
||||
|
||||
public function down(Schema $schema): void |
||||
{ |
||||
} |
||||
} |
Loading…
Reference in new issue