0

I'm trying to create a customer EAV which is always either 0 or 1, never null. The main issue I'm having is forcing the column in the customer_grid_flat table to be not nullable.

$customerSetup->addAttribute(Customer::ENTITY, 'is_company', [
    'type' => 'int',
    'backend' => '',
    'frontend' => '',
    'label' => 'Is a company',
    'input' => 'boolean',
    'class' => '',
    'source' => 'Vendor\Module\Model\Entity\Attribute\Source\BooleanNotNull',
    'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
    'visible' => true,
    'required' => true,
    'user_defined' => false,
    'default' => '0',
    'searchable' => true,
    'filterable' => true,
    'comparable' => true,
    'visible_on_front' => true,
    'system' => false,
    'is_used_in_grid' => true,
    'is_visible_in_grid' => true,
    'is_filterable_in_grid' => true,
    'is_searchable_in_grid' => true,
    'unique' => false,
    'apply_to' => '',
]);

BooleanNotNull class is as follows. I've intentionally left in my debug.

class BooleanNotNull extends \Magento\Eav\Model\Entity\Attribute\Source\Boolean
{
    public function getFlatColumns()
    {
        var_dump('getFlatColumns');exit();

        $attributeCode = $this->getAttribute()->getAttributeCode();
        return [
            $attributeCode => [
                'unsigned' => false,
                'default' => 0,
                'extra' => null,
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                'length' => 1,
                'nullable' => false,
                'comment' => $attributeCode . ' column',
            ],
        ];
    }
}

I never see that debug message. It seems as though the getFlatColumns function, which is originally defined in AbstractSource, never gets called during the setup:upgrade or indexer:reindex commands, which are when the customer_grid_flat table is updated.

In fact, looking at Magento\Framework\Indexer\GridStructure which actually rebuilds the flat table, I see this:

foreach ($fields as $field) {
    if ($field['type'] === 'searchable') {
        $searchableFields[] = $field['name'];
    }
    $columnMap = isset($field['dataType']) && isset($this->columnTypesMap[$field['dataType']])
        ? $this->columnTypesMap[$field['dataType']]
        : ['type' => $field['dataType'], 'size' => isset($field['size']) ? $field['size'] : null];
    $name = $field['name'];
    $type = $columnMap['type'];
    $size = $columnMap['size'];
    if ($field['type'] === 'filterable') {
        $table->addIndex(
            $this->resource->getIdxName($tableName, $name, AdapterInterface::INDEX_TYPE_INDEX),
            $name,
            ['type' => AdapterInterface::INDEX_TYPE_INDEX]
        );
    }
    $table->addColumn($name, $type, $size);
}

That last line is the issue. $options isn't even passed into that function so there's no way to define it.

So my question is, how does one define the flat column specification to be used when rebuilding the customer flat table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.