Mage_Core_Model_Resource_Setup Class Reference

Inheritance diagram for Mage_Core_Model_Resource_Setup:

Mage_CatalogIndex_Model_Mysql4_Setup Mage_Eav_Model_Entity_Setup Mage_Sendfriend_Model_Mysql4_Setup Mage_Tax_Model_Mysql4_Setup Mage_Catalog_Model_Resource_Eav_Mysql4_Setup Mage_Customer_Model_Entity_Setup Mage_Sales_Model_Entity_Setup Mage_Sales_Model_Mysql4_Setup Mage_AmazonPayments_Model_Mysql4_Setup Mage_GiftMessage_Model_Mysql4_Setup Mage_GoogleCheckout_Model_Mysql4_Setup Mage_Paypal_Model_Mysql4_Setup Mage_Weee_Model_Mysql4_Setup

List of all members.

Public Member Functions

 __construct ($resourceName)
 getConnection ()
 setTable ($tableName, $realTableName)
 getTable ($tableName)
 applyUpdates ()
 getTableRow ($table, $idField, $id, $field=null, $parentField=null, $parentId=0)
 deleteTableRow ($table, $idField, $id, $parentField=null, $parentId=0)
 updateTableRow ($table, $idField, $id, $field, $value=null, $parentField=null, $parentId=0)
 updateTable ($table, $conditionExpr, $valueExpr)
 tableExists ($table)
 addConfigField ($path, $label, array $data=array(), $default=null)
 setConfigData ($path, $value, $scope='default', $scopeId=0, $inherit=0)
 deleteConfigData ($path, $scope=null)
 run ($sql)
 startSetup ()
 endSetup ()

Static Public Member Functions

static applyAllUpdates ()

Public Attributes

const VERSION_COMPARE_EQUAL = 0
const VERSION_COMPARE_LOWER = -1
const VERSION_COMPARE_GREATER = 1

Protected Member Functions

 _installResourceDb ($newVersion)
 _upgradeResourceDb ($oldVersion, $newVersion)
 _rollbackResourceDb ($newVersion, $oldVersion)
 _uninstallResourceDb ($version)
 _modifyResourceDb ($actionType, $fromVersion, $toVersion)
 _getModifySqlFiles ($actionType, $fromVersion, $toVersion, $arrFiles)

Protected Attributes

 $_resourceName
 $_resourceConfig
 $_connectionConfig
 $_moduleConfig
 $_conn
 $_tables = array()
 $_setupCache = array()

Static Protected Attributes

static $_hadUpdates


Detailed Description

Resource setup model

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 32 of file Setup.php.


Constructor & Destructor Documentation

__construct ( resourceName  ) 

Definition at line 54 of file Setup.php.

00055     {
00056         $config = Mage::getConfig();
00057         $this->_resourceName = $resourceName;
00058         $this->_resourceConfig = $config->getResourceConfig($resourceName);
00059         $this->_connectionConfig = $config->getResourceConnectionConfig($resourceName);
00060         $modName = (string)$this->_resourceConfig->setup->module;
00061         $this->_moduleConfig = $config->getModuleConfig($modName);
00062         $this->_conn = Mage::getSingleton('core/resource')->getConnection($this->_resourceName);
00063     }


Member Function Documentation

_getModifySqlFiles ( actionType,
fromVersion,
toVersion,
arrFiles 
) [protected]

Get sql files for modifications

Parameters:
$actionType 
Returns:
array

Definition at line 317 of file Setup.php.

00318     {
00319         $arrRes = array();
00320 
00321         switch ($actionType) {
00322             case 'install':
00323                 uksort($arrFiles, 'version_compare');
00324                 foreach ($arrFiles as $version => $file) {
00325                     if (version_compare($version, $toVersion)!==self::VERSION_COMPARE_GREATER) {
00326                         $arrRes[0] = array('toVersion'=>$version, 'fileName'=>$file);
00327                     }
00328                 }
00329                 break;
00330 
00331             case 'upgrade':
00332                 uksort($arrFiles, 'version_compare');
00333                 foreach ($arrFiles as $version => $file) {
00334                     $version_info = explode('-', $version);
00335 
00336                     // In array must be 2 elements: 0 => version from, 1 => version to
00337                     if (count($version_info)!=2) {
00338                         break;
00339                     }
00340                     $infoFrom = $version_info[0];
00341                     $infoTo   = $version_info[1];
00342                     if (version_compare($infoFrom, $fromVersion)!==self::VERSION_COMPARE_LOWER
00343                         && version_compare($infoTo, $toVersion)!==self::VERSION_COMPARE_GREATER) {
00344                         $arrRes[] = array('toVersion'=>$infoTo, 'fileName'=>$file);
00345                     }
00346                 }
00347                 break;
00348 
00349             case 'rollback':
00350                 break;
00351 
00352             case 'uninstall':
00353                 break;
00354         }
00355         return $arrRes;
00356     }

