Magento 在控制器中保存多选?
$fieldset->addField('brand_id', 'multiselect', array(
'label' => Mage::helper('expertbrand')->__('Merk:'),
'name' => 'brand_id[]',
'values' => $aOptionsMerk,
));
我有一个带有大约 600 个选项的多选框。我想知道如何将其保存在控制器中?
我已经尝试了一切,并为这个问题工作了三天。另外在互联网上我找不到这个问题的正确答案。希望这里有人能够帮助我,因为我真的很想知道!
我的控制器代码:
public function saveAction() {
if ($data = $this->getRequest()->getPost()) {
if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
try {
/* Starting upload */
$uploader = new Varien_File_Uploader('filename');
// Any extention would work
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
// Set the file upload mode
// false -> get the file directly in the specified folder
// true -> get the file in the product like folders
// (file.jpg will go in something like /media/f/i/file.jpg)
$uploader->setFilesDispersion(false);
// We set media as the upload dir
$path = Mage::getBaseDir('media') . DS ;
$uploader->save($path, $_FILES['filename']['name'] );
} catch (Exception $e) {
}
//this way the name is saved in DB
$data['filename'] = $_FILES['filename']['name'];
}
/*
$brands=$data['brand_id'];
$t=count($brands);
$inhoud="";
$i=1;
foreach ($brands as $brand){
if ($t == $i){
$inhoud.=$brand;
} else {
$inhoud.=$brand." , ";
}
$i++;
}
//echo $inhoud;
// $br=array('brand_id');
$br=$inhoud;
$data['brand_id']=$br;
*/
//hier moet de loop komen
$id= $data['expert_id'];
$db1 = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $db1->query("SELECT name FROM expert where expert_id=$id");
$rows = $result->fetch(PDO::FETCH_ASSOC);
$data['name']=$rows['name'];
//$data['brand_id']=$_POST['brand_id'];
$model = Mage::getModel('expertbrand/expertbrand');
$model->setData($data)
->setId($this->getRequest()->getParam('id'));
try {
if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) {
$model->setCreatedTime(now())
->setUpdateTime(now());
} else {
$model->setUpdateTime(now());
}
$model->save();
//hier is het einde van de loop..
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('expertbrand')->__('Item was successfully saved'));
Mage::getSingleton('adminhtml/session')->setFormData(false);
if ($this->getRequest()->getParam('back')) {
$this->_redirect('*/*/edit', array('id' => $model->getId()));
return;
}
$this->_redirect('*/*/');
return;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setFormData($data);
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
}
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('expertbrand')->__('Unable to find item to save'));
$this->_redirect('*/*/');
}
我应该如何保存通过 $_POST
发送的 optionsArray
?提前致谢。
$fieldset->addField('brand_id', 'multiselect', array(
'label' => Mage::helper('expertbrand')->__('Merk:'),
'name' => 'brand_id[]',
'values' => $aOptionsMerk,
));
I have this multiselect box with some 600 options. I would like to know how to save this in the controller?
I have tried everything and worked on this problem for 3 days now. Also on the internet I cannot find a correct anwser to this problem. Hoping someone here is able to help me because I'd really like to know!
My controller code:
public function saveAction() {
if ($data = $this->getRequest()->getPost()) {
if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
try {
/* Starting upload */
$uploader = new Varien_File_Uploader('filename');
// Any extention would work
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
// Set the file upload mode
// false -> get the file directly in the specified folder
// true -> get the file in the product like folders
// (file.jpg will go in something like /media/f/i/file.jpg)
$uploader->setFilesDispersion(false);
// We set media as the upload dir
$path = Mage::getBaseDir('media') . DS ;
$uploader->save($path, $_FILES['filename']['name'] );
} catch (Exception $e) {
}
//this way the name is saved in DB
$data['filename'] = $_FILES['filename']['name'];
}
/*
$brands=$data['brand_id'];
$t=count($brands);
$inhoud="";
$i=1;
foreach ($brands as $brand){
if ($t == $i){
$inhoud.=$brand;
} else {
$inhoud.=$brand." , ";
}
$i++;
}
//echo $inhoud;
// $br=array('brand_id');
$br=$inhoud;
$data['brand_id']=$br;
*/
//hier moet de loop komen
$id= $data['expert_id'];
$db1 = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $db1->query("SELECT name FROM expert where expert_id=$id");
$rows = $result->fetch(PDO::FETCH_ASSOC);
$data['name']=$rows['name'];
//$data['brand_id']=$_POST['brand_id'];
$model = Mage::getModel('expertbrand/expertbrand');
$model->setData($data)
->setId($this->getRequest()->getParam('id'));
try {
if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) {
$model->setCreatedTime(now())
->setUpdateTime(now());
} else {
$model->setUpdateTime(now());
}
$model->save();
//hier is het einde van de loop..
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('expertbrand')->__('Item was successfully saved'));
Mage::getSingleton('adminhtml/session')->setFormData(false);
if ($this->getRequest()->getParam('back')) {
$this->_redirect('*/*/edit', array('id' => $model->getId()));
return;
}
$this->_redirect('*/*/');
return;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setFormData($data);
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
}
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('expertbrand')->__('Unable to find item to save'));
$this->_redirect('*/*/');
}
How should I save the optionsArray
that is sent through the $_POST
? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其添加到控制器中的 saveAction 中以将数据转换为字符串:
Add this to your saveAction in your Controller to convert the data to a string:
首先:
您已将输入声明为“brand_id[]”,因此您正在查找
$data['brand_id']
,它应该是一个数组。在注释代码中,您似乎使用了该数据,但您当前的代码仅查找$data['expert_id']
,这似乎不是同一件事。即使您将该行更改为
$data['brand_id']
,请记住它是一个数组,因此您需要了解 即使您将该行控制器中有一堆不属于那里的逻辑。在 MVC 应用程序(其中 Magento 是一个典型示例)中,大部分 SQL 逻辑(以及创建时间/更新时间内容)属于模型(可能是
expertbrand/expertbrand
)。尚不清楚您如何定义
expertbrand/expertbrand
。这是EAV型号吗?它的定义是什么?这很可能是问题的根源,因为您必须告诉 Magento 您的 EAV 模型才能这样保存。请修复这些问题(并澄清模型代码),如有必要,我们可以进一步调试。
谢谢,
乔
To start:
You've declared the input as "brand_id[]", so you are looking for
$data['brand_id']
, which should be an array. In the commented code, you appear to use that data, but your current code only looks for$data['expert_id']
, which doesn't seem to be the same thing.Even if you change that line to
$data['brand_id']
, keep in mind that it is an array, so you'll need to be cognizant of that in queries.You've got a bunch of logic in the controller that doesn't belong there. In an MVC app (of which Magento is a canonical example), most of that SQL logic (as well as the createdtime/updatedtime stuff) belongs in the model (probably
expertbrand/expertbrand
).It's not clear how you defined
expertbrand/expertbrand
. Is this an EAV model? What is its definition? This is most likely the root of your problem, as you have to tell Magento about your EAV model for it to save like that.Please fix those things (and clarify on the model code) and we can debug further if necessary.
Thanks,
Joe
我很清楚数据是在数组中发送的。为了解决这个问题,我创建了重写数组的函数。
保存在数据库中的数据看起来像这样:
我这样做是因为我在某处读到这样做是必要的。
然而,当我打开编辑字段时,唯一显示:
selected=“selected”的是值为 108(最后一个)的品牌,
这是一个问题,因为我需要将所有 4 个品牌显示为选定的。
这与我保存此数据的方式有关...不确定数据是否应该保存为数组,
以通过 Expert_id 是其他内容而不是 在编辑中显示所有 selected="selected" 字段这里的问题我知道sql应该在其他地方,但由于我仍在学习magento,这是一个快速肮脏的方法来解决我之前遇到的问题...
我想知道的是如何存储数组以将所有值显示为编辑表单中的 selected="selected" 字段...Txs...
好吧,我解决了问题,数据应保存为
106,108,114,99
现在所有内容都在编辑字段中选择,奇怪的是,这里找不到任何内容在互联网上希望这对处理同样问题的其他人有帮助
its clear to me that the data is sent in an array. In order to fix that i created function that rewrites the array.
the DATA saved in the database would look something like this:
i do this cos i read somewhere that this was necessary to do so.
How ever when i open my edit field the only one which shows :
selected="selected" is the brand with the value 108 (the last one)
this is a problem because i need all 4 of them to be shown as selected.
Has this something to do in the way how i save this data ...not sure if the data should be saved as an array to show all selected="selected" fields in the edit
by the way the expert_id is something else and not an issue here i know the sql should be somewhere else but since i am still learning magento this was a quick a dirty method to fix a problem i had earlier...
all i want to know is how to store the array to show all values as an selected="selected" field in the edit form...Txs....
Ok i fixed the problem the data should be saved as
106,108,114,99
now everything is selected in the edit field strange nothing of this is to be found here on the internet hopefully it will be helpfull to other people dealing with the same problem