magento addAttributeToFilter 的方法和AND OR使用

文章整理来自网络,主要参考有:

http://fishpig.co.uk/magento/tutorials/addattributetofilter/

http://stackoverflow.com/questions/5301231/addattributetofilter-and-or-condition-in-magentos-collection


主要的句子

$_products = Mage::getResourceModel('catalog/product_collection')
   ->addAttributeToSelect(array('name', 'product_url', 'small_image'))
   ->addAttributeToFilter('sku', array('like' => 'UX%'))
    ->load();

 

addAttributeToSelect没什么要说的,只要筛选正确的数据表有的字段就可以

 

eq的两种写法写法

$_products->addAttributeToFilter('status', array('eq' => 1)); // Using the operator
$_products->addAttributeToFilter('status', 1); // Without using the operator

如果上面有不同的筛选,就相当于AND了,OR的写法:

// OR QUERY

$collection->addAttributeToFilter(array(

array(

'attribute' => 'sku','like' => '%ch%'),

array(

'attribute' => 'status','eq' => '1')

));

 

noequal

$_products->addAttributeToFilter('sku', array('neq' => 'test-product'));

 

$_products->addAttributeToFilter('sku', array('like' => 'UX%'));//like

 

$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));//nolike

 

$_products->addAttributeToFilter('id', array('in' => array(1,4,98)));//in

 

$_products->addAttributeToFilter('id', array('nin' => array(1,4,98))); //not in

 

$_products->addAttributeToFilter('description', array('null' => true));  //null

 

$_products->addAttributeToFilter('description', array('notnull' => true)); //not mull

 

$_products->addAttributeToFilter('id', array('gt' => 5));//Greater Than - gt

 

$_products->addAttributeToFilter('id', array('lt' => 5)); //Less Than - lt

 

$_products->addAttributeToFilter('id', array('gteq' => 5)); //Greater Than or Equals To- gteq

 

$_products->addAttributeToFilter('id', array('lteq' => 5)); //Less Than or Equals To - lteq

 

排错方法

// Method 1
Mage::getResourceModel('catalog/product_collection')->load(true);

// Method 2
$collection = Mage::getResourceModel('catalog/product_collection')

echo $collection->getSelect();

 

 

mysql语句根据sku 查询magento的产品名

答案来自http://stackoverflow.com/questions/5323102/magento-products-import-from-database-using-sql-query

第一步,找出各个属性对应的attribute_code

select attribute_code, attribute_id, backend_type from eav_attribute
    where entity_type_id = (select entity_type_id from eav_entity_type where entity_type_code = 'catalog_product')
      and attribute_code in ('name', 'url_path', 'price', 'image', 'description', 'manufacturer');

一般结果为

+----------------+--------------+--------------+
| attribute_code | attribute_id | backend_type |
+----------------+--------------+--------------+
| description    |           61 | text         |
| image          |           74 | varchar      |
| manufacturer   |           70 | int          |
| name           |           60 | varchar      |
| price          |           64 | decimal      |
| url_path       |           87 | varchar      |
+----------------+--------------+--------------+

对应的name是60

第二步

select p.sku, p.entity_id, n.value name
    from catalog_product_entity p
    join catalog_product_entity_varchar n on n.entity_id = p.entity_id
  where n.attribute_id = 60 AND p.sku='001-C';

可以得出结果了,其他属性自己添加查询

再次感谢Joseph Mastey的回答