The problem not in EAV based entities. Method addFieldToFilter()
is available only in Varien_Data_Db_Collection
, that EAV and standard ORM models extend. If you'd like to have your admin grid module working with your collection in the same way as it works with DB collection, you need to implement two methods:
addFieldToFilter($filterName, $conditionArray)
setSort($fieldName, $direction)
Example of such custom collection implementation you can find in Varien_Data_Collection_Filesystem
class that utilizes FS based collections. This kind of collection, for instance, used in Mage_Backup
module to show grid with backup files.
Sincerely,
Ivan
To change the query you can do
$collection->getSelect()->doWhateverYouWantWithSelectObject();
foreach($collection as $order) {} // order collection
But to join a table, you can just use $collection->joinTable()
joinTable
only exists on Mage_Eav_Model_Entity_Collection, this means: products, categories, orders, invoices, creditmemo, shipping, customers. (Thanks @Fra for the comment)
joinTable()
But magento collections have a method joinTable()
it took me 45min to fizzle out how it is used. To avoid this for you, I share it.
$productCollection->joinTable(
array('bonus' => 'mycompany/bonus'), 'product_id=entity_id',
array('bonus_id' => 'bonus_id')
);
The parameters are:
public function joinTable($table, $bind, $fields = null, $cond = null, $joinType = 'inner')
- Table is easy, it is the magento
namespace/entity
format, which you use in your configuration, resource models and the collection. You can use an array of the format array('alias' => 'namespace/entity')
- Bind means, the ON statement in your SQL. This was the hardest part and I will explain it in details later. It is important to have your flat table BEFORE and your EAV table AFTER the equal sign. Don't use
main_table.
before the attribute. Magento will do this for you. More on this later.
- Fields is an array. If you use a string instead, you get this:
Warning: Invalid argument supplied for foreach() in /var/www/magento-1.6.2.0/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php on line 775
. You can use an array of the format array('field1', 'field2', '...')
or array('alias' => 'field1', '...')
- Condition is a **
WHERE ON condition** in the SQL.
- joinType. I hope you know what it is. But you have only the choice between LEFT and INNER.
app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php:793
More on this topic
Best Answer
Mage_Log
is the module whih stores log details in Magento. It mainly logs following detailsIf you dig more deeply, you can see that, you can get
log-visitor
collection. Howeverlog-customer
collection is not available. It is logical since, most of the time, you will only query about logs of a specific customer, not of multiple customers at a time. I think this is why core team of magento didn't include collection forlog-customer
s.To Get Collection Of Customers
Now take a look on the
Mage_Log
module's model section. You can see the entityvisitor
only holds the collection. But through this collection we can accesslog-customer
collection!. What ? I can get log-collection through visitors ? Yes. Because let us have a look onlog_customer
table.Did you see
visitor_id
that is used in the table. This means, every time a user visit, magento will generate a vistor_id for that user and store in the tablelog_visitor
. When a registered user then login to his account using his credentials,log_customer
table will get updated. So each time a customer visit, a uniquevisitor_id
reference will be associated with that user. So due to this, using visitor collection , we can accesslog-customer
collection.For an exmaple a simple query is shown below.
This collection filter will return every
log-customer
who are available for store id = 1. Is that what you need ? :)Wrap up
So in short, you don't need any collection defined for
log-customer
entity. It is intended to access details of one customer at a time. If you need collection, you can use visitor collection for that. That is the correct way to do that :)Hope that make sense