0

I am using this code but it's not working with multiple parameter. I want only use load() function in magento. You can see below code any please help me.

$attributes = array('entity_id'=> 1684, 'advertise_id'=> 283);
$model= Mage::getModel('catalog/product')->load($attributes);

Thanks for support in advance.

  • Why multiple parameters? You only need one for load function, that is your entity_id. – Prateek Mar 19 '16 at 6:32
  • when i use Mage::getModel('catalog/product')->getCollection(); at that time "advertise_id" attribute not come and when i use load() than coming this attribute. so i am using multiple parameter with load(). what can i do get "advertise_id" come using getCollection(); – Kartik Asodariya Mar 19 '16 at 6:37
  • @KartikAsodariya you can get advertise_id attribute value by load product just using an id. – DRAJI Mar 19 '16 at 6:49
  • I know if i am using single product id. Right ? but still i am using multiple products id so it's not possible and this collection i am using block file – Kartik Asodariya Mar 19 '16 at 6:55
  • You can't get multiple products by load() method. it use for load single product only. – DRAJI Mar 19 '16 at 7:01
2

Based upon your comments, I gather you are trying to iterate over a collection of products IDs and trying to get the 'advertise_id' column. In that case, I'd strongly suggest against load method. Using of load inside a loop will be like killing your mySql with your own hands.

You should try with this code :

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('advertise_id')
    ->addIdFilter($idArray);

foreach($collection as $key => $value) {
    var_dump($value->getData('advertise_id'));
}

Where $idArray is simply the array containing all the IDs of products you are trying to access.

|improve this answer|||||
  • when i use Mage::getModel('catalog/product')->getCollection(); at that time "advertise_id" attribute not come and when i use load() than coming this attribute. so i am using multiple parameter with load(). – Kartik Asodariya Mar 19 '16 at 9:02
  • You have to use addAttributeToSelect for that attribute to come in the collection. If it is still not coming, then check if you are using flat catalog. In that case. open up the attribute and make it available in product listing and then re-index the flat catalog product index. – Prateek Mar 19 '16 at 9:31
1

Hello if you want to load your model with out using primary key column you can use

Mage::getModel('catalog/product')->load("your data",'column_name'); 
|improve this answer|||||
  • Thanks for giving example but it's not my requirement. I know about how to define single value in load(). can you please help me with this parameter $attributes = array('entity_id'=> 1684, 'advertise_id'=> 283); – Kartik Asodariya Mar 19 '16 at 6:50
0

If you are not in product page, you should load product by id.

$_product = Mage::getModel('catalog/product')->load($id)

From this product object, you can get your custom attribute value

$_product->getAdvertiseId();

|improve this answer|||||
  • I know this is working. How many times i told i want multiple products ids using this functionality like load(). but you answer only one parameter – Kartik Asodariya Mar 19 '16 at 7:32
0

you have to load through a collection:

$product = Mage::getModel('catalog/product')
  ->getCollection()
  ->addAttributeToFilter('entity_id', 1684)
  ->addAttributeToFilter('advertise_id', 283)
  ->setPage(1, 1)
  ->getFirstItem();

if ($product->getId()) {
  ...
}

Hope it helps.

EDIT

To solve the problem of advertise_id attribute not belonging to flat table, try this snippet:

$attribute = Mage::getSingleton('eav/config')
  ->getAttribute('catalog_product', 'advertise_id');

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

$productCollection
  ->getSelect()
  ->join(
    array('attributeTable' => $attribute->getBackend()->getTable()), 
    'e.entity_id = attributeTable.entity_id', 
    array('advertise_id' => 'attributeTable.value')
  )
  ->where("attributeTable.attribute_id = ?", $attribute->getAttributeId())
  ->where("attributeTable.value = ?", 283)
;  

$product = productCollection
  ->addIdFilter(1684)
  ->setPage(1, 1)
  ->getFirstItem();

if ($product->getId()) {
  ...
}
|improve this answer|||||
  • when i use Mage::getModel('catalog/product')->getCollection(); at that time "advertise_id" attribute not come and when i use load() than coming this attribute. so i am using multiple parameter with load(). – Kartik Asodariya Mar 19 '16 at 9:01
  • after the getCollection() call try adding ->addAttributeToSelect('advertise_id') – Alessandro Ronchi Mar 19 '16 at 9:05
  • I am using ->addAttributeToSelect('*') but still not coming "advertise_id" attribute – Kartik Asodariya Mar 19 '16 at 9:30
  • Oh that probably means you have flat catalog enabled and advertise_id is not in flat table; in that case you have 2 options: add that attribute in product listing by editing attribute in the Admin Panel or perform a programmatic join of the attribute in the collection. I'm not able now to provide an example being on my mobile device but you should find examples even here on Stack Exchange – Alessandro Ronchi Mar 19 '16 at 9:41
  • Hi @KartikAsodariya did you try the snippet I put in the EDIT section of my answer? Did it work? – Alessandro Ronchi Mar 20 '16 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.