Varien_Data_Collection Class Reference

Inheritance diagram for Varien_Data_Collection:

Mage_Adminhtml_Model_Extension_Collection_Abstract Mage_Backup_Model_Fs_Collection Mage_Reports_Model_Mysql4_Entity_Summary_Collection_Abstract Varien_Data_Collection_Db Varien_Directory_Collection Mage_Adminhtml_Model_Extension_Collection Mage_Adminhtml_Model_Extension_Local_Collection Mage_Adminhtml_Model_Extension_Remote_Collection Mage_Admin_Model_Mysql4_Acl_Role_Collection Mage_Core_Model_Mysql4_Collection_Abstract Mage_Core_Model_Mysql4_Email_Template_Collection Mage_Directory_Model_Mysql4_Country_Collection Mage_Directory_Model_Mysql4_Currency_Collection Mage_Directory_Model_Mysql4_Region_Collection Mage_Eav_Model_Entity_Collection_Abstract Mage_Log_Model_Mysql4_Visitor_Collection Mage_Newsletter_Model_Mysql4_Subscriber_Collection Mage_Newsletter_Model_Mysql4_Template_Collection Mage_Rating_Model_Mysql4_Rating_Option_Collection Mage_Review_Model_Mysql4_Review_Collection Mage_Review_Model_Mysql4_Review_Status_Collection Mage_Review_Model_Mysql4_Review_Summary_Collection Mage_Shipping_Model_Mysql4_Carrier_Tablerate_Collection Mage_Core_Model_Mysql4_Design_Theme

List of all members.

Public Member Functions

 __construct ()
 addFilter ($field, $value, $type= 'and')
 isLoaded ()
 getCurPage ($displacement=0)
 getLastPageNumber ()
 getPageSize ()
 getSize ()
 getFirstItem ()
 getLastItem ()
 getItems ()
 getColumnValues ($colName)
 getItemsByColumnValue ($column, $value)
 getItemByColumnValue ($column, $value)
 addItem (Varien_Object $item)
 removeItemByKey ($key)
 clear ()
 walk ($callback, array $args=array())
 each ($obj_method, $args=array())
 setDataToAll ($key, $value=null)
 setCurPage ($page)
 setPageSize ($size)
 setOrder ($field, $direction= 'desc')
 setItemObjectClass ($className)
 getNewEmptyItem ()
 distinct ($flag)
 loadData ($printQuery=false, $logQuery=false)
 load ($printQuery=false, $logQuery=false)
 toXml ()
 toArray ($arrRequiredFields=array())
 toOptionArray ()
 toOptionHash ()
 getItemById ($idValue)
 getIterator ()
 count ()
 setCacheKey ($key)
 getCacheKey ()
 setCacheTags ($tags)
 getCacheTags ()
 getCacheLifetime ()
 getFlag ($flag)
 setFlag ($flag, $value=null)
 hasFlag ($flag)

Public Attributes

const SORT_ORDER_ASC = 'ASC'
const SORT_ORDER_DESC = 'DESC'

Protected Member Functions

 _setIsLoaded ($flag=true)
 _getItemId (Varien_Object $item)
 _renderFilters ()
 _renderOrders ()
 _renderLimit ()
 _toOptionArray ($valueField='id', $labelField='name', $additional=array())
 _toOptionHash ($valueField='id', $labelField='name')

Protected Attributes

 $_items = array()
 $_itemObjectClass = 'Varien_Object'
 $_orders = array()
 $_filters = array()
 $_isFiltersRendered = false
 $_curPage = 1
 $_pageSize = false
 $_totalRecords
 $_isCollectionLoaded
 $_cacheKey
 $_cacheTags = array()
 $_cacheLifetime = 86400
 $_flags = array()


Detailed Description

Definition at line 34 of file Collection.php.


Constructor & Destructor Documentation

__construct (  ) 


Member Function Documentation

_getItemId ( Varien_Object item  )  [protected]

Reimplemented in Varien_Data_Collection_Db.

Definition at line 342 of file Collection.php.

