Posted almost 10 years ago. Visible to the public.

Collection Manipulation (Filtering, Limiting, etc)

A list of collection methods, similar to this Archive

Where

OR:

You have to first specify an array of attributes in the filter, then a two dimensional array, with each second dimension have the filters for attribute n, as below:

Copy
$this->addFieldToFilter( array('email', 'customer_id'), array( array('eq' => $customer->getEmail()), // filters for e-mail array('eq' => $customer->getId()), // filters for customer_id ) );

The above results in this SQL:

Copy
SELECT `main_table`.* FROM `wherever` AS `main_table` WHERE ((email = 'noone@nowhere.com') OR (customer_id = '10'))

Order

Don't get fooled by the lack of an order statement on the SQL output, even when one is added, as below:

Copy
$collection->setOrder('name', 'ASC'); echo (string) $collection->getSelect();

The reason that the ORDER BY statement is not in the select is that orders get added to the select upon load(), so this would work:

Copy
$collection->setOrder('name', 'ASC'); $collection->load(); echo (string) $collection->getSelect();

Limit

The same applies to limit as does to order in respect of statement not being present until after load() is called.

Copy
$collection->getSelect()->limit(1);

Owner of this card:

Avatar
Mike Whitby
Last edit:
about 7 years ago
by Mike Whitby
Posted by Mike Whitby to Magento
This website uses short-lived cookies to improve usability.
Accept or learn more