How to list all users
-
16-10-2019 - |
Question
As part of an exercise I am trying to list all users email from my database on one page. So far the closest i've got is
$user = mage::getModel('customer/customer')->getCollection()->getData();
returns
array
0 =>
array
'entity_id' => string '1' (length=1)
'entity_type_id' => string '1' (length=1)
'attribute_set_id' => string '0' (length=1)
'website_id' => string '1' (length=1)
'email' => string 'john.doe@example.com' (length=20)
'group_id' => string '1' (length=1)
'increment_id' => string '000000001' (length=9)
'store_id' => string '1' (length=1)
'created_at' => string '2007-08-30 23:23:13' (length=19)
'updated_at' => string '2008-08-08 12:28:24' (length=19)
'is_active' => string '1' (length=1)
'disable_auto_group_change' => string '0' (length=1)
1 =>
array
'entity_id' => string '2' (length=1)
'entity_type_id' => string '1' (length=1)
'attribute_set_id' => string '0' (length=1)
'website_id' => string '1' (length=1)
'email' => string 'tony09uk@gmail.com' (length=18)
'group_id' => string '1' (length=1)
'increment_id' => null
'store_id' => string '1' (length=1)
'created_at' => string '2013-07-19 14:31:00' (length=19)
'updated_at' => string '2013-07-19 14:31:00' (length=19)
'is_active' => string '1' (length=1)
'disable_auto_group_change' => string '0' (length=1)
but I only want their emails to be listed I have tried the magic getters and setters but noluck (or at least not the way ive used them). I have also tried
$user = mage::getModel('customer/customer')->getCollection()->load();
and
$user = mage::getModel('customer/customer')->getCollection()
->addAttributeToSort('email', 'ASC');
and
$user = mage::getModel('customer/customer')->getCollection()->getEmail()->getData();
and
$user = mage::getModel('customer/customer')->getCollection()->getData();
echo $user->getEmail();
as well as several other variations, now I have got to the point that i am just randomly sticking commands in in the the hope they work, which I do not like doing.
How do I display the email for all my users? (I hope i'm not to far off the mark)
Solution
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
.* FROMcustomer_entity
ASe
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
ASfirstname
FROMcustomer_entity
ASe
INNER JOINcustomer_entity_varchar
ASat_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 BYe
.
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());