/** * Test that the model detects a connection when it becomes active */ public function testGetConnectionInMemoryCaching() { $connection = new Varien_Db_Adapter_Pdo_Mysql(array('dbname' => 'test_dbname', 'username' => 'test_username', 'password' => 'test_password')); $this->_resource->expects($this->atLeastOnce())->method('getConnection')->with('core_read')->will($this->onConsecutiveCalls(false, $connection, false)); $this->assertFalse($this->_model->getReadConnection()); $this->assertSame($connection, $this->_model->getReadConnection(), 'Inactive connection should not be cached'); $this->assertSame($connection, $this->_model->getReadConnection(), 'Active connection should be cached'); }
/** * Clean database * * @param SimpleXMLElement $config * @return Mage_Install_Model_Installer_Db_Abstract */ public function cleanUpDatabase(SimpleXMLElement $config) { $resourceModel = new Mage_Core_Model_Resource(); $connection = $resourceModel->getConnection(Mage_Core_Model_Resource::DEFAULT_SETUP_RESOURCE); $dbName = $config->dbname; $connection->query('DROP DATABASE IF EXISTS ' . $dbName); $connection->query('CREATE DATABASE ' . $dbName); return $this; }
/** * @magentoConfigFixture global/resources/db/table_prefix prefix_ */ public function testGetTableName() { $tablePrefix = 'prefix_'; $tableSuffix = 'suffix'; $tableNameOrig = 'core_website'; $tableName = $this->_model->getTableName(array($tableNameOrig, $tableSuffix)); $this->assertContains($tablePrefix, $tableName); $this->assertContains($tableSuffix, $tableName); $this->assertContains($tableNameOrig, $tableName); }
/** * Extract table names and prepare for cli rendering * * @param array $resources * * @return void */ protected function prepareResourcesTables($resources) { $preparedResources = array(); foreach ($resources as $resource) { try { $preparedResources[] = $this->coreResource->getTableName($resource); } catch (Mage_Core_Exception $e) { } } return $preparedResources; }
public function getEstado($id) { $resource = new Mage_Core_Model_Resource(); $read = $resource->getConnection("core_read"); $select = $read->select()->from('directory_country_region')->where('region_id = ?', $id); $r = $read->fetchAll($select); if (array_key_exists(0, $r)) { $estado = $r[0]; if (array_key_exists('default_name', $estado)) { return $estado['default_name']; } } }
public function deleteAction() { $post = $this->getRequest()->getParams(); $id = $post["id"]; $resource = new Mage_Core_Model_Resource(); $write = $resource->getConnection('core_write'); try { $write->delete("motoboyconfig", $write->quoteInto("id=?", $id)); $this->_redirect('*/*/rangecep'); Mage::getSingleton('adminhtml/session')->addSuccess('Faixa de CEP deletada com sucesso!'); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError('Erro ao deletar Faixa de CEP, contate o suporte!'); } }
/** * Init profiler during creation of DB connect */ public function testProfilerInit() { $connReadConfig = Mage::getConfig()->getResourceConnectionConfig('core_read'); $profilerConfig = $connReadConfig->addChild('profiler'); $profilerConfig->addChild('class', 'Mage_Core_Model_Resource_Db_Profiler'); $profilerConfig->addChild('enabled', 'true'); /** @var Zend_Db_Adapter_Abstract $connection */ $connection = $this->_model->getConnection('core_read'); /** @var Mage_Core_Model_Resource_Db_Profiler $profiler */ $profiler = $connection->getProfiler(); $this->assertInstanceOf('Mage_Core_Model_Resource_Db_Profiler', $profiler); $this->assertTrue($profiler->getEnabled()); $this->assertAttributeEquals((string) $connReadConfig->host, '_host', $profiler); $this->assertAttributeEquals((string) $connReadConfig->type, '_type', $profiler); }
/** * Copy data from temporary to main redirects table * * @return void */ protected function _copyRedirectsToMainTable() { $select = $this->_connection->select(); $select->from(array('t' => self::TMP_TABLE_NAME), array('*')); $query = $select->insertFromSelect($this->_resource->getTableName('enterprise_urlrewrite/redirect'), array('identifier', 'target_path', 'store_id', 'category_id', 'product_id'), Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE); $this->_connection->query($query); }
/** * Get trigger action body for event * * @param int $entityEventId * @return string */ public function getTriggerBody($entityEventId) { $select = $this->_connection->select()->reset()->from(array(), array('status' => new Zend_Db_Expr('?')))->joinInner(array('me' => $this->_resource->getTableName('enterprise_mview/metadata_event')), new Zend_Db_Expr('mm.metadata_id = me.metadata_id'), array())->where('mview_event_id = ?', $entityEventId); $update = $this->_connection->updateFromSelect($select, array('mm' => $this->_resource->getTableName('enterprise_mview/metadata'))); $update = $this->_connection->quoteInto($update, Enterprise_Mview_Model_Metadata::STATUS_INVALID, null, 1); return $update . ';'; }
/** * Creates a connection to resource whenever needed * * @param string $name * @return Varien_Db_Adapter_Interface */ public function getConnection($name) { $connection = parent::getConnection($name); /* * Make sure the profiler is enabled for all requests */ $connection->getProfiler()->setEnabled(true); return $connection; }
/** * Creates a connection to resource whenever needed * * @param string $name * @return Varien_Db_Adapter_Interface */ public function getConnection($name) { if (defined('XTEST_BOOTSTRAPPED')) { static $connection; if (!$connection) { $connection = parent::getConnection('core_write'); } return $connection; } return parent::getConnection('default_setup'); }
/** * Get connection by name or type * * @param string $connectionName * @return Zend_Db_Adapter_Abstract */ protected function _getConnection($connectionName) { if (isset($this->_connections[$connectionName])) { return $this->_connections[$connectionName]; } if (!empty($this->_resourcePrefix)) { $this->_connections[$connectionName] = $this->_resources->getConnection($this->_resourcePrefix . '_' . $connectionName); } else { $this->_connections[$connectionName] = $this->_resources->getConnection($connectionName); } return $this->_connections[$connectionName]; }
/** * Create $total Magento customers with emails "customer_$i[at]test.com" and save mapping to MageId into * $this->_testMageCustomers & $this->_testMageCustomersReverted * * ATTENTION: There is warning "Test method "_createMageCustomers" in test class "..." is not public." * in case of method's visibility is 'protected' (after $total argument was added). * * @param int $total total count of the Magento customers to be created. */ protected function _createMageCustomers($total = 13) { $tbl = $this->_resource->getTableName(Cfg::ENTITY_MAGE_CUSTOMER); for ($i = 1; $i <= $total; $i++) { $email = "customer_{$i}@test.com"; $this->_conn->insert($tbl, [Cfg::E_CUSTOMER_A_EMAIL => $email]); $id = $this->_conn->lastInsertId($tbl); $this->_mapCustomerMageIdByIndex[$i] = $id; $this->_mapCustomerIndexByMageId[$id] = $i; $this->_logger->debug("New Magento customer #{$i} is added with ID={$id} ({$email})."); } $this->_logger->debug("Total {$total} customer were added to Magento."); }
/** * Populate ACL with rules from external storage * * @param Magento_Acl $acl */ public function populateAcl(Magento_Acl $acl) { $ruleTable = $this->_resource->getTableName("admin_rule"); $adapter = $this->_resource->getConnection('read'); $select = $adapter->select()->from(array('r' => $ruleTable)); $rulesArr = $adapter->fetchAll($select); foreach ($rulesArr as $rule) { $role = $rule['role_type'] . $rule['role_id']; $resource = $rule['resource_id']; $privileges = !empty($rule['privileges']) ? explode(',', $rule['privileges']) : null; if ($rule['permission'] == 'allow') { if ($resource === Mage_Backend_Model_Acl_Config::ACL_RESOURCE_ALL) { $acl->allow($role, null, $privileges); } $acl->allow($role, $resource, $privileges); } else { if ($rule['permission'] == 'deny') { $acl->deny($role, $resource, $privileges); } } } }
/** * Get action model name by index table and type * * @param string $indexTable * @param string $type * @return bool|string */ public function getActionModelNameByIndexTable($indexTable, $type = 'all') { $indexers = $this->getIndexers(); $actionModelName = false; foreach ($indexers as $indexer) { $prefixedTablrName = $this->_resource->getTableName($indexer->index_table); if ($prefixedTablrName == $indexTable && isset($indexer->action_model->{$type})) { $actionModelName = (string) $indexer->action_model->{$type}; break; } } return $actionModelName; }
/** * Get connection by name or type * * @param string $connectionName * @return Varien_Db_Adapter_Interface|bool */ protected function _getConnection($connectionName) { if (isset($this->_connections[$connectionName])) { return $this->_connections[$connectionName]; } $connectionNameFull = ($this->_resourcePrefix ? $this->_resourcePrefix . '_' : '') . $connectionName; $connectionInstance = $this->_resources->getConnection($connectionNameFull); // cache only active connections to detect inactive ones as soon as they become active if ($connectionInstance) { $this->_connections[$connectionName] = $connectionInstance; } return $connectionInstance; }
/** * Populate ACL with roles from external storage * * @param Magento_Acl $acl */ public function populateAcl(Magento_Acl $acl) { $roleTableName = $this->_resource->getTableName('admin_role'); $adapter = $this->_resource->getConnection('read'); $select = $adapter->select()->from($roleTableName)->order('tree_level'); foreach ($adapter->fetchAll($select) as $role) { $parent = $role['parent_id'] > 0 ? Mage_User_Model_Acl_Role_Group::ROLE_TYPE . $role['parent_id'] : null; switch ($role['role_type']) { case Mage_User_Model_Acl_Role_Group::ROLE_TYPE: $roleId = $role['role_type'] . $role['role_id']; $acl->addRole($this->_objectFactory->getModelInstance('Mage_User_Model_Acl_Role_Group', array('roleId' => $roleId)), $parent); break; case Mage_User_Model_Acl_Role_User::ROLE_TYPE: $roleId = $role['role_type'] . $role['user_id']; if (!$acl->hasRole($roleId)) { $acl->addRole($this->_objectFactory->getModelInstance('Mage_User_Model_Acl_Role_User', array('roleId' => $roleId)), $parent); } else { $acl->addRoleParent($roleId, $parent); } break; } } }
/** * Extract table names and prepare for cli rendering * * @param array $resources * * @return void */ protected function prepareResources(&$resources) { foreach ($resources as $key => &$resource) { try { $table = $this->coreResource->getTableName($resource); } catch (\Mage_Core_Exception $e) { // table not exists $table = false; } if ($table === false) { unset($resources[$key]); } else { $resource = $table; } } }
/** * Test correct event starting and stopping in magento profile during SQL query fail */ public function testProfilerDuringSqlException() { /** @var Zend_Db_Adapter_Pdo_Abstract $connection */ $connection = $this->_model->getConnection('core_read'); try { $connection->query('SELECT * FROM unknown_table'); } catch (Zend_Db_Statement_Exception $exception) { } if (!isset($exception)) { $this->fail("Expected exception didn't thrown!"); } /** @var Mage_Core_Model_Resource $resource */ $resource = Mage::getSingleton('Mage_Core_Model_Resource'); $testTableName = $resource->getTableName('core_resource'); $connection->query('SELECT * FROM ' . $testTableName); /** @var Mage_Core_Model_Resource_Db_Profiler $profiler */ $profiler = $connection->getProfiler(); $this->assertInstanceOf('Mage_Core_Model_Resource_Db_Profiler', $profiler); $queryProfiles = $profiler->getQueryProfiles(Varien_Db_Profiler::SELECT); $this->assertCount(2, $queryProfiles); }
/** * Constructor * * @param Mage_Core_Model_Resource $resource */ public function __construct(Mage_Core_Model_Resource $resource) { $this->_sessionTable = $resource->getTableName('core_session'); $this->_write = $resource->getConnection('core_write'); }
public function testGetFkName() { $this->assertStringStartsWith('FK_', $this->_model->getFkName('sales_flat_creditmemo_comment', 'parent_id', 'sales_flat_creditmemo', 'entity_id')); }
/** Retorna endereços do cliente */ public function getAddressesSelect($customer_id, $scope) { $customer = Mage::getModel('customer/customer')->load($customer_id); $_string_options = ''; // variável para armazenar as informações dos endereços if (count($customer->getAddresses()) > 0) { $default_id = 0; if ($scope == 'billing') { $default_billing_address = $customer->getDefaultBillingAddress(); if (method_exists($default_billing_address, 'getData')) { $default_id = $default_billing_address->getData('entity_id'); } } else { if ($scope == 'shipping') { $default_shipping_address = $customer->getDefaultShippingAddress(); if (method_exists($default_shipping_address, 'getData')) { $default_id = $default_shipping_address->getData('entity_id'); } } } foreach ($customer->getAddresses() as $address) { $info_data = $address->getData(); if (array_key_exists('region_id', $info_data)) { $resource = new Mage_Core_Model_Resource(); $read = $resource->getConnection('core_read'); $select = $read->select()->from('directory_country_region')->where('region_id = ?', $info_data['region_id']); $r = $read->fetchAll($select); $result_set = $r[0]; $region = $result_set['default_name']; } else { $region = ""; } $_string_options .= "<option "; if ($address->getData('entity_id') == $default_id) { $_string_options .= "selected='selected'"; } $_string_options .= "value='" . $info_data['entity_id'] . "' > " . $info_data['firstname'] . " " . $info_data['lastname'] . ",\n\t\t\t\t" . $info_data['street'] . ", " . $info_data['postcode'] . ", " . $info_data['city'] . ", " . $region . " </option> \n"; } $_string_options .= "<option value='0'> Novo Endereço </option>"; return $_string_options; } else { return null; } }
/** * Prepare url rewrite left join statement for given select instance and store_id parameter. * * @param Varien_Db_Select $select * @param int $storeId * @return Enterprise_Catalog_Helper_Product_UrlRewrite */ public function joinTableToSelect(Varien_Db_Select $select, $storeId) { $requestPath = $this->_connection->getIfNullSql('url_rewrite.request_path', 'default_ur.request_path'); $select->joinLeft(array('url_rewrite_product' => $this->_resource->getTableName('enterprise_catalog/product')), 'url_rewrite_product.product_id = main_table.entity_id ' . 'AND url_rewrite_product.store_id = ' . (int) $storeId, array('url_rewrite_product.product_id' => 'url_rewrite_product.product_id'))->joinLeft(array('url_rewrite' => $this->_resource->getTableName('enterprise_urlrewrite/url_rewrite')), 'url_rewrite_product.url_rewrite_id = url_rewrite.url_rewrite_id AND url_rewrite.is_system = 1', array(''))->joinLeft(array('default_urp' => $this->_resource->getTableName('enterprise_catalog/product')), 'default_urp.product_id = main_table.entity_id AND default_urp.store_id = 0', array(''))->joinLeft(array('default_ur' => $this->_resource->getTableName('enterprise_urlrewrite/url_rewrite')), 'default_ur.url_rewrite_id = default_urp.url_rewrite_id', array('request_path' => $requestPath)); return $this; }
/** * Join url rewrite to select * * @param Varien_Db_Select $select * @param int $storeId * @return Mage_Catalog_Helper_Category_Url_Rewrite */ public function joinTableToSelect(Varien_Db_Select $select, $storeId) { $select->joinLeft(array('url_rewrite' => $this->_resource->getTableName('core/url_rewrite')), 'url_rewrite.category_id=main_table.entity_id AND url_rewrite.is_system=1 AND ' . $this->_connection->quoteInto('url_rewrite.store_id = ? AND ', (int) $storeId) . $this->_connection->prepareSqlCondition('url_rewrite.id_path', array('like' => 'category/%')), array('request_path' => 'url_rewrite.request_path')); return $this; }
protected function _construct() { $this->_resource = Mage::getSingleton('core/resource'); $this->_connection = $this->_resource->getConnection('core_write'); $this->_table = $this->_resource->getTableName($this->_table); }
/** * Join url rewrite to select * * @param Varien_Db_Select $select * @param int $storeId * @return Enterprise_Catalog_Helper_Category_UrlRewrite */ public function joinTableToSelect(Varien_Db_Select $select, $storeId) { $requestPath = $this->_connection->getIfNullSql('url_rewrite.request_path', 'default_ur.request_path'); $select->joinLeft(array('url_rewrite_category' => $this->_resource->getTableName('enterprise_catalog/category')), 'url_rewrite_category.category_id = main_table.entity_id' . ' AND ' . $this->_connection->quoteInto('url_rewrite_category.store_id = ?', (int) $storeId), array(''))->joinLeft(array('url_rewrite' => $this->_resource->getTableName('enterprise_urlrewrite/url_rewrite')), 'url_rewrite_category.url_rewrite_id = url_rewrite.url_rewrite_id AND url_rewrite.is_system = 1', array(''))->joinLeft(array('default_urc' => $this->_resource->getTableName('enterprise_catalog/category')), 'default_urc.category_id = url_rewrite_category.category_id AND default_urc.store_id = 0', array(''))->joinLeft(array('default_ur' => $this->_resource->getTableName('enterprise_urlrewrite/url_rewrite')), 'default_ur.url_rewrite_id = default_urc.url_rewrite_id AND default_ur.is_system = 1', array('request_path' => $requestPath)); return $this; }
/** * Get a list of host names for the supplied database resource. * * @param \Mage_Core_Model_Resource $resource * @return array */ public function getByResource(\Mage_Core_Model_Resource $resource) { return $this->getByConnection($resource->getConnection('core_read')); }