Magento Chapagain Collection
Magento Chapagain Collection
Here is a quick tip to add OR query and AND query to collection object. I am implementing this on
product collection.
// PRODUCT COLLECTION
$collection = Mage::getModel('catalog/product')->getCollection();
Filtering collection to select only those products whose sku is like ‘ch’ AND status is equal to 1 (i.e.
enabled status).
// AND QUERY
$collection->addAttributeToFilter('sku', array('like' => '%ch%'));
$collection->addAttributeToFilter('status', array('eq' => '1'));
Filtering collection to select only those products whose sku is like ‘ch’ OR status is equal to 1 (i.e.
enabled status).
// OR QUERY
$collection->addAttributeToFilter(array(
array(
'attribute' => 'sku',
'like' => '%ch%'),
array(
'attribute' => 'status',
'eq' => '1')
));
You may check the query statement with the following code
$collection->printLogQuery(true);
/* Identify referer url via all accepted methods (HTTP_REFERER, regular or base64-encoded request
param) */
_getRefererUrl()
Example:
You can use the redirect functions in your controller class. Like below:-
$this->_redirect($path, $arguments=array());
OR,
$this->_redirectUrl($url);
$url = "http://example.com";
Mage::app()->getFrontController()->getResponse()->setRedirect($url);
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl($path,
$arguments));
Magento: Join, filter, select and sort attributes, fields and tables
In my previous article (Magento: Very Useful Collection Functions), I had written about database
interaction functions present in class Varien_Data_Collection_Db.
Here, I am going to explain some database interaction functions present in the class
Mage_Eav_Model_Entity_Collection_Abstract. These collection functions are very useful to select
data from Magento database. We need them almost all the time for filtering collection object.
Below are some of the useful functions that we use most often.
Class: Mage_Eav_Model_Entity_Collection_Abstract
/**
* Add attribute filter to collection
*
* If $attribute is an array will add OR condition with following format:
* array(
* array(‘attribute’=>’firstname’, ‘like’=>’test%’),
* array(‘attribute’=>’lastname’, ‘like’=>’test%’),
*)
*
* @see self::_getConditionSql for $condition
* @param Mage_Eav_Model_Entity_Attribute_Interface|integer|string|array $attribute
* @param null|string|array $condition
* @param string $operator
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
addAttributeToFilter($attribute, $condition=null, $joinType=’inner’)
addAttributeToSelect: gets the value for $attribute in the SELECT clause; specify * to get all attributes
(i.e. to execute SELECT *)
/**
* Add attribute to entities in collection
*
* If $attribute==’*’ select all attributes
*
* @param array|string|integer|Mage_Core_Model_Config_Element $attribute
* @param false|string $joinType flag for joining attribute
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
addAttributeToSelect($attribute, $joinType=false)
If an array is passed but no attribute code specified, it will be interpreted as a group of OR conditions that
will be processed in the same way.
If no attribute code is specified, it defaults to eq.
$collection = Mage::getModel('catalog/product')->getCollection();
// using IN statement,
// i.e. selecting only those items whose ID fall in the given array
$collection->addAttributeToFilter('id', array('in' => array(1, 14, 51, 52)));
// selecting only those items whose ID is greater than the given value
$collection->addAttributeToFilter('id', array('gt' => 5));
// Add OR condition:
$collection->addAttributeToFilter(array(
array(
'attribute' => 'field_name',
'in' => array(1, 2, 3),
),
array(
'attribute' => 'date_field',
'from' => '2010-09-10',
),
));
Below is the full filter condition codes with attribute code and its sql equivalent
eq : =
neq : !=
like : LIKE
nlike : NOT LIKE
in : IN ()
nin : NOT IN ()
is : IS
notnull : IS NOT NULL
null : IS NULL
moreq : >=
gt : >
lt : <
gteq : >=
lteq : <=
finset : FIND_IN_SET()
from : >= (for use with dates)
to : <= (for use with dates)
date : optional flag for use with from/to to specify that comparison value should first be converted to a
date
datetime : optional flag for use with from/to to specify that comparison value should first be converted to
a datetime
addFieldToFilter: alias for addAttributeToFilter(). This filters the database table fields.
/**
* Wrapper for compatibility with Varien_Data_Collection_Db
*
* @param mixed $attribute
* @param mixed $condition
*/
addFieldToFilter($attribute, $condition=null)
/**
* Add attribute to sort order
*
* @param string $attribute
* @param string $dir
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
addAttributeToSort($attribute, $dir=’asc’)
/**
* Add attribute expression (SUM, COUNT, etc)
*
* Example: (‘sub_total’, ‘SUM({{attribute}})’, ‘revenue’)
* Example: (‘sub_total’, ‘SUM({{revenue}})’, ‘revenue’)
*
* For some functions like SUM use groupByAttribute.
*
* @param string $alias
* @param string $expression
* @param string $attribute
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
addExpressionAttributeToSelect($alias, $expression, $attribute)
/**
* Groups results by specified attribute
*
* @param string|array $attribute
*/
groupByAttribute($attribute)
joinAttribute: joins another entity and adds attribute from joined entity, using $alias, to SELECT clause.
$alias = selected field name. You can keep it’s name whatever you want.
$bind = attribute code of the main entity to link to the joined entity.
/**
* Add attribute from joined entity to select
*
* Examples:
* (‘billing_firstname’, ‘customer_address/firstname’, ‘default_billing’)
* (‘billing_lastname’, ‘customer_address/lastname’, ‘default_billing’)
* (‘shipping_lastname’, ‘customer_address/lastname’, ‘default_billing’)
* (‘shipping_postalcode’, ‘customer_address/postalcode’, ‘default_shipping’)
* (‘shipping_city’, $cityAttribute, ‘default_shipping’)
*
* Developer is encouraged to use existing instances of attributes and entities
* After first use of string entity name it will be cached in the collection
*
* @todo connect between joined attributes of same entity
* @param string $alias alias for the joined attribute
* @param string|Mage_Eav_Model_Entity_Attribute_Abstract $attribute
* @param string $bind attribute of the main entity to link with joined $filter
* @param string $filter primary key for the joined entity (entity_id default)
* @param string $joinType inner|left
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
joinAttribute($alias, $attribute, $bind, $filter=null, $joinType=’inner’, $storeId=null)
/**
* Join a table
*
* @param string|array $table
* @param string $bind
* @param string|array $fields
* @param null|array $cond
* @param string $joinType
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
joinTable($table, $bind, $fields=null, $cond=null, $joinType=’inner’)
$collection = Mage::getModel('sales/order_invoice_item')
->getCollection()
->joinTable('sales_order_entity', 'entity_id=parent_id',
array('invoice_id'=>'increment_id', 'store_id' => 'store_id'), null , 'left')
->joinAttribute('order_id', 'invoice/order_id', 'parent_id',
null, 'left')
->joinAttribute('product_name', 'invoice_item/name', 'entity_id',
null, 'left')
->joinAttribute('store_id', 'invoice/store_id', 'parent_id',
null, 'left')
->joinTable('sales_order', 'entity_id=order_id',
array('order_status'=>'status'), null , 'left')
;
/**
* Join regular table field and use an attribute as fk
*
* Examples:
* (‘country_name’, ‘directory/country_name’, ‘name’, ‘country_id=shipping_country’,
“{{table}}.language_code=’en’”, ‘left’)
*
* @param string $alias ‘country_name’
* @param string $table ‘directory/country_name’
* @param string $field ‘name’
* @param string $bind ‘PK(country_id)=FK(shipping_country_id)’
* @param string|array $cond “{{table}}.language_code=’en’” OR array(‘language_code’=>’en’)
* @param string $joinType ‘left’
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
joinField($alias, $table, $field, $bind, $cond=null, $joinType=’inner’)
removeAttributeToSelect: removes $attribute from SELECT clause; specify null to remove all attributes
/**
* Remove an attribute from selection list
*
* @param string $attribute
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
removeAttributeToSelect($attribute=null)
setPage: sets LIMIT clause by specifying page number (one-indexed) and number of records per page;
equivalent to calling setCurPage($pageNum) and setPageSize($pageSize)
/**
* Set collection page start and records to show
*
* @param integer $pageNum
* @param integer $pageSize
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
setPage($pageNum, $pageSize)
/**
* Import 2D array into collection as objects
*
* If the imported items already exist, update the data for existing objects
*
* @param array $arr
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
importFromArray($arr)
/**
* Get collection data as a 2D array
*
* @return array
*/
exportToArray()
setOrder: alias for addAttributeToSort() q.v., identical except that it can accept array of attributes, and
default $dir is desc
/**
* Set sorting order
*
* $attribute can also be an array of attributes
*
* @param string|array $attribute
* @param string $dir
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
setOrder($attribute, $dir=’desc’)