在Magento 1.9.2社区版中为自定义扩展创建属性时面临的问题

时间:2016-03-31 10:10:51

标签: php magento magento-1.9 entity-attribute-value

我正在开发扩展程序,我正在维护扩展程序的自定义属性。

我的安装程序和安装文件工作正常,我的atrribute正在插入eav_attribute表,但我遇到的唯一问题是我的custom_abc_eav_attribute值中某些字段没有插入。

这是我的setup.php文件

class Custom_ABC_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup
{
  public function getDefaultEntities()
  {
    $entities = array();
    $entities['custom_abc_user'] = array(
        'entity_model'                  => 'custom_abc/user',
        'attribute_model'               => 'custom_abc/resource_eav_attribute',
        'table'                         => 'custom_abc/user',
        'additional_attribute_table'    => 'custom_abc/eav_attribute',
        'entity_attribute_collection'   => 'custom_abc/user_attribute_collection',
        'attributes'                    => array(
                'user_name' => array(
                    'group'          => 'General',
                    'type'           => 'varchar',
                    'backend'        => '',
                    'frontend'       => '',
                    'label'          => 'Name',
                    'input'          => 'text',
                    'source'         => '',
                    'global'         => Custom_ABC_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
                    'required'       => '1',
                    'user_defined'   => false,
                    'default'        => '',
                    'unique'         => false,
                    'position'       => '10',
                    'note'           => '',
                    'visible'        => '1',
                    'wysiwyg_enabled'=> '0',
                ),
                'user_status' => array(
                    'group'          => 'General',
                    'type'           => 'int',
                    'backend'        => '',
                    'frontend'       => '',
                    'label'          => 'User Status',
                    'input'          => 'select',
                    'source'         => 'eav/entity_attribute_source_boolean',
                    'global'         => Custom_ABC_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
                    'required'       => '1',
                    'user_defined'   => true,
                    'default'        => '',
                    'unique'         => false,
                    'position'       => '20',
                    'note'           => '',
                    'visible'        => '1',
                    'wysiwyg_enabled'=> '0',
                ),
            )
     );

    return $entities;
  }
}

这是我的安装程序文件install-0.1.0.php

$this->startSetup();
$table = $this->getConnection()
->newTable($this->getTable('custom_abc/user'))
->addColumn(
    'entity_id',
    Varien_Db_Ddl_Table::TYPE_INTEGER,
    null,
    array(
        'identity'  => true,
        'unsigned'  => true,
        'nullable'  => false,
        'primary'   => true,
    ),
    'Entity ID'
)
->addColumn(
    'entity_type_id',
    Varien_Db_Ddl_Table::TYPE_SMALLINT,
    null,
    array(
        'unsigned'  => true,
        'nullable'  => false,
        'default'   => '0',
    ),
    'Entity Type ID'
)
->addColumn(
    'attribute_set_id',
    Varien_Db_Ddl_Table::TYPE_SMALLINT,
    null,
    array(
        'unsigned'  => true,
        'nullable'  => false,
        'default'   => '0',
    ),
    'Attribute Set ID'
)

->addColumn(
    'created_at',
    Varien_Db_Ddl_Table::TYPE_TIMESTAMP,
    null, array(),
    'Creation Time'
)
->addColumn(
    'updated_at',
    Varien_Db_Ddl_Table::TYPE_TIMESTAMP,
    null,
    array(),
    'Update Time'
)
->addIndex(
    $this->getIdxName(
        'custom_abc/user',
        array('entity_type_id')
    ),
    array('entity_type_id')
)
->addIndex(
    $this->getIdxName(
        'custom_abc/user',
        array('attribute_set_id')
    ),
    array('attribute_set_id')
)
->addForeignKey(
    $this->getFkName(
        'custom_abc/user',
        'attribute_set_id',
        'eav/attribute_set',
        'attribute_set_id'
    ),
    'attribute_set_id',
    $this->getTable('eav/attribute_set'),
    'attribute_set_id',
    Varien_Db_Ddl_Table::ACTION_CASCADE,
    Varien_Db_Ddl_Table::ACTION_CASCADE
)
->addForeignKey(
    $this->getFkName(
        'custom_abc/user',
        'entity_type_id',
        'eav/entity_type',
        'entity_type_id'
    ),
    'entity_type_id',
    $this->getTable('eav/entity_type'),
    'entity_type_id',
    Varien_Db_Ddl_Table::ACTION_CASCADE,
    Varien_Db_Ddl_Table::ACTION_CASCADE
)
->setComment('User Table');
$this->getConnection()->createTable($table);

$userEav = array();
$userEav['int'] = array(
'type'      => Varien_Db_Ddl_Table::TYPE_INTEGER,
'length'    => null,
'comment'   => 'User Datetime Attribute Backend Table'
);

$userEav['varchar'] = array(
'type'      => Varien_Db_Ddl_Table::TYPE_TEXT,
'length'    => 255,
'comment'   => 'User Varchar Attribute Backend Table'
);

$userEav['text'] = array(
'type'      => Varien_Db_Ddl_Table::TYPE_TEXT,
'length'    => '64k',
'comment'   => 'User Text Attribute Backend Table'
);

$userEav['datetime'] = array(
'type'      => Varien_Db_Ddl_Table::TYPE_DATETIME,
'length'    => null,
'comment'   => 'User Datetime Attribute Backend Table'
);

$userEav['decimal'] = array(
'type'      => Varien_Db_Ddl_Table::TYPE_DECIMAL,
'length'    => '12,4',
'comment'   => 'user Datetime Attribute Backend Table'
);