_installResourceDb ( newVersion  )  [protected]

Install resource

Parameters:
string $version
Returns:
boolean

Definition at line 165 of file Setup.php.

00166     {
00167         $oldVersion = $this->_modifyResourceDb('install', '', $newVersion);
00168         $this->_modifyResourceDb('upgrade', $oldVersion, $newVersion);
00169     }

_modifyResourceDb ( actionType,
fromVersion,
toVersion 
) [protected]

Run module modification sql

Parameters:
string $actionType install|upgrade|uninstall
string $fromVersion
string $toVersion
Returns:
bool

useful variables:

  • $conn: setup db connection
  • $sqlFilesDir: root dir for sql update files

Definition at line 215 of file Setup.php.

00216     {
00217         $resModel = (string)$this->_connectionConfig->model;
00218         $modName = (string)$this->_moduleConfig[0]->getName();
00219 
00220         $sqlFilesDir = Mage::getModuleDir('sql', $modName).DS.$this->_resourceName;
00221         if (!is_dir($sqlFilesDir) || !is_readable($sqlFilesDir)) {
00222             Mage::getResourceModel('core/resource')->setDbVersion($this->_resourceName, $toVersion);
00223             return $toVersion;
00224         }
00225         // Read resource files
00226         $arrAvailableFiles = array();
00227         $sqlDir = dir($sqlFilesDir);
00228         while (false !== ($sqlFile = $sqlDir->read())) {
00229             $matches = array();
00230             if (preg_match('#^'.$resModel.'-'.$actionType.'-(.*)\.(sql|php)$#i', $sqlFile, $matches)) {
00231                 $arrAvailableFiles[$matches[1]] = $sqlFile;
00232             }
00233         }
00234         $sqlDir->close();
00235         if (empty($arrAvailableFiles)) {
00236             Mage::getResourceModel('core/resource')->setDbVersion($this->_resourceName, $toVersion);
00237             return $toVersion;
00238         }
00239 
00240         // Get SQL files name
00241         $arrModifyFiles = $this->_getModifySqlFiles($actionType, $fromVersion, $toVersion, $arrAvailableFiles);
00242         if (empty($arrModifyFiles)) {
00243             Mage::getResourceModel('core/resource')->setDbVersion($this->_resourceName, $toVersion);
00244             return $toVersion;
00245         }
00246 
00247         $modifyVersion = null;
00248         foreach ($arrModifyFiles as $resourceFile) {
00249             $sqlFile = $sqlFilesDir.DS.$resourceFile['fileName'];
00250             $fileType = pathinfo($resourceFile['fileName'], PATHINFO_EXTENSION);
00251 
00252             // Execute SQL
00253             if ($this->_conn) {
00254                 try {
00255                     switch ($fileType) {
00256                         case 'sql':
00257                             $sql = file_get_contents($sqlFile);
00258                             if ($sql!='') {
00259                                 $result = $this->run($sql);
00260                             } else {
00261                                 $result = true;
00262                             }
00263                             break;
00264 
00265                         case 'php':
00266                             $conn = $this->_conn;
00267                             /**
00268                              * useful variables:
00269                              * - $conn: setup db connection
00270                              * - $sqlFilesDir: root dir for sql update files
00271                              */
00272                             try {
00273                                 #$conn->beginTransaction();
00274                                 $result = include($sqlFile);
00275                                 #$conn->commit();
00276                             } catch (Exception $e) {
00277                                 #$conn->rollback();
00278                                 throw ($e);
00279                             }
00280                             break;
00281 
00282                         default:
00283                             $result = false;
00284                     }
00285                     if ($result) {
00286                         /*$this->run("replace into ".$this->getTable('core/resource')." (code, version) values ('".$this->_resourceName."', '".$resourceFile['toVersion']."')");*/
00287                         Mage::getResourceModel('core/resource')->setDbVersion($this->_resourceName, $resourceFile['toVersion']);
00288                     }
00289                 }
00290                 catch (Exception $e){
00291                     echo "<pre>".print_r($e,1)."</pre>";
00292                     throw Mage::exception('Mage_Core', Mage::helper('core')->__('Error in file: "%s" - %s', $sqlFile, $e->getMessage()));
00293                 }
00294             }
00295 
00296             $modifyVersion = $resourceFile['toVersion'];
00297         }
00298 
00299         if ($actionType == 'upgrade' && $modifyVersion != $toVersion) {
00300             Mage::getResourceModel('core/resource')->setDbVersion($this->_resourceName, $toVersion);
00301         }
00302         else {
00303             $toVersion = $modifyVersion;
00304         }
00305 
00306         self::$_hadUpdates = true;
00307         return $toVersion;
00308     }