00343     {
00344         return $item->getId();
00345     }

_renderFilters (  )  [protected]

Render sql select conditions

Returns:
Varien_Data_Collection

Reimplemented in Varien_Data_Collection_Db, and Varien_Directory_Collection.

Definition at line 498 of file Collection.php.

00499     {
00500         return $this;
00501     }

_renderLimit (  )  [protected]

Render sql select limit

Returns:
Varien_Data_Collection

Reimplemented in Varien_Data_Collection_Db.

Definition at line 518 of file Collection.php.

00519     {
00520         return $this;
00521     }

_renderOrders (  )  [protected]

Render sql select orders

Returns:
Varien_Data_Collection

Reimplemented in Varien_Data_Collection_Db.

Definition at line 508 of file Collection.php.

00509     {
00510         return $this;
00511     }

_setIsLoaded ( flag = true  )  [protected]

Set collection loading status flag

Parameters:
unknown_type $flag
Returns:
unknown

Definition at line 157 of file Collection.php.

00158     {
00159         $this->_isCollectionLoaded = $flag;
00160         return $this;
00161     }

_toOptionArray ( valueField = 'id',
labelField = 'name',
additional = array() 
) [protected]

Convert items array to array for select options

return items array array( $index => array( 'value' => mixed 'label' => mixed ) )

Parameters:
string $valueField
string $labelField
Returns:
array

Definition at line 605 of file Collection.php.

00606     {
00607         $res = array();
00608         $additional['value'] = $valueField;
00609         $additional['label'] = $labelField;
00610 
00611         foreach ($this as $item) {
00612             foreach ($additional as $code => $field) {
00613                 $data[$code] = $item->getData($field);
00614             }
00615             $res[] = $data;
00616         }
00617         return $res;
00618     }

_toOptionHash ( valueField = 'id',
labelField = 'name' 
) [protected]

Convert items array to hash for select options

return items hash array($value => $label)

Parameters:
string $valueField
string $labelField
Returns:
array

Definition at line 640 of file Collection.php.

00641     {
00642         $res = array();
00643         foreach ($this as $item) {
00644             $res[$item->getData($valueField)] = $item->getData($labelField);
00645         }
00646         return $res;
00647     }

addFilter ( field,
value,
type = 'and' 
)

Add collection filter

Parameters:
string $field
string $value
string $type and|or|string

Definition at line 129 of file Collection.php.

00130     {
00131         $filter = array();
00132         $filter['field']   = $field;
00133         $filter['value']   = $value;
00134         $filter['type']    = strtolower($type);
00135 
00136         $this->_filters[] = $filter;
00137         $this->_isFiltersRendered = false;
00138         return $this;
00139     }

addItem ( Varien_Object item  ) 

Adding item to item array

Parameters:
Varien_Object $item
Returns:
Varien_Data_Collection

Reimplemented in Mage_Eav_Model_Entity_Collection_Abstract, and Mage_Rule_Model_Mysql4_Rule_Collection.

Definition at line 327 of file Collection.php.

00328     {
00329         $itemId = $this->_getItemId($item);
00330         if (!is_null($itemId)) {
00331             if (isset($this->_items[$itemId])) {
00332                 throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist');
00333             }
00334             $this->_items[$itemId] = $item;
00335         }
00336         else {
00337             $this->_items[] = $item;
00338         }
00339         return $this;
00340     }

clear (  ) 

Clear collection

Returns:
Varien_Data_Collection

Reimplemented in Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Compare_Item_Collection.

Definition at line 366 of file Collection.php.

00367     {
00368         $this->_setIsLoaded(false);
00369         $this->_items = array();
00370         return $this;
00371     }

count (  ) 

Retireve count of collection loaded items

Returns:
int

Definition at line 678 of file Collection.php.

00679     {
00680         $this->load();
00681         return count($this->_items);
00682     }

distinct ( flag  ) 

Set select distinct

Parameters:
bool $flag

Reimplemented in Varien_Data_Collection_Db.

