【发布时间】: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