_rollbackResourceDb ( newVersion,
oldVersion 
) [protected]

Roll back resource

Parameters:
string $newVersion
Returns:
bool

Definition at line 189 of file Setup.php.

00190     {
00191         $this->_modifyResourceDb('rollback', $newVersion, $oldVersion);
00192     }

_uninstallResourceDb ( version  )  [protected]

Uninstall resource

Parameters:
$version existing resource version
Returns:
bool

Definition at line 201 of file Setup.php.

00202     {
00203         $this->_modifyResourceDb('uninstall', $version, '');
00204     }

_upgradeResourceDb ( oldVersion,
newVersion 
) [protected]

Upgrade DB for new resource version

Parameters:
string $oldVersion
string $newVersion

Definition at line 177 of file Setup.php.

00178     {
00179         $this->_modifyResourceDb('upgrade', $oldVersion, $newVersion);
00180     }

addConfigField ( path,
label,
array data = array(),
default = null 
)

Definition at line 474 of file Setup.php.

00475     {
00476         $data['level'] = sizeof(explode('/', $path));
00477         $data['path'] = $path;
00478         $data['frontend_label'] = $label;
00479         if ($id = $this->getTableRow('core/config_field', 'path', $path, 'field_id')) {
00480             $this->updateTableRow('core/config_field', 'field_id', $id, $data);
00481         } else {
00482             if (empty($data['sort_order'])) {
00483                 $sql = "select max(sort_order) cnt from ".$this->getTable('core/config_field')." where level=".($data['level']+1);
00484                 if ($data['level']>1) {
00485                     $sql.= $this->_conn->quoteInto(" and path like ?", dirname($path).'/%');
00486                 }
00487 
00488                 $result = $this->_conn->raw_fetchRow($sql);
00489                 $this->_conn->fetchAll($sql);
00490 #print_r($result); die;
00491                 $data['sort_order'] = $result['cnt']+1;
00492 /*
00493 // Triggers "Command out of sync" mysql error for next statement!?!?
00494                 $data['sort_order'] = $this->_conn->fetchOne("select max(sort_order)
00495                     from ".$this->getTable('core/config_field')."
00496                     where level=?".$parentWhere, $data['level'])+1;
00497 */
00498             }
00499 
00500             #$this->_conn->raw_query("insert into ".$this->getTable('core/config_field')." (".join(',', array_keys($data)).") values ('".join("','", array_values($data))."')");
00501             $this->_conn->insert($this->getTable('core/config_field'), $data);
00502         }
00503 
00504         if (!is_null($default)) {
00505             $this->setConfigData($path, $default);
00506         }
00507         return $this;
00508     }

static applyAllUpdates (  )  [static]

Apply database updates whenever needed

Returns:
boolean

Definition at line 99 of file Setup.php.

00100     {
00101         Mage::app()->setUpdateMode(true);
00102         $res = Mage::getSingleton('core/resource');
00103         /*
00104         if ($res->getAutoUpdate() == Mage_Core_Model_Resource::AUTO_UPDATE_NEVER) {
00105             return true;
00106         }
00107         */
00108 
00109         self::$_hadUpdates = false;
00110 
00111         $resources = Mage::getConfig()->getNode('global/resources')->children();
00112         foreach ($resources as $resName=>$resource) {
00113             if (!$resource->setup) {
00114                 continue;
00115             }
00116             $className = __CLASS__;
00117             if (isset($resource->setup->class)) {
00118                 $className = $resource->setup->getClassName();
00119             }
00120             $setupClass = new $className($resName);
00121             $setupClass->applyUpdates();
00122         }
00123 /*
00124         if (self::$_hadUpdates) {
00125             if ($res->getAutoUpdate() == Mage_Core_Model_Resource::AUTO_UPDATE_ONCE) {
00126                 $res->setAutoUpdate(Mage_Core_Model_Resource::AUTO_UPDATE_NEVER);
00127             }
00128         }
00129 */
00130         Mage::app()->setUpdateMode(false);
00131         return true;
00132     }

applyUpdates (  ) 

Definition at line 134 of file Setup.php.

00135     {
00136         $dbVer = Mage::getResourceModel('core/resource')->getDbVersion($this->_resourceName);
00137         $configVer = (string)$this->_moduleConfig->version;
00138         // Module is installed
00139         if ($dbVer!==false) {
00140              $status = version_compare($configVer, $dbVer);
00141              switch ($status) {
00142                 case self::VERSION_COMPARE_LOWER:
00143                     $this->_rollbackResourceDb($configVer, $dbVer);
00144                     break;
00145                 case self::VERSION_COMPARE_GREATER:
00146                     $this->_upgradeResourceDb($dbVer, $configVer);
00147                     break;
00148                 default:
00149                     return true;
00150                     break;
00151              }
00152         }
00153         // Module not installed
00154         elseif ($configVer) {
00155             $this->_installResourceDb($configVer);
00156         }
00157     }

deleteConfigData ( path,
scope = null 
)

Delete config field values

Parameters:
string $path
string $scope (default|stores|websites|config)
Returns:
Mage_Core_Model_Resource_Setup

Definition at line 523 of file Setup.php.

00524     {
00525         $sql = "delete from ".$this->getTable('core/config_data')." where path='".$path."'";
00526         if ($scope) {
00527             $sql.= " and scope='".$scope."'";
00528         }
00529         $this->_conn->raw_query($sql);
00530         return $this;
00531     }

deleteTableRow ( table,
idField,
id,
parentField = null,
parentId = 0 
)

Delete table row

Parameters:
string $table
string $idField
string|int $id
null|string $parentField
int|string $parentId
Returns:
Mage_Core_Model_Resource_Setup

Definition at line 402 of file Setup.php.

00403     {
00404         if (strpos($table, '/')!==false) {
00405             $table = $this->getTable($table);
00406         }
00407 
00408         $condition = $this->_conn->quoteInto("$idField=?", $id);
00409         if ($parentField !== null) {
00410             $condition.= $this->_conn->quoteInto(" AND $parentField=?", $parentId);
00411         }
00412         $this->_conn->delete($table, $condition);
00413 
00414         if (isset($this->_setupCache[$table][$parentId][$id])) {
00415             unset($this->_setupCache[$table][$parentId][$id]);
00416         }
00417         return $this;
00418     }

endSetup (  ) 

Definition at line 549 of file Setup.php.

00550     {
00551         $this->_conn->multi_query("
00552 SET SQL_MODE=IFNULL(@OLD_SQL_MODE,'');
00553 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS,0);
00554 ");
00555         return $this;
00556     }

getConnection (  ) 

get Connection

Returns:
Varien_Db_Adapter_Pdo_Mysql

Definition at line 70 of file Setup.php.

00071     {
00072         return $this->_conn;
00073     }

getTable ( tableName  ) 

Get table name

Parameters:
string $tableName
Returns:
string

Definition at line 87 of file Setup.php.

00087                                          {
00088         if (!isset($this->_tables[$tableName])) {
00089             $this->_tables[$tableName] = Mage::getSingleton('core/resource')->getTableName($tableName);
00090         }
00091         return $this->_tables[$tableName];
00092     }

getTableRow ( table,
idField,
id,
field = null,
parentField = null,
parentId = 0 
)

Retrieve row or field from table by id or string and parent id

Parameters:
string $table
string $idField
string|integer $id
string $field
string $parentField
string|integer $parentId
Returns:
mixed|boolean

Definition at line 372 of file Setup.php.

00373     {
00374         if (strpos($table, '/')!==false) {
00375             $table = $this->getTable($table);
00376         }
00377 
00378         if (empty($this->_setupCache[$table][$parentId][$id])) {
00379             $sql = "select * from $table where $idField=?";
00380             if (!is_null($parentField)) {
00381                 $sql .= $this->_conn->quoteInto(" and $parentField=?", $parentId);
00382             }
00383             $this->_setupCache[$table][$parentId][$id] = $this->_conn->fetchRow($sql, $id);
00384             $this->_conn->fetchAll($sql, $id);
00385         }
00386         if (is_null($field)) {
00387             return $this->_setupCache[$table][$parentId][$id];
00388         }
00389         return isset($this->_setupCache[$table][$parentId][$id][$field]) ? $this->_setupCache[$table][$parentId][$id][$field] : false;
00390     }

run ( sql  ) 

Definition at line 533 of file Setup.php.

00534     {
00535         $this->_conn->multi_query($sql);
00536         return $this;
00537     }

setConfigData ( path,
value,
scope = 'default',
scopeId = 0,
inherit = 0 
)

Definition at line 510 of file Setup.php.

00511     {
00512         $this->_conn->raw_query("replace into ".$this->getTable('core/config_data')." (scope, scope_id, path, value) values ('$scope', $scopeId, '$path', '$value')");
00513         return $this;
00514     }

setTable ( tableName,
realTableName 
)

Definition at line 75 of file Setup.php.

00076     {
00077         $this->_tables[$tableName] = $realTableName;
00078         return $this;
00079     }

startSetup (  ) 

Definition at line 539 of file Setup.php.

00540     {
00541         $this->_conn->multi_query("SET SQL_MODE='';
00542 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
00543 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
00544 ");
00545 
00546         return $this;
00547     }

tableExists ( table  ) 

Definition at line 465 of file Setup.php.

00466     {
00467         $select = $this->getConnection()->quoteInto('SHOW TABLES LIKE ?', $table);
00468         $result = $this->getConnection()->fetchOne($select);
00469         return !empty($result);
00470     }

updateTable ( table,
conditionExpr,
valueExpr 
)

Definition at line 455 of file Setup.php.

00456     {
00457         if (strpos($table, '/')!==false) {
00458             $table = $this->getTable($table);
00459         }
00460         $sql = 'update ' . $table . ' set ' . $valueExpr . ' where ' . $conditionExpr;
00461         $this->_conn->query($sql);
00462         return $this;
00463     }

updateTableRow ( table,
idField,
id,
field,
value = null,
parentField = null,
parentId = 0 
)

Update one or more fields of table row

Parameters:
string $table
string $idField
string|integer $id
string|array $field
mixed|null $value
string $parentField
string|integer $parentId
Returns:
Mage_Eav_Model_Entity_Setup

Definition at line 432 of file Setup.php.

00433     {
00434         if (is_array($field)) {
00435             $updateArr = array();
00436             foreach ($field as $f=>$v) {
00437                 $updateArr[] = $this->_conn->quoteInto("$f=?", $v);
00438             }
00439             $updateStr = join(', ', $updateArr);
00440         } else {
00441             $updateStr = $this->_conn->quoteInto("$field=?", $value);
00442         }
00443         if (strpos($table, '/')!==false) {
00444             $table = $this->getTable($table);
00445         }
00446         $sql = "update $table set $updateStr where ".$this->_conn->quoteInto("$idField=?", $id);
00447         if (!is_null($parentField)) {
00448             $sql .= $this->_conn->quoteInto(" and $parentField=?", $parentId);
00449         }
00450         $this->_conn->query($sql);
00451 
00452         return $this;
00453     }


Member Data Documentation

$_conn [protected]

Definition at line 48 of file Setup.php.

$_connectionConfig [protected]

Definition at line 40 of file Setup.php.

$_hadUpdates [static, protected]

Definition at line 52 of file Setup.php.

$_moduleConfig [protected]

Definition at line 41 of file Setup.php.

$_resourceConfig [protected]

Definition at line 39 of file Setup.php.

$_resourceName [protected]

Definition at line 38 of file Setup.php.

$_setupCache = array() [protected]

Definition at line 50 of file Setup.php.

$_tables = array() [protected]

Definition at line 49 of file Setup.php.

Definition at line 34 of file Setup.php.

Definition at line 36 of file Setup.php.

Definition at line 35 of file Setup.php.


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

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