database - Magento Varien_Data_Collections: when is SQL query executed -
i`d know how magento internally processes database calls on varien_data_collections. example: 1 of models returns data collection block. additional clauses added via layout xml blocks , afterwards appended returned data collection te model.
does mean database calls made twice? 1 on initial $model->getcollection()
method , 1 on $collection->addfieldtofilter('x',array('eq'=>'y'))
in block.
or magento make sql call on iteraror?
see situation below:
model class:
<?php class x_y_model_a extends mage_core_model_abstract{ ... public function getrelateditems(){ return $this->getcollection() ->addfieldtofilter('id',array('eq'=>$this->getid())); } } ?>
block class:
<?php class x_y_block_a extends mage_core_block_template{ private $limit = 5; public function setdblimit($limit){ $this->limit = (int)$limit; return $this; } public function getdblimit(){ return $this->limit; } public function getrelateditems(){ $_collection = mage::getmodel('xy/a')->getrelateditems(); $_collection->setpagesize($this->getdblimit()); return $_collection; } } ?>
layout xml file
<layout> ... <reference name="content"> <block type="xy/a" name="xy" as="xy" template="xy/related.phtml"> <action method="setdblimit"><limit>2<limit></action> </block> </reference> </layout>
view .phtml
<?php // xy/related.phtml $_relateditems = $this->getrelateditems(); if($_relateditems->getsize()>0){ foreach($_relateditems $_relateditem){ // html output } } ?>
magento uses lazy-loading , performs query when needs data resultset. long don't use result, can keep adding conditionals or filters without executing query.
Comments
Post a Comment