2

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

2

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>'; 
  • thanks for your answer @kishan. without using query i need to get datas from that table. is there any possibilities ? – chris Feb 12 '18 at 8:48
  • May we know reason for fetching data without query ? – Dhaval Solanki Feb 12 '18 at 8:59
  • if i use query it showing codingstandard warning like i shown here 102 | WARNING | Possible raw SQL statement "Select * FROM `" | | detected. 105 | WARNING | fetchAll can be memory inefficient for large data | | sets. – chris Feb 12 '18 at 9:03
  • for this i am trying alternative method.if i get datas through collecton file.i can use addFieldtoFilter. and i can fetch my required datas from that table – chris Feb 12 '18 at 9:06
  • You can use condition for filter as per my updated answer. – Kishan Patadia Feb 12 '18 at 9:10
0

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.

  • can you please explain briefly? – chris Feb 12 '18 at 8:43
  • is there any way to get this table datas by using collection.php ? – chris Feb 12 '18 at 8:46
  • Alternatively you can use SQL select as per my answer. Please check. – Kishan Patadia Feb 12 '18 at 8:48
  • I think answer is no, there is no way to get that result as a collection, because collection require the id which missed in this table :( In case you want take direct response from the table please use answer given by @kishan-patadia . – Sergey Uchuhlebov Feb 12 '18 at 8:49
  • 1
    As I said, to get all data directly from the table you should use answer given by Kishan Patadia. This is the right way to obtain this data. – Sergey Uchuhlebov Feb 12 '18 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.