Created
March 19, 2025 15:50
-
-
Save wujku/d8c0fd74b048ed25440e20f47731f3e2 to your computer and use it in GitHub Desktop.
Create FULLTEXT index for localized column – Pimcore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\EventListener\ClassDefinition; | |
use Exception; | |
use Pimcore\Db; | |
use Pimcore\Event\Model\DataObject\ClassDefinitionEvent; | |
use Pimcore\Logger; | |
use Pimcore\Tool; | |
class FulltextIndexForTechnicalDescription | |
{ | |
public function createIndexes(ClassDefinitionEvent $event): void | |
{ | |
$class = $event->getClassDefinition(); | |
if ($class->getId() != 1) { | |
return; | |
} | |
$db = Db::get(); | |
$languages = Tool::getValidLanguages(); | |
foreach ($languages as $language) { | |
$tableName = 'object_localized_query_1_' . $language; | |
$tableExists = $db->fetchOne("SHOW TABLES LIKE '$tableName'"); | |
if (! $tableExists) { | |
continue; | |
} | |
$columnExists = $db->fetchOne("SHOW COLUMNS FROM `$tableName` LIKE 'technicalDescription'"); | |
if (! $columnExists) { | |
continue; | |
} | |
$indexExists = $db->fetchOne("SHOW INDEX FROM `$tableName` WHERE Key_name = 'ft_technicalDescription'"); | |
if ($indexExists) { | |
continue; | |
} | |
try { | |
$db->query( | |
"ALTER TABLE `$tableName` ADD FULLTEXT INDEX `ft_technicalDescription` (`technicalDescription`)" | |
); | |
} catch (Exception $e) { | |
Logger::error( | |
'Error during create a FULLTEXT index for the table ' . $tableName . ': ' . $e->getMessage() | |
); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
AppBundle\EventListener\ClassDefinition\FulltextIndexForTechnicalDescription: | |
tags: | |
- { name: kernel.event_listener, event: pimcore.class.postUpdate, method: createIndexes } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment