00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 abstract class Mage_Core_Model_Mysql4_Collection_Abstract extends Varien_Data_Collection_Db
00029 {
00030
00031
00032
00033
00034
00035 protected $_model;
00036
00037
00038
00039
00040
00041
00042 protected $_resourceModel;
00043
00044
00045
00046
00047
00048
00049 protected $_resource;
00050
00051
00052
00053
00054
00055
00056 protected $_joinedTables = array();
00057
00058
00059
00060
00061
00062
00063 public function __construct($resource=null)
00064 {
00065 parent::__construct();
00066 $this->_construct();
00067 $this->_resource = $resource;
00068 $this->setConnection($this->getResource()->getReadConnection());
00069 $this->_initSelect();
00070 }
00071
00072
00073
00074
00075
00076 protected function _construct()
00077 {
00078
00079 }
00080
00081 protected function _initSelect()
00082 {
00083 $this->getSelect()->from(array('main_table' => $this->getResource()->getMainTable()));
00084 return $this;
00085 }
00086
00087
00088
00089
00090
00091
00092
00093 protected function _init($model, $resourceModel=null)
00094 {
00095 $this->setModel($model);
00096 if (is_null($resourceModel)) {
00097 $resourceModel = $model;
00098 }
00099 $this->setResourceModel($resourceModel);
00100 return $this;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 public function setModel($model)
00110 {
00111 if (is_string($model)) {
00112 $this->_model = $model;
00113 $this->setItemObjectClass(Mage::getConfig()->getModelClassName($model));
00114 }
00115 return $this;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124 public function getModelName($args=array())
00125 {
00126 return $this->_model;
00127 }
00128
00129 public function setResourceModel($model)
00130 {
00131 $this->_resourceModel = $model;
00132 }
00133
00134 public function getResourceModelName()
00135 {
00136 return $this->_resourceModel;
00137 }
00138
00139
00140
00141
00142
00143
00144 public function getResource()
00145 {
00146 if (empty($this->_resource)) {
00147 $this->_resource = Mage::getResourceModel($this->getResourceModelName());
00148 }
00149 return $this->_resource;
00150 }
00151
00152 public function getTable($table)
00153 {
00154 return $this->getResource()->getTable($table);
00155 }
00156
00157
00158
00159
00160
00161
00162 public function getAllIds()
00163 {
00164 $idsSelect = clone $this->getSelect();
00165 $idsSelect->reset(Zend_Db_Select::ORDER);
00166 $idsSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00167 $idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00168 $idsSelect->reset(Zend_Db_Select::COLUMNS);
00169 $idsSelect->from(null,
00170 'main_table.' . $this->getResource()->getIdFieldName()
00171 );
00172 return $this->getConnection()->fetchCol($idsSelect);
00173 }
00174
00175 public function join($table, $cond, $cols='*')
00176 {
00177 if (!isset($this->_joinedTables[$table])) {
00178 $this->getSelect()->join(array($table=>$this->getTable($table)), $cond, $cols);
00179 $this->_joinedTables[$table] = true;
00180 }
00181 return $this;
00182 }
00183
00184
00185
00186
00187
00188
00189 public function load($printQuery = false, $logQuery = false)
00190 {
00191 if (!$this->isLoaded()) {
00192 Mage::dispatchEvent('core_collection_abstract_load_before', array('collection' => $this));
00193 }
00194 parent::load($printQuery, $logQuery);
00195 foreach ($this->_items as $item) {
00196 $item->setOrigData();
00197 }
00198 return $this;
00199 }
00200
00201
00202
00203
00204
00205
00206 public function save()
00207 {
00208 foreach ($this->getItems() as $item) {
00209 $item->save();
00210 }
00211 return $this;
00212 }
00213
00214 protected function _canUseCache()
00215 {
00216 return Mage::app()->useCache('collections');
00217 }
00218
00219
00220
00221
00222
00223
00224 protected function _getCacheTags()
00225 {
00226 $tags = parent::_getCacheTags();
00227 foreach ($tags as $key => $value) {
00228 $tags[$key] = Mage::app()->prepareCacheId($value);
00229 }
00230 $tags[] = Mage_Core_Model_App::CACHE_TAG;
00231 return $tags;
00232 }
00233 }