Magento 1.9 – How to Get All Relevant Data from Customer Table

magento-1.9PHP

I'm creating a backup script in magento, and would like to export all customer data from the table ('customer / customer'). How could I get all the data from this table? I tried to use:

 $user_loaded = Mage::getModel('customer/customer')->load($id)->getData();
 echo $user_loaded. 'test: '; 

to see what I would get I just get a test: Array. test: Array. test: Array how could I turn this into the information I need? Example, name, lastname, email etc..

Or would I have to specify every field I need?

$name = $user_loaded->getFirstname();
$lastname = $user_loaded->getLastname();

If I have to do one by one does someone have the data that is needed to pull a complete backup? I do not want to do a dump backup for the fact of changing the tables, I just like to insert data, not change tables..

Would I have how do I pull everything from the table ('customer/customer') to a text file?

Best Answer

You're actually almost there but it's important to know whats happening. If you using the getCollection method you're actually building a query. Go ahead and try the following

$collection = mage::getModel('customer/customer')->getCollection();
var_dump((string)$collection->getSelect());

which will return you to following

SELECT e.* FROM customer_entity AS e WHERE (e.entity_type_id = '1')

This is the default query for a customer collection. As you might notice without specifying fields to retrieve it retrieves everything. So lets add some fields!

$collection = mage::getModel('customer/customer')->getCollection()
   ->addAttributeToSelect('email')
   ->addAttributeToFilter('firstname', 'sander')
   ->addAttributeToSort('email', 'ASC');
var_dump((string)$collection->getSelect());

This will print the following query

SELECT e.*, at_firstname.value AS firstname FROM customer_entity AS e INNER JOIN customer_entity_varchar AS at_firstname ON (at_firstname.entity_id = e.entity_id) AND (at_firstname.attribute_id = '5') WHERE (e.entity_type_id = '1') AND (at_firstname.value = 'sander') ORDER BY e.email ASC

As you can see Magento builds to correct query for you depending on the attributes you add to filter, select, order or whatever you want to do. Check out the Magento Collection wiki page for more on collections because there are a LOT of options you can use.

In your case you just need to specify the addAttributeToSelect so it only retrieves that field. On non EAV collections use addFieldToSelect.

$users = mage::getModel('customer/customer')->getCollection()
           ->addAttributeToSelect('email');

foreach ($users as $user)
   var_dump($user->getData());
Related Topic