我正在为 magento 版本 1.3.2.3 开发我的第一个模块。 我创建了一个简单的表(不是 EAV,只有一个主键和 2 列)和一些访问它的类,遵循 Alan Storm's articles这对我帮助很大,但我不知道如何进行简单的选择:Alan 解释了如何使用主键加载,但不选择与某些值匹配的行。
在普通的 MySQL 中,我会写:
SELECT *
FROM my_table
WHERE some_field = '" . $someValue . "'
我找到了一个片段,它给出了我想要的结果:
$resource = new Mage_Core_Model_Resource();
$read = $resource->getConnection('core_read');
$select = $read->select()
->from('my_table')
->where('some_field = ?', $someValue);
return $read->fetchAll($select);
但是必须有一个更简单/更漂亮的解决方案,使用我创建的模型类。结果将是单行,而不是集合。
我已经尝试了所有我能想到的,比如:
return Mage::getModel('modulename/classname')->select()->where('some_field = ?', $comeValue);
return Mage::getModel('modulename/classname')->load()->where('some_field = ?', $comeValue);
return Mage::getModel('modulename/classname')->load(array('some_field = ?', $comeValue));
还有更多东西,但到目前为止还没有运气:我错过了什么??
最佳答案
您可能想使用 your model's Collection为此。
$collection = Mage::getModel('mygroup/mymodel')->getCollection();
$collection->addFieldToFilter('some_field',$some_value);
foreach($collection as $item)
{
var_dump($item);
}
var_dump($collection->getFirstItem());
var_dump($collection->getLastItem());
关于php - Magento:从数据库中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3197239/