Magento – Getting Customer Names

customermodel

I am trying to display all my users first and last names and email addresses to practice pulling values from the database and joining tables.

I have been trying to dig through the code in app/code/core/Mage/Customer/Model to see how to select specific values from two different tables. I can see the data I want is stored in customer_entity_varchar and customer_entity and is linked by the entity_id column but can't find an example of how I could achieve my desired result.

I have been working on the assumption that collections are the way to do it and so far have this

$model = Mage::getResourceModel('customer/customer_collection');

var_dump($model->getData());

but this shows all the columns and I just want email so I tried this

$model = Mage::getResourceModel('customer/customer_collection');

foreach($model as $cust){
    echo '<br/>' . $cust['email'] . '<br/>';
}

but I'm sure a collection would be a better way of doing it. Also I still need to join customer_entity_varchar but I can't find which model I need to use.

Firstly, how do I select customer email and full name? Secondly, Where should I have been looking in the code to get this answer?

===========

One last attempt I made was

$model = Mage::getResourceModel('customer/customer_collection');
    $model->getSelect()
          ->join(Mage::getConfig()->getTablePrefix() . 'customer', 'main_table.entity_id = ' . Mage::getConfig()->getTablePrefix() . 'customer.entity_id', array('*'));

  var_dump($model->getData());

but got this SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magentostoretest.customer' doesn't exist

Best Answer

The customers collection is an EAV collection. This means you will have to add attributes to select when retrieving the collection. If it's not EAV you can replace addAttributeToSelect for addFieldToSelect.

$collection = Mage::getModel('customer/customer')->getCollection()
   ->addAttributeToSelect('firstname')
   ->addAttributeToSelect('lastname')
   ->addAttributeToSelect('email');

foreach ($collection as $item)
{
   var_dump($item->getData());
   break; // just display one item
}