4
votes

I have created a new product with a custom data attribute.

$product = Mage::getModel('catalog/product');
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setName('name');
$product->setDescription('description');
$product->setPrice(1.24);
$product->setData('lid', 123);
$product->save

Now I want to query for this product using Mage::getModel('catalog/product').

How can I query for products with 'lid' 123?

How can I query for products where 'lid' exists aka not null?

================

Error log when performing query with addAttributeToFilter.

( ! ) Fatal error: Call to a member function getBackend() on boolean in /Users/asdf/Sites/magento19.dev/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816
Call Stack
#   Time    Memory  Function    Location
1   0.0002  249016  {main}( )   .../index.php:0
2   0.0024  514024  Mage::run( )    .../index.php:87
3   0.0085  1433456 Mage_Core_Model_App->run( ) .../Mage.php:684
4   0.0729  7141088 Mage_Core_Controller_Varien_Front->dispatch( )  .../App.php:354
5   0.0776  7404488 Mage_Core_Controller_Varien_Router_Standard->match( )   .../Front.php:172
6   0.0825  7941968 Mage_Core_Controller_Varien_Action->dispatch( ) .../Standard.php:250
7   0.1205  10859488    Asdf_Sync_Adminhtml_SyncbackendController->indexAction( )   .../Action.php:418
8   0.1535  13447744    Asdf_Sync_Adminhtml_SyncbackendController->performSync( )   .../SyncbackendController.php:18
9   0.1542  13504568    Asdf_Sync_Helper_Data->pushProducts( )  .../SyncbackendController.php:33
10  0.3723  17554640    Mage_Catalog_Model_Resource_Product_Collection->addAttributeToFilter( ) .../Data.php:67
11  0.3723  17555008    Mage_Eav_Model_Entity_Collection_Abstract->addAttributeToFilter( )  .../Collection.php:1438
12  0.3723  17555360    Mage_Eav_Model_Entity_Collection_Abstract->_getAttributeConditionSql( ) .../Abstract.php:321
13  0.3723  17555472    Mage_Eav_Model_Entity_Abstract->isAttributeStatic( )    .../Abstract.php:1379
3
have you created a product attribute which code is lid?OSdave
No I have not. Can this be done in PHP? and I assume this has to be done at module installation?Pim Reijersen

3 Answers

2
votes

For create attribute run this script in your magento root directory (You can edit it by your requirements)

<?php  

require_once('app/Mage.php');
 Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();                   

$installer->addAttribute('catalog_product', 'lid', array(
             'label'             => 'LId',
             'type'              => 'int',
             'input'             => 'text',
             'backend'           => '',
             'frontend'          => '',
             'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
             'visible'           => true,
             'required'          => false,
             'user_defined'      => false,
             'searchable'        => false,
             'filterable'        => false,
             'comparable'        => false,
             'visible_on_front'  => true,
             'visible_in_advanced_search' => false,
             'unique'            => false
));
$installer->endSetup();

?>

Now your attribute is created see i have created
Then set value you want see

Try This for filter product by Attribute.

$product = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter('lid', 123);

foreach ($product as $products) { 
    echo '<pre>';
    print_r($products);
}

Get product with Lid Exist

$product = Mage::getModel('catalog/product')
    ->getCollection();


foreach ($product as $products) { 
    if($products->getLid()){
       echo $products->getName().'-->'.$products->getLid().'<br>';
    }
}
0
votes

If there is only one product can be loaded with

$product = Mage::getModel('catalog/product')->load(123,'lid')

If you need to load a collection

$product = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter('lid', 123);
0
votes

In theory you can filter a product collection:

$product = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter('lid', 123);

foreach ($product as $products) { /** @var Mage_Catalog_Model_Product $product */
    // Product Collection Here
}