Question

Table contains these columns.

enter image description here

I need to get data from this table 'directory_currency_rate'

which is come under this directory

/vendor/magento/module-directory/Model/Currency.php
/vendor/magento/module-directory/Model//ResourceModel/Currency.php

This model file does not have any collection.php file so can't able to get datas from this table.

Anyone know how to get data from this table ?

Thanks in advance

Was it helpful?

Solution

If you can not get using any class or methods, then you can use SQL select statement as below example.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('directory_currency_rate');

// SELECT DATA 
$fields = array('rate');
$sql = $connection->select()                      
                  ->from($tableName, $fields) // to select some particular fields                  
                  ->where('currency_from = ?', 'USD')
                  ->where('currency_to = ?', 'EUR'); // adding WHERE condition with AND
$result = $connection->fetchAll($sql); 
echo '<pre>'; print_r($result); echo '</pre>'; 

OTHER TIPS

You can use the Magento\Directory\Api\CurrencyInformationAcquirerInterface and its method getCurrencyInfo which return all available currencies and its conversion rates.

On the my test host it returns next result:

result

If you want to take plain response from the database I'll recommend you to write own query.

Here the code example which can be run from external script (outside Magento):

<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$currencyInformation = $obj->get('Magento\Directory\Api\CurrencyInformationAcquirerInterface');
$currenciesAndRates = $currencyInformation->getCurrencyInfo();

In your class you should use di: add the Magento\Directory\Api\CurrencyInformationAcquirerInterface in its construct method and use it.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top