简体   繁体   English

来自PHTML的Magento SQL查询

[英]Magento sql query from phtml

I wanted to know how to run sql queries in magento via the phtml files. 我想知道如何通过phtml文件在magento中运行sql查询。 The query itself is quite complicated and will require joins. 查询本身非常复杂,需要连接。 Any ideas? 有任何想法吗?

For you information: 供您参考:

I have products which have 3 custom attributes( Towns, Regions, Cities). 我的产品具有3个自定义属性(城镇,地区,城市)。 Now what I am trying to achieve is a list of towns from AZ in an alphabet order. 现在,我想要实现的是按字母顺序排列的AZ城镇列表。 When clicked on this town, it filters the product associated with the town. 单击此镇时,它将过滤与镇相关的产品。

Hope it makes sense. 希望有道理。

Any help is much appreciated. 任何帮助深表感谢。

Julian 朱利安

The phtml file is technically nothing else than a usual php file, so you can use all models, ressources and so on. 从技术上讲,phtml文件不是普通的php文件,因此您可以使用所有模型,资源等。 So you could just go with: 因此,您可以选择:

$resource = Mage::getSingleton('core/resource'); 
$readConnection = $resource->getConnection('core_read'); 
$query = 'SELECT * FROM ' . $resource->getTableName('catalog/product');
$results = $readConnection->fetchAll($query);

BUT the phtml file is a view, so you should not work directly on database there. 但是phtml文件是一个视图,因此您不应在那里直接在数据库上工作。 Instead try to create a model, use that in the block and then call the prepared data from the block object. 而是尝试创建模型,在模块中使用该模型,然后从模块对象中调用准备好的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM