OSd*_*ave 4 php mysql oop magento
我正在研究magento 1.3.2.3版的第一个模块.我创建了一个简单的表(不是EAV,只是一个主键和2列)和一些访问它的类,遵循Alan Storm的文章对我有很多帮助,但我无法弄清楚如何进行简单的选择:Alan解释了如何使用主键加载,但不选择与某个值匹配的行.
在正常的MySQL我写道:
SELECT *
FROM my_table
WHERE some_field = '" . $someValue . "'
Run Code Online (Sandbox Code Playgroud)
我找到了一个片段,它给了我想要的结果:
$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);
Run Code Online (Sandbox Code Playgroud)
但是必须有一个更简单/更漂亮的解决方案,使用我创建的模型类.结果将是单行,而不是集合.
我已经尝试了我能想到的一切,比如:
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));
Run Code Online (Sandbox Code Playgroud)
还有更多的东西,但到目前为止没有运气:我错过了什么?
你可能想要使用你的模型的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());
Run Code Online (Sandbox Code Playgroud)
以下是CoreUrlRewrite Model类中如何实现此示例的示例:
public function loadByIdPath($path)
{
$this->setId(null)->load($path, 'id_path');
return $this;
}
Run Code Online (Sandbox Code Playgroud)
您可以在模型类中创建类似的方法.您还可以在代码中的任何位置使用替代形式的加载方法:
$model = Mage::getModel('modulename/classname')->load($someValue, 'some_field');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17622 次 |
| 最近记录: |