我在magento 1中使用了fetch_assoc()方法。 我想将其转换为Magento 2。 magento 2中没有fetch_assoc()方法。
[Nederland, Nederland, Verenigde Arabische Emiraten, Maleisië, Verenigde Staten van Amerika]
答案 0 :(得分:0)
我不确定我提出的解决方案是否对您有用,但是在Magento 2中获取数据的最佳方法是基于def data_encoder(text):
if len(text)>0:
message = base64.urlsafe_b64decode(text)
message = str(message, 'utf-8')
# message = quopri.decodestring(message).decode('utf8')
message = email.message_from_string(message)
return message
def readMessage(content)->str:
message = None
if "data" in content['payload']['body']:
message = content['payload']['body']['data']
message = data_encoder(message)
elif "data" in content['payload']['parts'][0]['body']:
message = content['payload']['parts'][0]['body']['data']
message = data_encoder(message)
else:
print("body has no data.")
return message
和Models
。
步骤1:首先,您必须在模块中创建一个Collections
文件
Model
第2步:在您的自定义模块中创建<?php
namespace <Vendor_Name>\<Module_Name>\Model;
use Magento\Framework\Model\AbstractModel;
class Data extends AbstractModel
{
protected function _construct()
{
$this->_init('<Vendor_Name>\<Module_Name>\Model\ResourceModel\Data');
}
}
文件
ResourceModel
步骤3:创建<?php
namespace <Vendor_Name>\<Module_Name>\Model\ResourceModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Data extends AbstractDb
{
protected function _construct()
{
// Second parameter is a primary key of the table
$this->_init('Table_Name', 'id');
}
}
文件以初始化Collection
和Model
文件。
ResourceModel
步骤4:最后,您需要做的是在同一模块中创建一个namespace <Vendor_Name>\<Module_Name>\Model\ResourceModel\Data;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection
{
protected function _construct()
{
$this->_init(
'<Vendor_Name>\<Module_Name>\Model\Data',
'<Vendor_Name>\<Module_Name>\Model\ResourceModel\Data'
);
}
}
文件并利用集合,如下所示:
Block
如果您不想实现基于namespace <Vendor_Name>\<Module_Name>\Block;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
use <Vendor_Name>\<Module_Name>\Model\Data as DataCollection;
class Custom_Module extends Template
{
protected $dataCollection;
public function __construct(Context $context, DataCollection $dataCollection)
{
$this->_dataCollection = $dataCollection;
parent::__construct($context);
}
public function getDataCollecton()
{
$collection = $this->_dataCollection->getCollection();
return $collection;
}
}
和fetchAll
的解决方案,也可以在Magento 2中使用fetch_assoc()
代替models
,例如: / p>
collections
作为参考,您也可以查看Magento2 – Write Custom Mysql Query (Without Using Model)
答案 1 :(得分:0)
在magento 2中,您可以使用相同的控件,但是为此您需要创建数据库连接。 我建议您使用资源或集合模型来获取结果,如果要以对象格式获取第一行,则应使用 getFirstItem();
答案 2 :(得分:0)
我认为我们可以使用以下内容:
$adapter = $this->resourceConnection->getConnection($resource);
$stmt = $adapter->query($sql);
// Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
$results = $stmt->fetchAll(\Zend_Db::FETCH_ASSOC);
或者如果我们有\ Magento \ Framework \ DB \ Adapter \ AdapterInterface的$ connection实例
$connection->fetchAll($sql, $binds, \PDO::FETCH_ASSOC);
使用这些,我认为您将获得与magento 1 fetch_assoc相同的结果