2

I have a requirement to save an Order Item attribute as a string up to 400 char. While it could be implemented using a text attribute, I would rather use a varchar(400). However, the _addFlatAttribute() method in Mage_Sales_Model_Resource_Setup hardcodes the length of varchar to 255. It could be changed after the fact by a setup script with DDL, but I'm wondering if there are likely to be downstream dependencies on VARCHAR being 255 char.

Any thoughts?

4

3 回答 3

2

鉴于实例中订单实体数据的规模不断扩大,我相信您会很好并且建议您这样做。我很好奇 Magento 的版本,因为 CE1.6+ 是第一个具有Mage_Sales_Model_Resource_Setup[link]类的版本。在 CE1.6 之前,该课程是Mage_Sales_Model_Mysql4_Setup[link]。这是一个重要的区别,因为这些类中的列定义方法不同,以适应 1.6+ 中与 DB 无关的方法

Mage_Sales_Model_Mysql4_Setup::_getAttributeColumnDefinition[链接]

protected function _getAttributeColumnDefinition($code, $data)
{
    $columnDefinition = '';
    $type   = isset($data['type']) ? $data['type'] : 'varchar';
    $req    = isset($data['required']) ? $data['required'] : false;

    switch ($type) {
        case 'int':
            $columnDefinition = 'int(10) unsigned';
            break;
        case 'decimal':
            $columnDefinition = 'decimal(12,4)';
            break;
        case 'text':
            $columnDefinition = 'text';
            break;
        case 'date':
            $columnDefinition = 'datetime';
            break;
        default:
            $columnDefinition = 'varchar(255)';
            break;
    }

    if ($req) {
        $columnDefinition.= ' NOT NULL';
    }
    return $columnDefinition;
}

Mage_Sales_Model_Resource_Setup::_getAttributeColumnDefinition[链接]

protected function _getAttributeColumnDefinition($code, $data)
{
    // Convert attribute type to column info
    $data['type'] = isset($data['type']) ? $data['type'] : 'varchar';
    $type = null;
    $length = null;
    switch ($data['type']) {
        case 'timestamp':
            $type = Varien_Db_Ddl_Table::TYPE_TIMESTAMP;
            break;
        case 'datetime':
            $type = Varien_Db_Ddl_Table::TYPE_DATETIME;
            break;
        case 'decimal':
            $type = Varien_Db_Ddl_Table::TYPE_DECIMAL;
            $length = '12,4';
            break;
        case 'int':
            $type = Varien_Db_Ddl_Table::TYPE_INTEGER;
            break;
        case 'text':
            $type = Varien_Db_Ddl_Table::TYPE_TEXT;
            $length = 65536;
            break;
        case 'char':
        case 'varchar':
            $type = Varien_Db_Ddl_Table::TYPE_TEXT;
            $length = 255;
            break;
    }
    if ($type !== null) {
        $data['type'] = $type;
        $data['length'] = $length;
    }

    $data['nullable'] = isset($data['required']) ? !$data['required'] : true;
    $data['comment']  = isset($data['comment']) ? $data['comment'] : ucwords(str_replace('_', ' ', $code));
    return $data;
}
于 2012-11-30T12:54:42.587 回答
1

使用类型 TEXT 而不是 VARCHAR。 Mage_Sales_Model_Resource_Setup在这种情况下假定长度为 64KB。

于 2012-11-30T13:19:02.097 回答
1

I can't imagine so, it might be different if you were to decrease the size of varchar, though.

See also: Is there a good reason I see VARCHAR(255) used so often (as opposed to another length)?

于 2012-11-30T01:33:33.567 回答