【问题标题】:How to fetch db table values using mysql query in magento 2. Magento 2 use sql query?如何在 magento 2 中使用 mysql 查询获取 db 表值。 Magento 2 使用 sql 查询?
【发布时间】:2020-12-22 15:28:21
【问题描述】:

我是 Magento 的新手。

CustomModule\Database\Controller\Page\Index.php

<?php
namespace CustomModule\Database\Controller\Page;
class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory)
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        return $this->_pageFactory->create();
    }
}

CustomModule\Database\Block\Index.php

<?php

namespace CustomModule\Database\Block;

use Magento\Framework\View\Element\Template;

class Index extends Template
{
 
    /**
     * @var \Magento\Framework\App\ResourceConnection
     */
    public $resourceConnection;
 
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\ResourceConnection $resourceConnection
    ) {
        $this->resourceConnection = $resourceConnection;
        parent::__construct($context);
    }
 
    /**
     * Get Table name using direct query
     */
    public function getTablename($tableName)
    {
        /* Create Connection */
        $connection  = $this->resourceConnection->getConnection();
        $tableName   = $connection->getTableName($tableName);
        return $tableName;
    }
}

CustomModule\Database\view\frontend\templates\index.phtml

<?php
$tableName = $this->getTableName('sales_invoice_item');
$query = 'SELECT name,sku,price,qty FROM' . $tableName;
/**
 * Execute the query and store the results in $results variable
 */
$results = $this->resourceConnection->getConnection()->fetchCol($query);
echo "<pre>";print_r($results);
?>

显示错误。

1 exception(s):
Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: CustomModule\Database\Block\Index

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: CustomModule\Database\Block\Index
<pre>#1 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('CustomModule\Dat...', array('data' => array())) called at [vendor\magento\framework\ObjectManager\ObjectManager.php:56]
#2 Magento\Framework\ObjectManager\ObjectManager->create('CustomModule\Dat...', array('data' => array())) called at [vendor\magento\framework\View\Element\BlockFactory.php:46]
#3 Magento\Framework\View\Element\BlockFactory->createBlock('CustomModule\Dat...', array('data' => array())) called at [vendor\magento\framework\View\Layout\Generator\Block.php:272]
#4 Magento\Framework\View\Layout\Generator\Block->getBlockInstance('CustomModule\Dat...', array('data' => array())) called at [vendor\magento\framework\View\Layout\Generator\Block.php:252]
#5 Magento\Framework\View\Layout\Generator\Block->createBlock('CustomModule\Dat...', 'index', array('data' => array())) called at [vendor\magento\framework\View\Layout\Generator\Block.php:229]
#6 Magento\Framework\View\Layout\Generator\Block->generateBlock(&Magento\Framework\View\Layout\ScheduledStructure#0000000006b3bc800000000037a2235d#, &Magento\Framework\View\Layout\Data\Structure#0000000006b3b9d10000000037a2235d#, 'index') called at [vendor\magento\framework\View\Layout\Generator\Block.php:134]
#7 Magento\Framework\View\Layout\Generator\Block->process(&Magento\Framework\View\Layout\Reader\Context#0000000006b3b33b0000000037a2235d#, &Magento\Framework\View\Layout\Generator\Context#0000000006b3bc8d0000000037a2235d#) called at [vendor\magento\framework\View\Layout\GeneratorPool.php:81]
#8 Magento\Framework\View\Layout\GeneratorPool->process(&Magento\Framework\View\Layout\Reader\Context#0000000006b3b33b0000000037a2235d#, &Magento\Framework\View\Layout\Generator\Context#0000000006b3bc8d0000000037a2235d#) called at [vendor\magento\framework\View\Layout.php:352]
#9 Magento\Framework\View\Layout->generateElements() called at [vendor\magento\framework\Interception\Interceptor.php:58]
#10 Magento\Framework\View\Layout\Interceptor->___callParent('generateElements', array()) called at [vendor\magento\framework\Interception\Interceptor.php:138]
#11 Magento\Framework\View\Layout\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor\magento\framework\Interception\Interceptor.php:153]
#12 Magento\Framework\View\Layout\Interceptor->___callPlugins('generateElements', array(), array(array('layout-model-cac...', 'core-session-dep...', 'customer-session...', 'catalog-session-...', 'persistent-sessi...', 'checkout-session...', 'tax-session-depe...'))) called at [generated\code\Magento\Framework\View\Layout\Interceptor.php:91]
#13 Magento\Framework\View\Layout\Interceptor->generateElements() called at [vendor\magento\framework\View\Layout\Builder.php:129]
#14 Magento\Framework\View\Layout\Builder->generateLayoutBlocks() called at [vendor\magento\framework\View\Page\Builder.php:55]
#15 Magento\Framework\View\Page\Builder->generateLayoutBlocks() called at [vendor\magento\framework\View\Layout\Builder.php:65]
#16 Magento\Framework\View\Layout\Builder->build() called at [vendor\magento\framework\View\Page\Config.php:224]
#17 Magento\Framework\View\Page\Config->build() called at [vendor\magento\framework\View\Page\Config.php:237]
#18 Magento\Framework\View\Page\Config->publicBuild() called at [generated\code\Magento\Framework\View\Page\Config\Interceptor.php:37]
#19 Magento\Framework\View\Page\Config\Interceptor->publicBuild() called at [vendor\magento\framework\View\Result\Page.php:242]
#20 Magento\Framework\View\Result\Page->render(&Magento\Framework\App\Response\Http\Interceptor#0000000006b3bbde0000000037a2235d#) called at [vendor\magento\framework\View\Result\Layout.php:171]
#21 Magento\Framework\View\Result\Layout->renderResult(&Magento\Framework\App\Response\Http\Interceptor#0000000006b3bbde0000000037a2235d#) called at [vendor\magento\framework\Interception\Interceptor.php:58]
#22 Magento\Framework\View\Result\Page\Interceptor->___callParent('renderResult', array(&Magento\Framework\App\Response\Http\Interceptor#0000000006b3bbde0000000037a2235d#)) called at [vendor\magento\framework\Interception\Interceptor.php:138]
#23 Magento\Framework\View\Result\Page\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Response\Http\Interceptor#0000000006b3bbde0000000037a2235d#) called at [vendor\magento\framework\Interception\Interceptor.php:153]
#24 Magento\Framework\View\Result\Page\Interceptor->___callPlugins('renderResult', array(&Magento\Framework\App\Response\Http\Interceptor#0000000006b3bbde0000000037a2235d#), array(array('result-messages', 'result-builtin-c...', 'result-varnish-c...'))) called at [generated\code\Magento\Framework\View\Result\Page\Interceptor.php:130]
#25 Magento\Framework\View\Result\Page\Interceptor->renderResult(&Magento\Framework\App\Response\Http\Interceptor#0000000006b3bbde0000000037a2235d#) called at [vendor\magento\framework\App\Http.php:120]
#26 Magento\Framework\App\Http->launch() called at [generated\code\Magento\Framework\App\Http\Interceptor.php:24]
#27 Magento\Framework\App\Http\Interceptor->launch() called at [vendor\magento\framework\App\Bootstrap.php:261]
#28 Magento\Framework\App\Bootstrap->run(&Magento\Framework\App\Http\Interceptor#0000000006b3bbd70000000037a2235d#) called at [index.php:39]
</pre>