Definition at line 528 of file Collection.php.

00529     {
00530         return $this;
00531     }

each ( obj_method,
args = array() 
)

Definition at line 399 of file Collection.php.

00400     {
00401         foreach ($args->_items as $k => $item) {
00402             $args->_items[$k] = call_user_func($obj_method, $item);
00403         }
00404     }

getCacheKey (  ) 

Definition at line 690 of file Collection.php.

00691     {
00692         return $this->_cacheKey;
00693     }

getCacheLifetime (  ) 

Definition at line 706 of file Collection.php.

00707     {
00708         return $this->_cacheLifetime;
00709     }

getCacheTags (  ) 

Definition at line 701 of file Collection.php.

00702     {
00703         return $this->_cacheTags;
00704     }

getColumnValues ( colName  ) 

Retrieve field values from all items

Parameters:
string $colName
Returns:
array

Reimplemented in Mage_Review_Model_Mysql4_Review_Product_Collection.

Definition at line 271 of file Collection.php.

00272     {
00273         $this->load();
00274 
00275         $col = array();
00276         foreach ($this->getItems() as $item) {
00277             $col[] = $item->getData($colName);
00278         }
00279         return $col;
00280     }

getCurPage ( displacement = 0  ) 

Get current collection page

Parameters:
int $displacement
Returns:
int

Definition at line 169 of file Collection.php.

00170     {
00171         if ($this->_curPage + $displacement < 1) {
00172             return 1;
00173         }
00174         elseif ($this->_curPage + $displacement > $this->getLastPageNumber()) {
00175             return $this->getLastPageNumber();
00176         } else {
00177             return $this->_curPage + $displacement;
00178         }
00179     }

getFirstItem (  ) 

Retrieve collection first item

Returns:
Varien_Object

Definition at line 226 of file Collection.php.

00227     {
00228         $this->load();
00229 
00230         if (count($this->_items)) {
00231             reset($this->_items);
00232             return current($this->_items);
00233         }
00234 
00235         return new $this->_itemObjectClass();
00236     }

getFlag ( flag  ) 

Retrieve Flag

Parameters:
string $flag
Returns:
mixed

Definition at line 717 of file Collection.php.

00718     {
00719         return isset($this->_flags[$flag]) ? $this->_flags[$flag] : null;
00720     }

getItemByColumnValue ( column,
value 
)

Search first item by field value

Parameters:
string $column
mixed $value
Returns:
Varien_Object || null

Definition at line 309 of file Collection.php.

00310     {
00311         $this->load();
00312 
00313         foreach ($this as $item) {
00314             if ($item->getData($column)==$value) {
00315                 return $item;
00316             }
00317         }
00318         return null;
00319     }

getItemById ( idValue  ) 

Retrieve item by id

Parameters:
mixed $idValue
Returns:
Varien_Object

Reimplemented in Mage_Directory_Model_Mysql4_Country_Collection.

Definition at line 655 of file Collection.php.

00656     {
00657         $this->load();
00658         if (isset($this->_items[$idValue])) {
00659             return $this->_items[$idValue];
00660         }
00661         return null;
00662     }

getItems (  ) 

Retrieve collection items

Returns:
array

Definition at line 259 of file Collection.php.

00260     {
00261         $this->load();
00262         return $this->_items;
00263     }

getItemsByColumnValue ( column,
value 
)

Search all items by field value

Parameters:
string $column
mixed $value
Returns:
array

Definition at line 289 of file Collection.php.

00290     {
00291         $this->load();
00292 
00293         $res = array();
00294         foreach ($this as $item) {
00295             if ($item->getData($column)==$value) {
00296                 $res[] = $item;
00297             }
00298         }
00299         return $res;
00300     }

getIterator (  ) 

Implementation of IteratorAggregate.getIterator()

Definition at line 667 of file Collection.php.

00668     {
00669         $this->load();
00670         return new ArrayIterator($this->_items);
00671     }

getLastItem (  ) 

Retrieve collection last item

Returns:
Varien_Object

Definition at line 243 of file Collection.php.

