Skip to content

Commit

Permalink
Merge pull request mautic#7596 from d-code-ltd/multiselect-as-text
Browse files Browse the repository at this point in the history
Store multiselect values as text with maximum length of 65535
  • Loading branch information
dennisameling authored Mar 18, 2020
2 parents 0555e1f + 9c1e95a commit 46b1d07
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function (FormEvent $event) use ($alias, $type) {
case 'multiselect':
case 'boolean':
if ($type == 'multiselect') {
$constraints[] = new Length(['max' => 255]);
$constraints[] = new Length(['max' => 65535]);
}

$typeProperties = [
Expand Down Expand Up @@ -277,7 +277,7 @@ function (FormEvent $event) use ($alias, $type) {
break;
case 'multiselect':
if ($type == 'multiselect') {
$constraints[] = new Length(['max' => 255]);
$constraints[] = new Length(['max' => 65535]);
}
}

Expand Down
8 changes: 6 additions & 2 deletions app/bundles/LeadBundle/Model/FieldModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,7 @@ public static function getSchemaDefinition($alias, $type, $isUnique = false)
];
}

$schemaLength = null;
switch ($type) {
case 'datetime':
case 'date':
Expand All @@ -1033,22 +1034,25 @@ public static function getSchemaDefinition($alias, $type, $isUnique = false)
case 'email':
case 'lookup':
case 'select':
case 'multiselect':
case 'region':
case 'tel':
$schemaType = 'string';
break;
case 'text':
$schemaType = (strpos($alias, 'description') !== false) ? 'text' : 'string';
break;
case 'multiselect':
$schemaType = 'text';
$schemaLength = 65535;
break;
default:
$schemaType = 'text';
}

return [
'name' => $alias,
'type' => $schemaType,
'options' => ['notnull' => false],
'options' => ['notnull' => false, 'length' => $schemaLength],
];
}

Expand Down
77 changes: 77 additions & 0 deletions app/migrations/Version20190724110039.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Mautic\CoreBundle\Doctrine\AbstractMauticMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20190724110039 extends AbstractMauticMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// Change multiselect custom fields from VARCHAR(255) to TEXT
$qb = $this->connection->createQueryBuilder();
$qb->select('lf.alias')
->from($this->prefix.'lead_fields', 'lf')
->where(
$qb->expr()->eq('lf.type', $qb->expr()->literal('multiselect'))
);
$multiselectFields = $qb->execute()->fetchAll();

$leadsTable = $schema->getTable($this->prefix.'leads');

if (!empty($multiselectFields)) {
foreach ($multiselectFields as $key => $field) {
if ($leadsTable->hasColumn($field['alias'])) {
if ($leadsTable->hasIndex("{$field['alias']}_search")) {
$this->addSql("DROP INDEX `{$field['alias']}_search` ON `{$this->prefix}leads`");
}
$this->addSql("ALTER TABLE `{$this->prefix}leads` CHANGE `{$field['alias']}` `{$field['alias']}` TEXT");
}
}
}
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// Change multiselect custom fields from VARCHAR(255) to TEXT
$qb = $this->connection->createQueryBuilder();
$qb->select('lf.alias')
->from($this->prefix.'lead_fields', 'lf')
->where(
$qb->expr()->eq('lf.type', $qb->expr()->literal('multiselect'))
);
$multiselectFields = $qb->execute()->fetchAll();

$leadsTable = $schema->getTable($this->prefix.'leads');

if (!empty($multiselectFields)) {
foreach ($multiselectFields as $key => $field) {
if ($leadsTable->hasColumn($field['alias'])) {
$this->addSql("ALTER TABLE `{$this->prefix}leads` CHANGE `{$field['alias']}` `{$field['alias']}` VARCHAR(255)");
if (!$leadsTable->hasIndex("{$field['alias']}_search")) {
$this->addSql("CREATE INDEX `{$field['alias']}_search` ON `{$this->prefix}leads`(`{$field['alias']}`)");
}
}
}
}
}
}

0 comments on commit 46b1d07

Please sign in to comment.