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
00029
00030
00031
00032
00033
00034
00035
00036 abstract class Mage_Core_Model_Mysql4_Abstract extends Mage_Core_Model_Resource_Abstract
00037 {
00038 const CHECKSUM_KEY_NAME = 'Checksum';
00039
00040
00041
00042
00043
00044
00045 protected $_resources;
00046
00047
00048
00049
00050
00051
00052 protected $_resourcePrefix;
00053
00054
00055
00056
00057
00058
00059 protected $_connections = array();
00060
00061
00062
00063
00064
00065
00066 protected $_resourceModel;
00067
00068
00069
00070
00071
00072
00073 protected $_tables = array();
00074
00075
00076
00077
00078
00079
00080 protected $_mainTable;
00081
00082
00083
00084
00085
00086
00087 protected $_idFieldName;
00088
00089
00090
00091
00092
00093
00094 protected $_isPkAutoIncrement = true;
00095
00096 protected $_mainTableFields;
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 protected $_uniqueFields = null;
00116
00117
00118
00119
00120
00121
00122
00123
00124 protected function _init($mainTable, $idFieldName)
00125 {
00126 $this->_setMainTable($mainTable, $idFieldName);
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 protected function _setResource($connections, $tables=null)
00140 {
00141 $this->_resources = Mage::getSingleton('core/resource');
00142
00143 if (is_array($connections)) {
00144 foreach ($connections as $k=>$v) {
00145 $this->_connections[$k] = $this->_resources->getConnection($v);
00146 }
00147 }
00148 elseif (is_string($connections)) {
00149 $this->_resourcePrefix = $connections;
00150 }
00151
00152 if (is_null($tables) && is_string($connections)) {
00153 $this->_resourceModel = $this->_resourcePrefix;
00154 }
00155 elseif (is_array($tables)) {
00156 foreach ($tables as $k=>$v) {
00157 $this->_tables[$k] = $this->_resources->getTableName($v);
00158 }
00159 }
00160 elseif (is_string($tables)) {
00161 $this->_resourceModel = $tables;
00162 }
00163 return $this;
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 protected function _setMainTable($mainTable, $idFieldName=null)
00176 {
00177 $mainTableArr = explode('/', $mainTable);
00178
00179 if (!empty($mainTableArr[1])) {
00180 if (empty($this->_resourceModel)) {
00181 $this->_setResource($mainTableArr[0]);
00182 }
00183 $this->_setMainTable($mainTableArr[1], $idFieldName);
00184 } else {
00185 $this->_mainTable = $mainTable;
00186 if (is_null($idFieldName)) {
00187 $idFieldName = $mainTable.'_id';
00188 }
00189 $this->_idFieldName = $idFieldName;
00190 }
00191
00192 return $this;
00193 }
00194
00195
00196
00197
00198
00199
00200 public function getIdFieldName()
00201 {
00202 if (empty($this->_idFieldName)) {
00203 Mage::throwException(Mage::helper('core')->__('Empty identifier field name'));
00204 }
00205 return $this->_idFieldName;
00206 }
00207
00208
00209
00210
00211
00212
00213 public function getMainTable()
00214 {
00215 if (empty($this->_mainTable)) {
00216 Mage::throwException(Mage::helper('core')->__('Empty main table name'));
00217 }
00218 return $this->getTable($this->_mainTable);
00219 }
00220
00221
00222
00223
00224
00225
00226 public function getTable($entityName)
00227 {
00228 if (isset($this->_tables[$entityName])) {
00229 return $this->_tables[$entityName];
00230 }
00231 if (strpos($entityName, '/')) {
00232 $this->_tables[$entityName] = $this->_resources->getTableName($entityName);
00233 } elseif (!empty($this->_resourceModel)) {
00234 $this->_tables[$entityName] = $this->_resources->getTableName(
00235 $this->_resourceModel.'/'.$entityName);
00236 } else {
00237 $this->_tables[$entityName] = $entityName;
00238 }
00239 return $this->_tables[$entityName];
00240 }
00241
00242
00243
00244
00245
00246
00247
00248 protected function _getConnection($connectionName)
00249 {
00250 if (isset($this->_connections[$connectionName])) {
00251 return $this->_connections[$connectionName];
00252 }
00253 if (!empty($this->_resourcePrefix)) {
00254 $this->_connections[$connectionName] = $this->_resources->getConnection(
00255 $this->_resourcePrefix.'_'.$connectionName);
00256 } else {
00257 $this->_connections[$connectionName] = $this->_resources->getConnection($connectionName);
00258 }
00259
00260 return $this->_connections[$connectionName];
00261 }
00262
00263
00264
00265
00266
00267
00268 protected function _getReadAdapter()
00269 {
00270 return $this->_getConnection('read');
00271 }
00272
00273
00274
00275
00276
00277
00278 protected function _getWriteAdapter()
00279 {
00280 return $this->_getConnection('write');
00281 }
00282
00283
00284
00285
00286
00287
00288 public function getReadConnection()
00289 {
00290 return $this->_getReadAdapter();
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301 public function load(Mage_Core_Model_Abstract $object, $value, $field=null)
00302 {
00303 if (is_null($field)) {
00304 $field = $this->getIdFieldName();
00305 }
00306
00307 $read = $this->_getReadAdapter();
00308 if ($read && !is_null($value)) {
00309 $select = $this->_getLoadSelect($field, $value, $object);
00310 $data = $read->fetchRow($select);
00311
00312 if ($data) {
00313 $object->setData($data);
00314 }
00315 }
00316
00317 $this->_afterLoad($object);
00318
00319 return $this;
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329 protected function _getLoadSelect($field, $value, $object)
00330 {
00331 $select = $this->_getReadAdapter()->select()
00332 ->from($this->getMainTable())
00333 ->where($this->getMainTable().'.'.$field.'=?', $value);
00334 return $select;
00335 }
00336
00337
00338
00339
00340
00341
00342
00343 public function save(Mage_Core_Model_Abstract $object)
00344 {
00345 if ($object->isDeleted()) {
00346 return $this->delete($object);
00347 }
00348
00349 $this->_beforeSave($object);
00350 $this->_checkUnique($object);
00351
00352 if (!is_null($object->getId())) {
00353 $condition = $this->_getWriteAdapter()->quoteInto($this->getIdFieldName().'=?', $object->getId());
00354
00355
00356
00357 if ($this->_isPkAutoIncrement) {
00358 $this->_getWriteAdapter()->update($this->getMainTable(), $this->_prepareDataForSave($object), $condition);
00359 } else {
00360 $select = $this->_getWriteAdapter()->select($this->getMainTable(), array($this->getIdFieldName()))
00361 ->where($condition);
00362 if ($this->_getWriteAdapter()->fetchOne($select) !== false) {
00363 $this->_getWriteAdapter()->update($this->getMainTable(), $this->_prepareDataForSave($object), $condition);
00364 } else {
00365 $this->_getWriteAdapter()->insert($this->getMainTable(), $this->_prepareDataForSave($object));
00366 }
00367 }
00368 } else {
00369 $this->_getWriteAdapter()->insert($this->getMainTable(), $this->_prepareDataForSave($object));
00370 $object->setId($this->_getWriteAdapter()->lastInsertId($this->getMainTable()));
00371 }
00372
00373 $this->_afterSave($object);
00374
00375 return $this;
00376 }
00377
00378
00379
00380
00381
00382
00383
00384 public function delete(Mage_Core_Model_Abstract $object)
00385 {
00386 $this->_beforeDelete($object);
00387 $this->_getWriteAdapter()->delete(
00388 $this->getMainTable(),
00389 $this->_getWriteAdapter()->quoteInto($this->getIdFieldName().'=?', $object->getId())
00390 );
00391 $this->_afterDelete($object);
00392 return $this;
00393 }
00394
00395
00396
00397
00398
00399
00400
00401 public function addUniqueField($field)
00402 {
00403 $this->_initUniqueFields();
00404 if(is_array($this->_uniqueFields) ) {
00405 $this->_uniqueFields[] = $field;
00406 }
00407 return $this;
00408 }
00409
00410
00411
00412
00413
00414
00415 public function resetUniqueField()
00416 {
00417 $this->_uniqueFields = array();
00418 return $this;
00419 }
00420
00421
00422
00423
00424
00425
00426 protected function _initUniqueFields()
00427 {
00428 $this->_uniqueFields = array();
00429 return $this;
00430 }
00431
00432
00433
00434
00435
00436
00437 public function getUniqueFields()
00438 {
00439 if (is_null($this->_uniqueFields)) {
00440 $this->_initUniqueFields();
00441 }
00442 return $this->_uniqueFields;
00443 }
00444
00445
00446
00447
00448
00449
00450
00451 protected function _prepareDataForSave(Mage_Core_Model_Abstract $object)
00452 {
00453 $data = array();
00454 $fields = $this->_getWriteAdapter()->describeTable($this->getMainTable());
00455 foreach (array_keys($fields) as $field) {
00456 if ($object->hasData($field)) {
00457 $fieldValue = $object->getData($field);
00458 if ($fieldValue instanceof Zend_Db_Expr) {
00459 $data[$field] = $fieldValue;
00460 }
00461 else {
00462 if (null !== $fieldValue) {
00463 $data[$field] = $this->_prepareValueForSave($fieldValue, $fields[$field]['DATA_TYPE']);
00464 }
00465 elseif (!empty($fields[$field]['NULLABLE'])) {
00466 $data[$field] = null;
00467 }
00468 }
00469 }
00470 }
00471 return $data;
00472 }
00473
00474
00475
00476
00477
00478
00479
00480
00481 protected function _prepareValueForSave($value, $type)
00482 {
00483 if ($type == 'decimal') {
00484 $value = Mage::app()->getLocale()->getNumber($value);
00485 }
00486 return $value;
00487 }
00488
00489
00490
00491
00492
00493
00494
00495
00496 protected function _checkUnique(Mage_Core_Model_Abstract $object)
00497 {
00498 $existent = array();
00499 $fields = $this->getUniqueFields();
00500 if (!empty($fields)) {
00501 if (!is_array($fields)) {
00502 $this->_uniqueFields = array(
00503 array(
00504 'field' => $fields,
00505 'title' => $fields
00506 ));
00507 }
00508
00509 $data = new Varien_Object($this->_prepareDataForSave($object));
00510 $select = $this->_getWriteAdapter()->select()
00511 ->from($this->getMainTable());
00512
00513 foreach ($fields as $unique) {
00514 $select->reset(Zend_Db_Select::WHERE);
00515
00516 if (is_array($unique['field'])) {
00517 foreach ($unique['field'] as $field) {
00518 $select->where($field.'=?', $data->getData($field));
00519 }
00520 }
00521 else {
00522 $select->where( $unique['field'] . ' = ?', $data->getData($unique['field']) );
00523 }
00524
00525 if ($object->getId()) {
00526 $select->where($this->getIdFieldName().' != ?', $object->getId());
00527 }
00528
00529 if ( $test = $this->_getWriteAdapter()->fetchRow($select) ) {
00530 $existent[] = $unique['title'];
00531 }
00532 }
00533 }
00534
00535 if (!empty($existent)) {
00536 if (count($existent) == 1 ) {
00537 $error = Mage::helper('core')->__('%s already exist', $existent[0]);
00538 }
00539 else {
00540 $error = Mage::helper('core')->__('%s already exists', implode(', ', $existent));
00541 }
00542 Mage::throwException($error);
00543 }
00544 return $this;
00545 }
00546
00547 public function afterLoad(Mage_Core_Model_Abstract $object)
00548 {
00549 $this->_afterLoad($object);
00550 }
00551
00552
00553
00554
00555
00556
00557 protected function _afterLoad(Mage_Core_Model_Abstract $object)
00558 {
00559 return $this;
00560 }
00561
00562
00563
00564
00565
00566
00567 protected function _beforeSave(Mage_Core_Model_Abstract $object)
00568 {
00569 return $this;
00570 }
00571
00572
00573
00574
00575
00576
00577 protected function _afterSave(Mage_Core_Model_Abstract $object)
00578 {
00579 return $this;
00580 }
00581
00582
00583
00584
00585
00586
00587 protected function _beforeDelete(Mage_Core_Model_Abstract $object)
00588 {
00589 return $this;
00590 }
00591
00592
00593
00594
00595
00596
00597 protected function _afterDelete(Mage_Core_Model_Abstract $object)
00598 {
00599 return $this;
00600 }
00601
00602
00603
00604
00605
00606
00607
00608 public function getChecksum($table)
00609 {
00610 if (!$this->_getConnection('read')) {
00611 return false;
00612 }
00613
00614 if (is_array($table)) {
00615 $table = implode(',', $table);
00616 }
00617
00618 $data = $this->_getConnection('read')->fetchAll('checksum table '.$table);
00619 $checksum = 0;
00620 foreach ($data as $row) {
00621 $checksum+= $row[self::CHECKSUM_KEY_NAME];
00622 }
00623 return $checksum;
00624 }
00625 }