我经常在源模型中看到两种不同的方法似乎做同样的事情:
class Mypackage_Mymodule_Model_Source_Generic {
/* I sometimes see this method */
public function getAllOptions() {}
/* And other times this method */
public function toOptionArray() {}
}
Run Code Online (Sandbox Code Playgroud)
根据我的经验,使用哪种方法名称没有押韵或理由; 它们似乎都返回相同的数据结构.
有什么我想念的吗?
源模型toOptionArray
和Varien_Data_Collection::toOptionArray
?之间是否存在语义链接?
我正在运行Magento 1.4,但也在1.7中验证了这个问题.
使用实例Varien_Data_Collection
提供了使用Varien_Data_Collection::removeItemByKey
.在我的情况下,我正在从集合中删除项目,然后尝试获取该集合的更新大小,如下所示:
$body=$this->getTable()->getBody();
echo $body->getHeight(); // Outputs 25
$body->deleteRow(1);
echo $body->getHeight(); // Still outputs 25
...
My_Model_Body_Class extends Mage_Core_Model_Abstract {
/* @var $_rows Varien_Data_Collection */
protected $_rows;
public function deleteRow($index) {
$this->_rows->removeItemByKey($index);
return $this;
}
public function getHeight() {
return $this->_rows->getSize();
}
}
...
Run Code Online (Sandbox Code Playgroud)
代码限制简洁.
因此,如果您调用我的deleteRow
方法,该项目实际上将从集合中删除,但后续调用以获取该集合的大小将始终返回原始计数.因此,如果我在集合中有25个项目,并删除1,那么getSize
对集合的调用将返回25.
我将其追溯到父类,在Varien_Data_Collection::getSize
:
/**
* Retrieve collection all items count
*
* @return int
*/
public function getSize()
{
$this->load();
if (is_null($this->_totalRecords)) { …
Run Code Online (Sandbox Code Playgroud)