foreach ($UserEav as $type => $options) {
$table = $this->getConnection()
    ->newTable($this->getTable(array('custom_abc/user', $type)))
    ->addColumn(
        'value_id',
        Varien_Db_Ddl_Table::TYPE_INTEGER,
        null,
        array(
            'identity'  => true,
            'nullable'  => false,
            'primary'   => true,
        ),
        'Value ID'
    )
    ->addColumn(
        'entity_type_id',
        Varien_Db_Ddl_Table::TYPE_SMALLINT,
        null,
        array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
        ),
        'Entity Type ID'
    )
    ->addColumn(
        'attribute_id',
        Varien_Db_Ddl_Table::TYPE_SMALLINT,
        null,
        array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
        ),
        'Attribute ID'
    )
    ->addColumn(
        'store_id',
        Varien_Db_Ddl_Table::TYPE_SMALLINT,
        null,
        array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
        ),
        'Store ID'
    )
    ->addColumn(
        'entity_id',
        Varien_Db_Ddl_Table::TYPE_INTEGER,
        null,
        array(
            'unsigned'  => true,
            'nullable'  => false,
            'default'   => '0',
        ),
        'Entity ID'
    )
    ->addColumn(
        'value',
        $options['type'],
        $options['length'], array(),
        'Value'
    )
    ->addIndex(
        $this->getIdxName(
            array('custom_abc/user', $type),
            array('entity_id', 'attribute_id', 'store_id'),
            Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE
        ),
        array('entity_id', 'attribute_id', 'store_id'),
        array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE)
    )
    ->addIndex(
        $this->getIdxName(
            array('custom_abc/user', $type),
            array('store_id')
        ),
        array('store_id')
    )
    ->addIndex(
        $this->getIdxName(
            array('custom_abc/user', $type),
            array('entity_id')
        ),
        array('entity_id')
    )
    ->addIndex(
        $this->getIdxName(
            array('custom_abc/user', $type),
            array('attribute_id')
        ),
        array('attribute_id')
    )
    ->addForeignKey(
        $this->getFkName(
            array('custom_abc/user', $type),
            'attribute_id',
            'eav/attribute',
            'attribute_id'
        ),
        'attribute_id',
        $this->getTable('eav/attribute'),
        'attribute_id',
        Varien_Db_Ddl_Table::ACTION_CASCADE,
        Varien_Db_Ddl_Table::ACTION_CASCADE
    )
    ->addForeignKey(
        $this->getFkName(
            array('custom_abc/user', $type),
            'entity_id',
            'custom_abc/user',
            'entity_id'
        ),
        'entity_id',
        $this->getTable('custom_abc/user'),
        'entity_id',
        Varien_Db_Ddl_Table::ACTION_CASCADE,
        Varien_Db_Ddl_Table::ACTION_CASCADE
    )
    ->addForeignKey(
        $this->getFkName(
            array('custom_abc/user', $type),
            'store_id',
            'core/store',
            'store_id'
        ),
        'store_id',
        $this->getTable('core/store'),
        'store_id',
        Varien_Db_Ddl_Table::ACTION_CASCADE,
        Varien_Db_Ddl_Table::ACTION_CASCADE
    )
    ->setComment($options['comment']);
$this->getConnection()->createTable($table);
}
$table = $this->getConnection()
->newTable($this->getTable('custom_abc/eav_attribute'))
->addColumn(
    'attribute_id',
    Varien_Db_Ddl_Table::TYPE_INTEGER,
    null,
    array(
        'identity'  => true,
        'nullable'  => false,
        'primary'   => true,
    ),
    'Attribute ID'
)
->addColumn(
    'is_global',
    Varien_Db_Ddl_Table::TYPE_INTEGER,
    null,
    array(),
    'Attribute scope'
)
->addColumn(
    'position',
    Varien_Db_Ddl_Table::TYPE_INTEGER,
    null,
    array(),
    'Attribute position'
)
->addColumn(
    'is_wysiwyg_enabled',
    Varien_Db_Ddl_Table::TYPE_INTEGER,
    null,
    array(),
    'Attribute uses WYSIWYG'
)
->addColumn(
    'is_visible',
    Varien_Db_Ddl_Table::TYPE_INTEGER,
    null,
    array(),
    'Attribute is visible'
)
->setComment('ABC attribute table');
$this->getConnection()->createTable($table);

$this->installEntities();

$this->endSetup();

它没有抛出任何错误,这就是它在数据库中保存的方式

eav_attribute表的屏幕截图 enter image description here

custom_abc_eav_attribute表的屏幕截图 enter image description here

所以主要问题是位置is_wysiwyg_enabled,is_visible正在插入空值。

任何帮助都会很明显

1 个答案:

答案 0 :(得分:0)

我得到的解决方案基本上我在setup.php中错过了_prepareValues()函数

protected function _prepareValues($attr)
{
    $data = parent::_prepareValues($attr);
    $data = array_merge($data, array(
        'frontend_input_renderer'       => $this->_getValue($attr, 'input_renderer'),
        'is_global'                     => $this->_getValue(
            $attr,
            'global',
            Custom_ABC_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
        ),
        'is_visible'                    => $this->_getValue($attr, 'visible', 1),
        'is_wysiwyg_enabled'            => $this->_getValue($attr, 'wysiwyg_enabled', 0),
        'position'                      => $this->_getValue($attr, 'position', 0)
    ));
    return $data;
}