00244     {
00245         $this->load();
00246 
00247         if (count($this->_items)) {
00248             return end($this->_items);
00249         }
00250 
00251         return new $this->_itemObjectClass();
00252     }

getLastPageNumber (  ) 

Retrieve collection last page number

Returns:
int

Definition at line 186 of file Collection.php.

00187     {
00188         $collectionSize = (int) $this->getSize();
00189         if (0 === $collectionSize) {
00190             return 1;
00191         }
00192         elseif($this->_pageSize) {
00193             return ceil($collectionSize/$this->_pageSize);
00194         }
00195         else{
00196             return 1;
00197         }
00198     }

getNewEmptyItem (  ) 

Retrieve collection empty item

Returns:
Varien_Object

Reimplemented in Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection.

Definition at line 488 of file Collection.php.

00489     {
00490         return new $this->_itemObjectClass();
00491     }

getPageSize (  ) 

Retrieve collection page size

Returns:
int

Definition at line 205 of file Collection.php.

00206     {
00207         return $this->_pageSize;
00208     }

getSize (  ) 

Retrieve collection all items count

Returns:
int

Reimplemented in Mage_Backup_Model_Fs_Collection, and Varien_Data_Collection_Db.

Definition at line 215 of file Collection.php.

00216     {
00217         $this->load();
00218         return $this->_totalRecords;
00219     }

hasFlag ( flag  ) 

Has Flag

Parameters:
string $flag
Returns:
bool

Definition at line 741 of file Collection.php.

00742     {
00743         return array_key_exists($flag, $this->_flags);
00744     }

isLoaded (  ) 

Retrieve collection loading status

Returns:
bool

Definition at line 146 of file Collection.php.

00147     {
00148         return $this->_isCollectionLoaded;
00149     }

load ( printQuery = false,
logQuery = false 
)

loadData ( printQuery = false,
logQuery = false 
)

Load data

Returns:
Varien_Data_Collection

Reimplemented in Mage_Adminhtml_Model_Extension_Collection_Abstract, and Varien_Data_Collection_Db.

Definition at line 538 of file Collection.php.

00539     {
00540         return $this;
00541     }

removeItemByKey ( key  ) 

Remove item from collection by item key

Parameters:
mixed $key
Returns:
Varien_Data_Collection

Definition at line 353 of file Collection.php.

00354     {
00355         if (isset($this->_items[$key])) {
00356             unset($this->_items[$key]);
00357         }
00358         return $this;
00359     }

setCacheKey ( key  ) 

Definition at line 684 of file Collection.php.

00685     {
00686         $this->_cacheKey = $key;
00687         return $this;
00688     }

setCacheTags ( tags  ) 

Definition at line 695 of file Collection.php.

00696     {
00697         $this->_cacheTags = $tags;
00698         return $this;
00699     }

setCurPage ( page  ) 

Set current page

Parameters:
int $page
Returns:
Varien_Data_Collection

Definition at line 433 of file Collection.php.

00434     {
00435         $this->_curPage = $page;
00436         return $this;
00437     }

setDataToAll ( key,
value = null 
)

Setting data for all collection items

Parameters:
mixed $key
mixed $value
Returns:
Varien_Data_Collection

Definition at line 413 of file Collection.php.

00414     {
00415         if (is_array($key)) {
00416             foreach ($key as $k=>$v) {
00417                 $this->setDataToAll($k, $v);
00418             }
00419             return $this;
00420         }
00421         foreach ($this->getItems() as $item) {
00422             $item->setData($key, $value);
00423         }
00424         return $this;
00425     }

setFlag ( flag,
value = null 
)

Set Flag

Parameters:
string $flag
mixed $value
Returns:
Varien_Data_Collection

Definition at line 729 of file Collection.php.

00730     {
00731         $this->_flags[$flag] = $value;
00732         return $this;
00733     }

setItemObjectClass ( className  ) 

Set collection item class name

Parameters:
string $className
Returns:
Varien_Data_Collection

