I'm trying to build a simple Magento Module that needs to connect to the database at the start of each page request. All tables need to be accessible to it. I've been pulling my hair out trying to understand how to do it. The best I could figure out was that I need to set this in the config.xml
file of my module, but exactly what that command is/how it may be used, i haven't been able to figure out.
我正在尝试构建一个简单的Magento模块,它需要在每个页面请求开始时连接到数据库。所有表都需要可访问。我一直在试着理解如何去做我的头发。我能想到的最好的是我需要在我的模块的config.xml文件中设置它,但确切地说该命令是什么/如何使用它,我一直无法弄清楚。
Can someone please guide me or show me how to accomplish this? If not, would it be too bad a practice to include a custom config.php
file which connects to the database manually?
有人可以指导我或告诉我如何做到这一点?如果没有,那么包含一个手动连接到数据库的自定义config.php文件会不会太糟糕?
2 个解决方案
#1
16
Are you trying to use a resource model or entity ? here is how you can query a table using raw SQL but you need to know the tablename.
您是否尝试使用资源模型或实体?以下是使用原始SQL查询表的方法,但您需要知道表名。
$w = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $w->query('select entity_id from catalog_product_entity');
if (!$result) {
return false;
}
$row = $result->fetch(PDO::FETCH_ASSOC);
if (!$row) {
return false;
}
When trying to all a model you can use
尝试使用所有模型时
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('entity_id', array('in' => array(1,2)));
$products->addAttributeToSelect('*');
$products->load();
foreach ($products as $_prod) {
var_dump($_prod->getData());
}
I use the second method for my custom modules and it has worked out fine for me , hope this answer helps :)
我使用第二种方法为我的自定义模块,它对我来说很好,希望这个答案有帮助:)
#2
0
What about objects that do not have addAttributeToFilter
? I've been trying for hours to load a role object by its name and nothing works. The only possible method is load, which would work perfectly if not for an unexplicable
那些没有addAttributeToFilter的对象呢?我一直在尝试按名称加载角色对象几个小时,但没有任何效果。唯一可能的方法是加载,如果不是无法解释的话,这将是完美的
if (!intval($value) && is_string($value)) {
$field = 'role_id';
}
in Mage_Admin_Model_Mysql4_Role::load
, which only makes it impossible to filter using a string value.
在Mage_Admin_Model_Mysql4_Role :: load中,只能使用字符串值进行过滤。
Magento has a Rube Goldberg
architecture...
Magento拥有Rube Goldberg建筑......
#1
16
Are you trying to use a resource model or entity ? here is how you can query a table using raw SQL but you need to know the tablename.
您是否尝试使用资源模型或实体?以下是使用原始SQL查询表的方法,但您需要知道表名。
$w = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $w->query('select entity_id from catalog_product_entity');
if (!$result) {
return false;
}
$row = $result->fetch(PDO::FETCH_ASSOC);
if (!$row) {
return false;
}
When trying to all a model you can use
尝试使用所有模型时
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('entity_id', array('in' => array(1,2)));
$products->addAttributeToSelect('*');
$products->load();
foreach ($products as $_prod) {
var_dump($_prod->getData());
}
I use the second method for my custom modules and it has worked out fine for me , hope this answer helps :)
我使用第二种方法为我的自定义模块,它对我来说很好,希望这个答案有帮助:)
#2
0
What about objects that do not have addAttributeToFilter
? I've been trying for hours to load a role object by its name and nothing works. The only possible method is load, which would work perfectly if not for an unexplicable
那些没有addAttributeToFilter的对象呢?我一直在尝试按名称加载角色对象几个小时,但没有任何效果。唯一可能的方法是加载,如果不是无法解释的话,这将是完美的
if (!intval($value) && is_string($value)) {
$field = 'role_id';
}
in Mage_Admin_Model_Mysql4_Role::load
, which only makes it impossible to filter using a string value.
在Mage_Admin_Model_Mysql4_Role :: load中,只能使用字符串值进行过滤。
Magento has a Rube Goldberg
architecture...
Magento拥有Rube Goldberg建筑......