Following this tutorial I've implemented a dynamic key => value pair setting in my magento adminhtml backend module config.
So, this is my system.xml
<shipping_method_mapping translate="label">
<label>Shipping Method Mapping</label>
<comment><![CDATA[Select an active shipping method in magento and map it to the code used by Asendia.]]></comment>
<frontend_model>infiniteconnect/fieldasendia</frontend_model>
<backend_model>adminhtml/system_config_backend_serialized_array</backend_model>
<source_model>infiniteconnect_adminhtml/system_config_source_dropdown_activeshippingmethods</source_model>
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</shipping_method_mapping>
and this is my Activeshippingmethods.php (dropdown source model):
class Intellibi_InfiniteConnect_Adminhtml_Model_System_Config_Source_Dropdown_Activeshippingmethods
{
public function toOptionArray()
{
// Init Option Array
$optionArray = array();
// Load Magento DB Connection
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
// Query Magento DB
$active_shipping_methods = $readConnection->fetchAll(
"SELECT DISTINCT `shipping_description` AS `name` FROM ". $resource->getTableName('sales_flat_order'));
// Build Option Array
foreach ($active_shipping_methods as $active_shipping_method) {
$optionArray[] = array(
'value' => md5($active_shipping_method['name']),
'label' => Mage::helper('infiniteconnect')->__($active_shipping_method['name'])
);
}
// Finished
return $optionArray;
}
}
When I test this locally on my wamp installation; it works great:
enter image description here
However, when I upload this to my remote DEV server running LEMP stack, the behaviour changes and the config screen breaks/does not render correctly like this:
enter image description here
If I modify my Activeshippingmethods.php dropdown source model and comment out all the direct query stuff and return an empty array, then the config screen doesn't break and renders correctly - so this is why I thought there's a problem with the direct query...
I've already checked if my remote linux server has entries in sales_flat_order table and it does (58 distinct shipping_description).
Any idea why this isn't working correctly on a linux server, but it's fine on the WAMP server for some reason? Is this some sort of issue with my module not having the correct ACL to use core_read connection?
1 Answer 1
I've solved the problem by updating my drop down source model like this:
public function toOptionArray()
{
// Init Option Array
$optionArray = array();
// Query Magento & Build Option Array
$collection = Mage::getModel('sales/order')->getCollection();
$collection->addAttributeToSelect('shipping_description');
$collection->getSelect()->distinct(true);
foreach ($collection as $order) {
$optionArray[] = array(
'value' => md5($order->getShippingDescription()),
'label' => str_replace("'", "\'", $order->getShippingDescription())
);
}
// Finished
return $optionArray;
}
The selection options were used inside javascript, which broke when any "labels" have a single quote in them. So I had to explicitly escape it...
-
I'd recommend using
Mage::helper('core')->jsQuoteEscape()instead of str_replace (see How to escape output). It does the same but is more explicit. Also, feel free to accept your own answer.Fabian Schmengler– Fabian Schmengler2015年02月20日 09:59:52 +00:00Commented Feb 20, 2015 at 9:59
var/logfolder and both empty. I've also checked mynginxandphp-fpmlogs and nothing found nothing in there related to this error my module.shipping_descriptionfromsales_flat_ordertable.