I cannot find the solution to this problem in magento tutorials.
How can I implement the boolean OR operator in custom models when there are two attributes involved? The example in the official tutorial only demonstrated the use of OR boolean for one field, sku.
$filter_a = array('like'=>'a%');
$filter_b = array('like'=>'b%');
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('sku', array($filter_a, $filter_b))
->getSelect();
This translates to
WHERE e.sku like 'a%' or e.sku like 'b%'
But what if I need to run conditions such as :
WHERE (e.sku like 'a%' or e.sku like 'b%') or (table_price.value >= '10' )
How can I do this on Magento? Thanks
You have your syntax incorrec, try this:
Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter(
array(
array('attribute'=>'firstname', 'like'=>'test%'),
array('attribute'=>'lastname', 'like'=>'test%'),
)
)
you could replace the attribute names by 'sku' or what ever you wanted. Each array entry will be OR'd.
Related
i have managed to do this with addFieldToFilter default functionality like this:
$collection->addFieldToFilter(
array('first_name','last_name'),
array($post['firstName'],$post['lastName'])
);
this is working when i print sql (this is just part from the whole statement being printed):
((first_name = 'r') OR (last_name = 't'))
now i am trying to understand how to use 'like' instead of =.
need some help in understanding that.
your time and answers are highly appericiated thank you.
It's weird, but after some researching into your question the solution is quite simple - it's given in the comments in the Magento code:
In the comment for the method addAttributeToFilter() in Mage/Eav/Model/Entity/Collection/Abstract.php (which finally gets called) you can read:
/**
* 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%'),
* )
*/
Wasn't quite clear to me what this was supposed to mean, but it's as simple: If you want to add an OR condition between the attributes in your statement, then you have to give your parameter for the addAttributeToFilter() method in this way, so in your case:
$collection->addFieldToFilter(
array(
array('attribute'=>'firstname', 'like'=>$post['firstName']),
array('attribute'=>'lastname', 'like'=>$post['lastName'])
));
You can follow this if you look inside the addAttributeToFilter() method:
if (is_array($attribute)) {
$sqlArr = array();
foreach ($attribute as $condition) {
$sqlArr[] = $this->_getAttributeConditionSql($condition['attribute'], $condition, $joinType);
}
$conditionSql = '('.implode(') OR (', $sqlArr).')';
For you googlers, the rigth way to to it is:
$collection->addFieldToFilter(
array('first_name','last_name'),
array(
array('like' => '%'.$post['firstName'].'%'),
array('like' => '%.'$post['lastName'].'%')
)
);
You should pass an array of fields and an array of conditions.
Generated SQL:
... WHERE ((first_name LIKE '%xxxx%') OR (last_name LIKE '%yyy%')) ...
addFieldToFilter( 'sku', array( "like"=>'abc123' ) )
Here you can read much more about Magento collections: http://alanstorm.com/magento_collections
For example, see section named "AND or OR, or is that OR and AND?" in this article.
I am able to run AND and OR boolean conditions on Magento using addFieldToFilter and addAttributeToFilter.
But, I am curious about implementing more complex boolean conditions like below:
WHERE (`sku > 5` AND `price` > '10') OR (`name` = 'books')
for AND condition, I have
$collections = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')
->addFieldToFilter( 'sku', array('gt' => 5 )
->addFieldToFilter('price', array('gt' => 10);
Which translates to the first parenthesis of the WHERE condition:
(`sku` > 5 AND `price` > 10)
I do not know how to proceed from here to express the OR condition
`name` = 'books'
Any help will be appreciated. Thanks
See my earlier post:
complex boolean conditions on Magento
You can create OR by passing an array:
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('price', array('gt' => 10))
->addAttributeToFilter(
array(
array('attribute'=>'firstname', 'like'=>'test%'),
array('attribute'=>'lastname', 'like'=>'test%'),
)
)
Each additional call to addAttributeToFilter() will ANDed, but firstname / lastname will be ORed.
If you need more complex select statements you can always get the select object from the collection and perform adjustments to the query. Look at Varien_Db_Select for hints :)
$collection->getSelect()->join(..)->orWhere(..); // etc
You can even debug the query to see if it's looking ok:
echo $collection->getSelect()->__toString();
I would like to ask you for piece of information that would help me to resolve my issue.
My purpose is to get from the Magento database particular product qty in every order (orders must be in exact definied status). I use batch/script standing apart from Magento but uses Mage:app. I do not know if I should start with models (which seems to be logical approach but slow in the same time) or to work directly on database (which is more difficult).
Thank you for any advice.
Regards,
Querying the database is not that complicated:
SELECT *
FROM sales_flat_order o
LEFT JOIN sales_flat_order_item i ON o.entity_id = i.order_id
WHERE o.status IN ('pending', 'processing')
AND i.product_id = <YOUR_PRODUCT_ID>
total_qty_ordered field in result will represent the ordered quantity.
Getting ordered items quantity through models is not heavy either:
<?php
require_once('app/Mage.php');
umask(0);
Mage::app('default');
$core_resource = Mage::getSingleton('core/resource');
$orders = Mage::getResourceModel('sales/order_collection');
$orders->getSelect()->joinLeft(array('ordered_products' => $core_resource->getTableName('sales/order_item')), 'main_table.entity_id = ordered_products.order_id', array('ordered_products.*'));
$orders->addAttributeToSelect('*')
->addFieldToFilter('status', array('in' => array('pending', 'processing')))
->addAttributeToFilter('main_table.store_id', Mage::app()->getStore()->getId())
->addAttributeToFilter('ordered_products.product_id', array('eq' => '2'));
foreach($orders as $order) {
echo 'Order #' . $order->getId() . ': ' . $order->getData('total_qty_ordered') . '<br/>';
}
The first approach may be a faster but the second one will be Magneto-Upgrade-Safe. So you decide which approach to use.
I would like to filter a collection by relative value per that row. For example,
SELECT * FROM table WHERE column_1 > column_2
The only thing I know how to do in Magento would be
$q = Mage::getModel('table')->getCollection()
->addAttributeToFilter('column_1', array('gt' => $some_number));
or something of that sort. I can only give it a value to compare against, not a column name. I also looked at the Zend_Db_Select method at the where clause but didn't find anything that would help. Do I actually have to go all the way down to a direct SQL query (something which is, of course, avoided at all costs)? (I'm running Magento 1.3.2.4)
Thank you.
Try something like
$q = Mage::getModel('table')->getCollection()
->addAttributeToSelect('column_1')
->addAttributeToSelect('column_2')
->addAttributeToFilter('column_1', array('gt' => Zend_Db_Expr('`column_2`')));
OK, this is probably not the ideal solution, but it's one way of getting what you need.
This is how I got a collection of products that were on sale, where a products final price is lower than its price.
I first made a new empty data collection. Then defined the collection I was going to loop through filtering what I could first. After that I looped through that collection and any products that matched my requirements got added to the empty collection.
$this->_itemCollection = new Varien_Data_Collection();
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addCategoryFilter($category)
->addAttributeToSort('position', 'asc');
$i=0;
foreach($collection as $product){
if($product->getFinalPrice() > $product->getPrice() && !$this->_itemCollection->getItemById($product->getId())){
$this->_itemCollection->addItem($product);
$i++;
}
if($i==$this->getProductsCount()){
break;
}
}
$this->setProductCollection($this->_itemCollection);
i am working on a magento project and i need to get the value according to country wise like select address where country ="Nepal"
can we send the where condition in getCollection() function
$collection = Mage::getModel('relocator/location')->getCollection();
any help will be appreciated
got the solution
;P
Mage::getModel('relocator/location')
->getCollection()
->addFilter('country','Nepal');
You can use diffrent condition like below reference from this.
$collection = Mage::getModel('catalog/product')->getCollection();
Is Equal To
$collection->addAttributeToFilter('status', array('eq' => 1));
Greater Than
$collection->addAttributeToFilter('price', array('gt' => 3));
Contains – with % wildcards
Is NULL
$collection->addAttributeToFilter('sku', array('like' => 'DVD%'));
$collection->addAttributeToFilter('entity_id', array('nin' => array(1,2,12)));