【问题讨论】:

  • edit你的问题:澄清问题的标题,更多细节或澄清问题,提高内容质量,改进文本排列,改进代码格式,包括当前结果,包括预期结果。见How to Ask

标签: php mysql magento magento2


【解决方案1】:

您可以使用以下代码通过 MySQL 查询从数据库表中获取数据:

protected $_resource;

public function __construct(
    \Magento\Framework\App\ResourceConnection $resource
) {
    $this->_resource = $resource;
}

public function execute()
{
    // get connection
    $connection     = $this->_resource->getConnection();
    // table name
    $tableName = $this->_resource->getTableName('your table name');
    // Query
    $select_sql = "Select * FROM " . $tableName;
    // fetch result
    $results = $connection->fetchAll($select_sql);
    // print result
    print_r($results);
}

【讨论】:

  • @ZubairShaikh 如果这个答案对您有用,请接受。
【解决方案2】:

简短的回答是:你不知道。
Magento 2 为您提供集合和工厂来访问数据库资源,而不需要您像任何现代框架那样编写任何普通的 SQL 查询。 您可以像这样将 CollectionFactory 类注入到您的 Classes 构造函数中:

   /** @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory */
   private $productCollectionFactory;

   public function __construct(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory) {
      $this->productCollectionFactory = $productCollectionFactory;
   }

这样,您可以获得任何发票、订单、产品、产品类别等的对象表示。
但是,如果你真的需要直接运行 SQL,Amit Saini 的答案是可行的。
我在上面找到了this source

【讨论】:

  • 谢谢@L。 Gerhardt 指导我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多