Magento 2 Как выбрать поля в запросе


Я использую код ниже

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$select = $connection->select()
        ->from(
        ['ce' => 'customer_entity']
);
$data = $connection->fetchAll($select);

Все работает нормально. Он возвращает все поля клиента. Я хотел бы выбрать только Имя, фамилию и адрес электронной почты.

Вместо select(), какую функцию нам нужно использовать и как?

Author: Jackson, 2016-08-19

2 answers

Попробуйте сделать это следующим образом:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$select = $connection->select()
    ->from(
        ['ce' => 'customer_entity'],
        ['firstname', 'lastname', 'email']
    );
$data = $connection->fetchAll($select);
 13
Author: Sohel Rana, 2016-08-19 10:18:25

Используйте приведенный ниже код для выбора определенного поля из таблицы базы данных.

Путь к файлу: magento/app/code/Venodr/ModuleName/Model/CustomerData.php

<?php

namespace Venodr\ModuleName\Model;

use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;

class CustomerData
{
    protected $connection;

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

    public function getQueryData()
    { 
        $query = $this->connection->fetchAll("SELECT firstname, lastname, email FROM customer_entity");
        return $query;
    }
}
 2
Author: Kirti Nariya, 2020-02-03 05:17:45