Magento 2 中的直接 SQL 查询

在许多情况下,执行直接原始 SQL 查询会简单且更快,从而导致更优化的 Magento 获取集合查询作为性能基础。

在实体的大型数据集上,保存每个单独的实体可能需要很长时间,而且资源匮乏,因此会使系统无法使用。

克服此问题,可以发出直接 SQL 查询,该查询可以在更短的时间内更新实体的大量数据。

当您使用 Direct SQL 查询时,您无需担心模型/工厂模式。

默认情况下,Magento 将使用带有getConnection()函数的Magento\Framework\App\ResourceConnection类自动连接到其数据库。

使用以下代码片段运行直接 SQL 查询以获取带前缀的表名,

<?php
namespace Path\To\ClassDirectory;

class DirectSqlQuery {

    /**
     * @var \Magento\Framework\App\ResourceConnection
     */
    protected $resourceConnection;

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

    /**
     * Get Table name using direct query
     */
    public function getTablename($tableName)
    {
        /* Create Connection */
        $connection  = $this->resourceConnection->getConnection();
        $tableName   = $connection->getTableName($tableName);
        return $tableName;
    }
}

从模板或其他 PHP 文件调用,

您需要使用 $this->resourceConnection 对象来运行任何 Direct SQL 查询。

从数据库中读取所有记录, 

fetchAll() =>此方法以顺序数组的形式获取所有 SQL 结果行。
这个方法接受一个查询,因为它是第一个参数,执行它,然后将所有结果作为一个数组返回。
使用这个我们可以得到
一个数据库表的所有记录。

<?php
$tableName = $this->getTableName('customer_entity');
$query = 'SELECT * FROM ' . $tableName;
/**
 * Execute the query and store the results in $results variable
 */
$results = $this->resourceConnection->getConnection()->fetchAll($query);
echo "<pre>";print_r($results);

结果将是一个客户实体的所有记录作为一个数组。

fetchCol() =>此方法获取所有 SQL 结果行的第一列作为数组。

<?php
	$tableName = $this->getTableName('customer_entity');
	$query = 'SELECT email FROM ' . $tableName;
	/**
	 * Execute the query and store the email from table in $results
	 */
	$results = $this->resourceConnection->getConnection()->fetchCol($query);
	echo "<pre>";print_r($results);

The result looks like,

[0] => Array
    (
        [email] => abc@abcd.com
    )
[1] => Array
    (
        [email] => helloworld@testtest.com
    )
    ....

fetchOne() =>此方法获取 SQL 结果第一行的第一列。

<?php
	$tableName = $this->getTableName('customer_entity');
	$query = 'SELECT email FROM ' . $tableName . ' limit 1';
	
	$results =$this->resourceConnection->getConnection()->fetchOne($query);
	echo $results; // result will be just value

使用 fetchOne 你只能得到一个结果值。在上面的查询中,您可以获得一个电子邮件 ID。

更新查询: 更新已存在的数据库表中的记录。

$tableName = $this->getTableName('customer_entity'); 
$sql = "UPDATE $tableName SET `suffix` = 'Mr' WHERE $tableName.`entity_id` = 1"; 
$this->resourceConnection->getConnection()->query($sql);

删除查询:从数据库表中删除特定行。

$tableName = $this->getTableName('customer_entity'); 
$sql = "DELETE FROM $tableName WHERE `entity_id` IS 1"; 
$this->resourceConnection->getConnection()->query($sqls);

插入查询:使用行查询在数据库表中插入一条记录。第一个参数是表名
在给定的示例中,我们将在评论表中添加一条记录。

$tableName = $block->getTableName('review'); 
$newdate = date("Ymd H:i:s"); 
$review = [ 
            'created_at' =>$newdate, 
            'entity_id' => 1, 
            'entity_pk_value' => 1, 
            'status_id' => 2 
        ]; 
$this->resourceConnection->getConnection()->insert($tableName, $review);

新记录将插入到review表中。

insertArray 查询:根据列定义将数组插入表中。
仅传递要在第二个参数列中插入值的列,第三个参数是特定列的数据。

在下面的示例中,我们将记录添加到review表中。

$tableName = $block->getTableName('review'); 
$newdate = date("Ymd H:i:s"); 
$reviewColumn = ['entity_id','entity_pk_value','status_id']; 
$reviewData[] = [$newdate,1,4,2]; 

$this->resourceConnection->getConnection()->insertArray($tableName, $reviewColumn,$reviewData);

上面的查询将新记录按列插入到表中。

还有许多其他与数据库相关的行查询功能。您可以从文件Magento/framework/DB/Adapter/AdapterInterface.php中获取所有 SQL 函数

版权属于: sbboke版权所有。

转载时必须以链接形式注明作者和原始出处及本声明。

张贴在magento2教程标签:

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论