在 Magento 1.9.2 社区版中为自定义扩展创建属性时面临的问题
Issue facing while creating attribute for custom extension in Magento 1.9.2 Community edition
我正在开发一个扩展,并且正在维护我的扩展的自定义属性。
我的安装程序和设置文件工作正常,我的属性正在插入 eav_attribute table 但我面临的唯一问题是我的 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的截图table
custom_abc_的屏幕截图eav_attribute table
所以主要问题是位置 is_wysiwyg_enabled、is_visible 正在插入空值。
任何帮助将不胜感激
我得到了解决方案,基本上我错过了 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;
}
我正在开发一个扩展,并且正在维护我的扩展的自定义属性。
我的安装程序和设置文件工作正常,我的属性正在插入 eav_attribute table 但我面临的唯一问题是我的 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的截图table
custom_abc_的屏幕截图eav_attribute table
所以主要问题是位置 is_wysiwyg_enabled、is_visible 正在插入空值。
任何帮助将不胜感激
我得到了解决方案,基本上我错过了 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;
}