is_subclass_of($className, 'Varien_Object') - Segmentation fault in php 5.2.3

Definition at line 470 of file Collection.php.

00471     {
00472         $className = Mage::getConfig()->getModelClassName($className);
00473         /**
00474          * is_subclass_of($className, 'Varien_Object') - Segmentation fault in php 5.2.3
00475          */
00476         /*if (!is_subclass_of($className, 'Varien_Object')) {
00477             throw new Exception($className.' does not extends from Varien_Object');
00478         }*/
00479         $this->_itemObjectClass = $className;
00480         return $this;
00481     }

setOrder ( field,
direction = 'desc' 
)

setPageSize ( size  ) 

Set collection page size

Parameters:
int $size
Returns:
Varien_Data_Collection

Definition at line 445 of file Collection.php.

00446     {
00447         $this->_pageSize = $size;
00448         return $this;
00449     }

toArray ( arrRequiredFields = array()  ) 

Convert collection to array

Returns:
array

Reimplemented in Mage_Eav_Model_Entity_Collection_Abstract.

Definition at line 578 of file Collection.php.

00579     {
00580         $arrItems = array();
00581         $arrItems['totalRecords'] = $this->getSize();
00582 
00583         $arrItems['items'] = array();
00584         foreach ($this as $item) {
00585             $arrItems['items'][] = $item->toArray($arrRequiredFields);
00586         }
00587         return $arrItems;
00588     }

toOptionArray (  ) 

toOptionHash (  ) 

toXml (  ) 

Convert collection to XML

Returns:
string

Definition at line 558 of file Collection.php.

00559     {
00560         $xml = '<?xml version="1.0" encoding="UTF-8"?>
00561         <collection>
00562            <totalRecords>'.$this->_totalRecords.'</totalRecords>
00563            <items>';
00564 
00565         foreach ($this as $item) {
00566             $xml.=$item->toXml();
00567         }
00568         $xml.= '</items>
00569         </collection>';
00570         return $xml;
00571     }

walk ( callback,
array args = array() 
)

Walk through the collection and run model method or external callback with optional arguments

Returns array with results of callback for each item

Parameters:
string $method
array $args
Returns:
array

Definition at line 383 of file Collection.php.

00384     {
00385         $results = array();
00386         $useItemCallback = is_string($callback) && strpos($callback, '::')===false;
00387         foreach ($this->getItems() as $id=>$item) {
00388             if ($useItemCallback) {
00389                 $cb = array($item, $callback);
00390             } else {
00391                 $cb = $callback;
00392                 array_unshift($args, $item);
00393             }
00394             $results[$id] = call_user_func_array($cb, $args);
00395         }
00396         return $results;
00397     }


Member Data Documentation

$_cacheKey [protected]

Definition at line 104 of file Collection.php.

$_cacheLifetime = 86400 [protected]

Definition at line 108 of file Collection.php.

$_cacheTags = array() [protected]

Definition at line 106 of file Collection.php.

$_curPage = 1 [protected]

Definition at line 79 of file Collection.php.

$_filters = array() [protected]

Reimplemented in Varien_Directory_Collection.

Definition at line 65 of file Collection.php.

$_flags = array() [protected]

Definition at line 115 of file Collection.php.

$_isCollectionLoaded [protected]

Definition at line 102 of file Collection.php.

$_isFiltersRendered = false [protected]

Definition at line 72 of file Collection.php.

$_itemObjectClass = 'Varien_Object' [protected]

Definition at line 51 of file Collection.php.

$_items = array() [protected]

Definition at line 44 of file Collection.php.

$_orders = array() [protected]

Definition at line 58 of file Collection.php.

$_pageSize = false [protected]

Definition at line 88 of file Collection.php.

$_totalRecords [protected]

Definition at line 95 of file Collection.php.

const SORT_ORDER_ASC = 'ASC'

Definition at line 36 of file Collection.php.

const SORT_ORDER_DESC = 'DESC'

Definition at line 37 of file Collection.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:58 2009 for Magento by  doxygen 1.5.8