質問

演習の一環として、1つのページのデータベースからすべてのユーザーのメールをリストしようとしています。これまでのところ、私が持っている最も近いのはです

$user = mage::getModel('customer/customer')->getCollection()->getData();

戻り値

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)

しかし、私は彼らのメールをリストしたいだけです。私は魔法のゲッターとセッターを試してみましたが、ノラック(または少なくとも私が使用した方法ではありません)。私も試しました

    $user = mage::getModel('customer/customer')->getCollection()->load();

    $user = mage::getModel('customer/customer')->getCollection()
                                               ->addAttributeToSort('email', 'ASC');

$user = mage::getModel('customer/customer')->getCollection()->getEmail()->getData();

$user = mage::getModel('customer/customer')->getCollection()->getData();
echo $user->getEmail();

他のいくつかのバリエーションと同様に、今では、彼らが働くことを望んでいることに、私がランダムにコマンドを固定しているという点になりました。

すべてのユーザーにメールを表示するにはどうすればよいですか? (私はそれほど遠くないことを願っています)

役に立ちましたか?

解決

あなたは実際にはほとんどそこにいますが、何が起こっているのかを知ることが重要です。を使用している場合 getCollection 方法実際にクエリを作成します。先に進んで、次のことを試してみてください

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

それはあなたをフォローに返します

選択する e。* から customer_entity なので e どこ (e.entity_type_id = '1')

これは、カスタマーコレクションのデフォルトクエリです。フィールドを指定することなく気付くかもしれません。それで、いくつかのフィールドを追加しましょう!

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

これにより、次のクエリが印刷されます

選択する e.*, at_firstname.value なので firstname から customer_entity なので e 内側の結合 customer_entity_varchar なので at_firstname オン (at_firstname.entity_id = e.entity_id) と (at_firstname.attribute_id = '5')where(e.entity_type_id = '1')および(at_firstname.value = 'Sander')by e.email ASC

ご覧のとおり、Magentoは、フィルター、選択、注文、または何をしたいかに応じて、クエリを修正するためにクエリを修正するためにビルドします。をチェックしてください Magento Collection Wiki コレクションの詳細については、使用できるオプションがたくさんあるためです。

あなたの場合、あなたはただ指定する必要があります addAttributeToSelect そのため、そのフィールドを取得するだけです。非EAVコレクションの使用で addFieldToSelect.

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

foreach ($users as $user)
   var_dump($user->getData());
ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top