Mage_Rating_Model_Mysql4_Rating_Collection Class Reference

Inheritance diagram for Mage_Rating_Model_Mysql4_Rating_Collection:

Mage_Core_Model_Mysql4_Collection_Abstract Varien_Data_Collection_Db Varien_Data_Collection

List of all members.

Public Member Functions

 _construct ()
 addEntityFilter ($entity)
 setPositionOrder ($dir='ASC')
 setStoreFilter ($storeId)
 addOptionToItems ()
 addEntitySummaryToItem ($entityPkValue, $storeId)
 addRatingPerStoreName ($storeId)
 addStoresToCollection ()

Protected Attributes

 $_isStoreJoined = false


Detailed Description

Definition at line 34 of file Collection.php.


Member Function Documentation

_construct (  ) 

Initialization here

Reimplemented from Mage_Core_Model_Mysql4_Collection_Abstract.

Definition at line 41 of file Collection.php.

00042     {
00043         $this->_init('rating/rating');
00044     }

addEntityFilter ( entity  ) 

add entity filter

Parameters:
int|string $entity
Returns:
Varien_Data_Collection_Db

Definition at line 54 of file Collection.php.

00055     {
00056         $this->_select->join($this->getTable('rating_entity'),
00057            'main_table.entity_id='.$this->getTable('rating_entity').'.entity_id');
00058 
00059         if (is_numeric($entity)) {
00060             $this->addFilter('entity',
00061                 $this->getConnection()->quoteInto($this->getTable('rating_entity').'.entity_id=?', $entity),
00062                 'string');
00063         }
00064         elseif (is_string($entity)) {
00065             $this->addFilter('entity',
00066                 $this->getConnection()->quoteInto($this->getTable('rating_entity').'.entity_code=?', $entity),
00067                 'string');
00068         }
00069         return $this;
00070     }

addEntitySummaryToItem ( entityPkValue,
storeId 
)

Definition at line 125 of file Collection.php.

00126     {
00127         $arrRatingId = $this->getColumnValues('rating_id');
00128 
00129         if( count($arrRatingId) == 0 ) {
00130             return;
00131         }
00132 
00133         $sql = "SELECT
00134                     {$this->getTable('rating_vote')}.rating_id as rating_id,
00135                     SUM({$this->getTable('rating_vote')}.percent) as sum,
00136                     COUNT(*) as count
00137                 FROM
00138                     {$this->getTable('rating_vote')}
00139                 INNER JOIN
00140                     {$this->getTable('review/review_store')}
00141                     ON {$this->getTable('rating_vote')}.review_id={$this->getTable('review/review_store')}.review_id AND {$this->getTable('review/review_store')}.store_id = ". (int) $storeId . "
00142                 INNER JOIN
00143                     {$this->getTable('rating/rating_store')} AS rst
00144                     ON rst.rating_id = {$this->getTable('rating_vote')}.rating_id AND rst.store_id = ". (int) $storeId . "
00145                 INNER JOIN
00146                     {$this->getTable('review/review')} ON {$this->getTable('review/review_store')}.review_id={$this->getTable('review/review')}.review_id AND {$this->getTable('review/review')}.status_id=1
00147                 WHERE
00148                     {$this->getConnection()->quoteInto($this->getTable('rating_vote').'.rating_id IN (?)', $arrRatingId)}
00149                     AND {$this->getConnection()->quoteInto($this->getTable('rating_vote').'.entity_pk_value=?', $entityPkValue)}
00150                 GROUP BY
00151                     {$this->getTable('rating_vote')}.rating_id";
00152 
00153         $data = $this->getConnection()->fetchAll($sql);
00154 
00155         foreach ($data as $item) {
00156             $rating = $this->getItemById($item['rating_id']);
00157                 if ($rating && $item['count']>0) {
00158                     $rating->setSummary($item['sum']/$item['count']);
00159                 }
00160         }
00161         return $this;
00162     }

addOptionToItems (  ) 

add options to ratings in collection

Returns:
Varien_Data_Collection_Db

Definition at line 107 of file Collection.php.

00108     {
00109         $arrRatingId = $this->getColumnValues('rating_id');
00110 
00111         if (!empty($arrRatingId)) {
00112             $collection = Mage::getResourceModel('rating/rating_option_collection')
00113                 ->addRatingFilter($arrRatingId)
00114                 ->setPositionOrder()
00115                 ->load();
00116 
00117             foreach ($this as $rating) {
00118                 $rating->setOptions($collection->getItemsByColumnValue('rating_id', $rating->getId()));
00119             }
00120         }
00121 
00122         return $this;
00123     }

addRatingPerStoreName ( storeId  ) 

Definition at line 164 of file Collection.php.

00164                                                     {
00165         $this->getSelect()->joinLeft(array('title'=>$this->getTable('rating_title')),
00166                           'main_table.rating_id=title.rating_id AND title.store_id = '. (int) $storeId,
00167                           array('IF(title.value IS NULL, main_table.rating_code, title.value) AS rating_code'));
00168         return $this;
00169     }

addStoresToCollection (  ) 

Definition at line 171 of file Collection.php.

00172     {
00173         if (!$this->_isCollectionLoaded) {
00174             return $this;
00175         }
00176         $ratingIds = array();
00177         foreach ($this as $item) {
00178             $ratingIds[] = $item->getId();
00179             $item->setStores(array());
00180         }
00181         if (!$ratingIds) {
00182             return $this;
00183         }
00184 
00185         $this->_select = $this->getConnection()
00186             ->select()
00187             ->from($this->getTable('rating_store'))
00188             ->where('rating_id IN(?)', $ratingIds);
00189         foreach ($this->getConnection()->fetchAll($this->_select) as $row) {
00190             $item = $this->getItemById($row['rating_id']);
00191             $item->setStores(array_merge($item->getStores(), array($row['store_id'])));
00192         }
00193 
00194         return $this;
00195     }

setPositionOrder ( dir = 'ASC'  ) 

set order by position field

Parameters:
string $dir
Returns:
Varien_Data_Collection_Db

Definition at line 78 of file Collection.php.

00079     {
00080         $this->setOrder('main_table.position', $dir);
00081         return $this;
00082     }

setStoreFilter ( storeId  ) 

Definition at line 84 of file Collection.php.

00085     {
00086         if(!is_array($storeId)) {
00087            $storeId = array($storeId === null ? -1 : $storeId);
00088         }
00089         if (empty($storeId)) {
00090             return $this;
00091         }
00092         if (!$this->_isStoreJoined) {
00093             $this->getSelect()->join(array('store'=>$this->getTable('rating_store')), 'main_table.rating_id = store.rating_id')
00094                 ->group('main_table.rating_id');
00095             $this->_isStoreJoined = true;
00096         }
00097         $this->getSelect()->where($this->getConnection()->quoteInto('store.store_id IN(?)', $storeId));
00098         $this->setPositionOrder();
00099         return $this;
00100     }


Member Data Documentation

$_isStoreJoined = false [protected]

Definition at line 39 of file Collection.php.


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

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