7

I'm using below code

$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);

It's working fine. It returns all customer fields. I would like to select only First Name, Last Name & Email.

Instead of select(), which function we need to use & how?

13

Try following way:

$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);
| improve this answer | |
  • How will we add where condition in this? – Amrit Pal Singh May 22 '18 at 6:09
  • 1
    @AmritPalSingh - Taking reference from Sohel Rana's query after select statement(& before fetchAll) you can add where clause ie. $select->where('customer_id = ?', (int)$cust_id); – GunJan Mehta Nov 26 '18 at 6:31
2

Use below code for select particular field from database table.

File path: 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;
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.