magento多选插入数据库 [英] magento multiselect insert database
问题描述
我想创建在Magento的管理员部分多选属性下拉,这样我可以插入属性以序列化方式的值到数据库中.
I want to create a multi-select attribute dropdown in magento's administrator section, so that I can insert the attributes values in a serialized manner into database.
请注意,该表,我想插入值是一个自定义的,即,我通过自己的表在数据库中创建. 我已经看到了多商店的代码,如下所示:
Kindly note that the table to which I want to insert values is a custom one ie, I have created by own table in database. I have seen the code for multi store as below:
if (!Mage::app()->isSingleStoreMode()) {
$fieldset->addField('store_id', 'multiselect', array(
'name' => 'stores[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
));
}
但是我无法使用这种格式,因为我的数据库表是自定义格式. 请让我知道交替代码,通过它我可以将多选属性的值DATABSE. 谢谢
but I am not able to use such format as my database table is custom one. Please let me know the alternate code through which I can insert values of multi select attribute to databse. thanks
推荐答案
由于Magento的有它自己的方式在DB我店中店IDS会建议以下这些标准.使用序列化值不是一个好主意...
As Magento has it's own way to store Store ids in the DB I would suggest following those standards. Using serialised values are not a very good idea...
YM =您的模块 YM_store是我们稍后将创建的新表. YM_entity_id是您要在此处使用的任何实体的ID. (例如,在核心/CMS/块, YM_entity_id" 是 BLOCK_ID".)
YM = Your module YM_store is the new table we will create later on. YM_entity_id is the id of whatever entity you want to use here. (Eg. in Core/cms/block, "YM_entity_id" is "block_id".)
请确保在任何地方使用相同的大写.
Please make sure to use the same capitalisation everywhere.
1)
您将需要创建一个新的表来存储ENTITY_ID<> STORE_ID映射.
You will need to create a new table to store the entity_id <> store_id map.
在您的mysql4升级-X.X.X-n.n.n.php文件(YM/SQL/YM_setup/)添加以下代码来创建一个新表:
In your mysql4-upgrade-x.x.x-n.n.n.php file (YM/sql/YM_setup/) add the following code to create a new table:
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
CREATE TABLE {$this->getTable('YM_store')} (
`YM_entity_id` smallint(6) NOT NULL default '0',
`store_id` smallint(5) NOT NULL default '0',
PRIMARY KEY (`YM_entity_id`,`store_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
2)
下一步是此表添加到模块配置: 下面的行添加到config.xml(YM的/etc/config.xml中)
Next step is to add this table to the module configuration: Add the following line to the config.xml (YM/etc/config.xml)
在最后一个表实体之后查找并添加:
Find and add this after the last table entities:
<YM_store>
<table>YM_store</table>
</YM_store>
确保将config.xml中的版本号更改为与mysql升级文件相同的版本.
Make sure to change the version number in config.xml to the same as your mysql upgrade file.
3)
下一步是下拉形式元素添加到你的form.php的 (YM/砌块/Adminhtml/YM/编辑/form.php的)
Next step is to add the dropdown form element to your Form.php (YM/Block/Adminhtml/YM/Edit/Form.php)
/**
* Check is single store mode
*/
if (!Mage::app()->isSingleStoreMode()) {
$fieldset->addField('store_id', 'multiselect', array(
'name' => 'stores[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
));
}
else {
$fieldset->addField('store_id', 'hidden', array(
'name' => 'stores[]',
'value' => Mage::app()->getStore(true)->getId()
));
$model->setStoreId(Mage::app()->getStore(true)->getId());
}
4)
下一步是让有关新表控制器知道: 在您的YMController.php文件(YM/控制器/Adminhtml/)你应该有它具有以下行中有一个saveAction功能:
Next step is to let the controller know about the new table: In your YMController.php file (YM/controllers/Adminhtml/) you should have a saveAction function which has the following line in it:
$model->setData($data);
这是一个通用的存储功能,这将确保从表单发送的所有数据的保存功能. (包括新创建的下拉的值)
This is a generic save function which will make sure to send all the data from the form to the save function. (Including the values of the newly created dropdown)
5)
下一步是将addStoreFilter函数添加到Collection.php(YM/Model/Mysql4/YM) 这部分代码是从cms/block Collection.php复制的.您将需要更改表字段名称. (YM_entity_id)
Next step is to add the addStoreFilter function to the Collection.php (YM/Model/Mysql4/YM) This bit of code is copied from cms/block Collection.php. You will need to change the table field names. (YM_entity_id)
/**
* Add Filter by store
*
* @param int|Mage_Core_Model_Store $store
* @return Mage_Cms_Model_Mysql4_Page_Collection
*/
public function addStoreFilter($store, $withAdmin = true)
{
if ($store instanceof Mage_Core_Model_Store) {
$store = array($store->getId());
}
$this->getSelect()->join(
array('store_table' => $this->getTable('YM/YM_store')),
'main_table.YM_entity_id = store_table.YM_entity_id',
array()
)
->where('store_table.store_id in (?)', ($withAdmin ? array(0, $store) : $store))
->group('main_table.YM_entity_id');
return $this;
}
6)
让我们添加商店网格为好. 在_prepareColumns()函数(YM/Block/Adminhtml/YM/Grid.php)中,在要显示存储的列之后添加以下行:
Let's add the Store to the Grid as well. In _prepareColumns() function (YM/Block/Adminhtml/YM/Grid.php) add the following lines after the column you want the stores to appear:
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('store_id', array(
'header' => Mage::helper('cms')->__('Store View'),
'index' => 'store_id',
'type' => 'store',
'store_all' => true,
'store_view' => true,
'sortable' => false,
'filter_condition_callback'
=> array($this, '_filterStoreCondition'),
));
}
和该功能添加到关闭之前}"的文件的末尾.
and add this function to the end of the file before the closing "}".
protected function _filterStoreCondition($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return;
}
$this->getCollection()->addStoreFilter($value);
}
请注意,此代码将仅在网格中列出所有商店视图"作为缺少的内容,我无法弄清楚是什么.也许别人会给我们答案...:)
Please note, that this code will only list "All store views" in the Grid as something missing and I couldn't figure out what. Maybe someone else will give us the answer... :)
7)
最后一步是将一些功能添加到YM.php所以这将是能够保存和加载store_ids(YM/型号/Mysql4/YM.php)以下函数从CMS/块复制
Last step is to add some functions to the YM.php so it will be able to save and load the store_ids (YM/Model/Mysql4/YM.php) The following functions are copied from cms/blocks.
BeforeSave:
BeforeSave:
/**
*
*
* @param Mage_Core_Model_Abstract $object
*/
protected function _beforeSave(Mage_Core_Model_Abstract $object)
{
if (! $object->getId()) {
$object->setCreationTime(Mage::getSingleton('core/date')->gmtDate());
}
$object->setUpdateTime(Mage::getSingleton('core/date')->gmtDate());
return $this;
}
AfterSave
AfterSave
/**
*
* @param Mage_Core_Model_Abstract $object
*/
protected function _afterSave(Mage_Core_Model_Abstract $object)
{
$condition = $this->_getWriteAdapter()->quoteInto('YM_entity_id = ?', $object->getId());
$this->_getWriteAdapter()->delete($this->getTable('YM/YM_store'), $condition);
foreach ((array)$object->getData('stores') as $store) {
$storeArray = array();
$storeArray['YM_entity_id'] = $object->getId();
$storeArray['store_id'] = $store;
$this->_getWriteAdapter()->insert($this->getTable('YM/YM_store'), $storeArray);
}
return parent::_afterSave($object);
}
加载.你可能没有标识符,所以我认为你不需要if语句.
Load. You probably don't have an identifier so I think you don't need the if statement.
public function load(Mage_Core_Model_Abstract $object, $value, $field=null)
{
if (!intval($value) && is_string($value)) {
$field = 'identifier'; // You probably don't have an identifier...
}
return parent::load($object, $value, $field);
}
AfterLoad
AfterLoad
/**
*
* @param Mage_Core_Model_Abstract $object
*/
protected function _afterLoad(Mage_Core_Model_Abstract $object)
{
$select = $this->_getReadAdapter()->select()
->from($this->getTable('YM/YM_store'))
->where('YM_entity_id = ?', $object->getId());
if ($data = $this->_getReadAdapter()->fetchAll($select)) {
$storesArray = array();
foreach ($data as $row) {
$storesArray[] = $row['store_id'];
}
$object->setData('store_id', $storesArray);
}
return parent::_afterLoad($object);
}
getLoadSelect
getLoadSelect
/**
* Retrieve select object for load object data
*
* @param string $field
* @param mixed $value
* @return Zend_Db_Select
*/
protected function _getLoadSelect($field, $value, $object)
{
$select = parent::_getLoadSelect($field, $value, $object);
if ($object->getStoreId()) {
$select->join(array('cbs' => $this->getTable('YM/YM_store')), $this->getMainTable().'.YM_entity_id = cbs.YM_entity_id')
->where('is_active=1 AND cbs.store_id in (0, ?) ', $object->getStoreId())
->order('store_id DESC')
->limit(1);
}
return $select;
}
lookupStoreIds
lookupStoreIds
/**
* Get store ids to which specified item is assigned
*
* @param int $id
* @return array
*/
public function lookupStoreIds($id)
{
return $this->_getReadAdapter()->fetchCol($this->_getReadAdapter()->select()
->from($this->getTable('YM/YM_store'), 'store_id')
->where("{$this->getIdFieldName()} = ?", $id)
);
}
好吧,如果我没有留下任何东西出来这应该工作. :) 登录到管理区,进入系统>高级.如果你设置了正确的版本,这应该运行MySQL升级.您可以检查,如果你有一个新表的数据库.
Ok, if I didn't leave out anything this should work. :) Log in to the Admin area, go to System > Advanced. This should run the mysql upgrade if you set the versions right. You can check if you have a new table the DB.
转到你的实体和检查,如果你有一个新的存储选择下拉菜单. 请尝试保存新的值...:)
Go to your entity and check if you have a new Store selection dropdown. Try saving the new values... :)
让我知道,如果它不工作.我可能会做的代码犯了一些错误.前面已经说过,我复制极本从CMS/块.同样如上所述,网格不会加载正确的价值,但我不知道为什么.有人可以告诉我们吗? :)
Let me know if it doesn't work. I might made a few mistakes in the code. As said before I copied most of this from cms/blocks. Also as stated above, the Grid doesn't load the right value but I don't know why. Anyone can tell us? :)
好运,盖尔盖伊
这篇关于magento多选插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!