Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

whats the difference between addAttributeToFilter and select()
 
number25
Member
 
Total Posts:  64
Joined:  2008-07-12
 

I notice some people grab products like this

$products Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('entity_id',
array(
'in'=> array(123) )
);

and some people grab them like this

$resource Mage::getSingleton('core/resource');
$read $resource->getConnection('catalog_read');



$select $read->select() ->from(array('cp'=>$categoryProductTable))->where('cp.category_id=?'$categoryId)

whats the difference in the two. and how can I get the url afterwards

 
Magento Community Magento Community
Magento Community
Magento Community
 
number25
Member
 
Total Posts:  64
Joined:  2008-07-12
 

Can someone please answer this question if you know it. Ive probably posted over a dozen post and probably 2 have been answered. I really appreciate if you could lend support for the communities sake.

 
Magento Community Magento Community
Magento Community
Magento Community
 
cosmicbandito
Jr. Member
 
Total Posts:  16
Joined:  2008-06-04
 

Well, I’m not sure, if I’m totally right. The second way is just a like a sql request and you will get an data array. But in the first way you can use all the implemented methods of that class, which should be Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection. So you get a ready to go collection of that ‘catalog/product’ model. I think that’s the easier way to get all that product attributes you want.

So if you want to get the product url use something like this:

$products Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter'entity_id',array('in'=> array(123) )
                  ->
addAttributeToSelect(array('name''price''url_key'), 'inner')
);

And then you can use it in your Template like that:

foreach($products as $product){
  
echo $product->getUrlPath();
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Danielc1234
Sr. Member
 
Avatar
Total Posts:  175
Joined:  2008-01-09
Miami, FL - United States
 

So if you want to get the product url use something like this:
$products = Mage::getModel(’catalog/product’)->getCollection();
$products->addAttributeToFilter( ‘entity_id’,array(’in’=> array(123) )
->addAttributeToSelect(array(’name’, ‘price’, ‘url_key’), ‘inner’)
);

And then you can use it in your Template like that:
foreach($products as $product){
echo $product->getUrlPath();
}

So let’s say I wanted to pull in just one attribute value, like lets say ‘manufacturer’ what would I have to do in that case?
Would it be simular to what you show above? I cannot figure it out. I have tried to post as well, but no luck.
And all I want to do is echo the value of ‘manufacturer’ on a page.

Please help!!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
cosmicbandito
Jr. Member
 
Total Posts:  16
Joined:  2008-06-04
 

Did you try:

...
$products->addAttributeToSelect(array('manufacturer'), 'inner')
...

and then to access this for a single object:

...
echo 
$product->getManufacturer();
...

Another way, if you want to diplay this in the catalog product list is to add to your layout declaration in catalog.xml:

...
    <
catalog_category_default>
       ...
       <
block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
         <
action method="addAttribute"><attribute>manufacturer</attribute></action>
          ...
       </
block>
       ...
   </
catalog_category_default>
...

You can access this now in the template/catalog/product/list.phtml file the same way like above.

I hope this could help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Danielc1234
Sr. Member
 
Avatar
Total Posts:  175
Joined:  2008-01-09
Miami, FL - United States
 

This is what I have. And I put your suggestion into the file, but still are getting object errors. Do I have to set up code in the xlm file as well? This file’s name is select.phtml and is used for the select options for a product.
Can you see anything wrong with the code? Thanks for you help.

<?php /* @var $this Mage_Catalog_Block_Product_View_Options_Type_Select */ ?>
<?php $_option 
$this->getOption() ?>
<dt>
    <
label><?php echo  $this->htmlEscape($_option->getTitle()) ?>:</label
    
<?php if ($_option->getIsRequire()): ?> 
        
<span class="required">&nbsp;*</span
        
<?php if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_RADIO || $_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_CHECKBOX): ?> 
            
<span id="options-<?php echo $_option->getId() ?>-container"></span
        
<?php endif; ?> 
    <?php 
endif; ?> 
</dt

<
dd><?php echo $this->getValuesHtml() ?> <?php if ($_option->getTitle() == "Select Color"{echo $this->__('test');} ?> </dd>

<?php $products->addAttributeToSelect(array('manufacturer'), 'inner'?>
<?php 
echo $product->getManufacturer(); ?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
cosmicbandito
Jr. Member
 
Total Posts:  16
Joined:  2008-06-04
 

Ok, that propably won’t work wink

So first of all the reasn why: You make a call for a method on a non object. Here $products->addAttributeToSelect(...) and then another call to another non object: $product->getManufacturer(). You didnÄt declare $products neither $product.

So these lines would work:

//instantiate the class
$products Mage::getModel('catalog/product')->getCollection();
//add the Attribute
$products->addAttributeToSelect(array('manufacturer'), 'inner');
//walk the collection
foreach($products as $product){
  
//print the manufacturer
  
echo $product->getManufacturer();
}

But I think, that’s not what you want. This would give you an Collection of all Products and then just outputting the manufacturer for each product.

Well unfortunatly I can’t find that class for visualization in my installation. Also I don’t have any select.phtml in my template folder. So what you could do is have a look in the Block class. You can find mostly a comment in the template files, which class is used. Here it would be:

/* @var $this Mage_Catalog_Block_Product_View_Options_Type_Select */

So have a look in the file app/code/core/Mage/Catalog/Block/Product/Options/Type/Select.php. Ther you will propably find a method named getOption(). Look what that returns and how it consturct that.

Sorry, if I can’t help you more…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Danielc1234
Sr. Member
 
Avatar
Total Posts:  175
Joined:  2008-01-09
Miami, FL - United States
 

I’m going to try your example. But what if I only want to view the value of the ‘manufacturer’ that the product/item the customer is looking at or is on? I really don’t need to see the whole array or load the whole array. Does that make sense?
Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Danielc1234
Sr. Member
 
Avatar
Total Posts:  175
Joined:  2008-01-09
Miami, FL - United States
 

Your suggestion works, but two problems. It is pulling ALL the data and it seems to be pulling an ID instead of the value. So instead of getting the name of the manufacturer, I’m getting a number.

Almost!!!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
cosmicbandito
Jr. Member
 
Total Posts:  16
Joined:  2008-06-04
 

The thing is, that in $this->getOptions() you get something like an Option Array or similar. I don’t know what kind of Options, beacause I don’t have these files in my installation. I also don’t know how that is related to the product attribute you want to display.

Well the behavior is that what I’ve said. Ok,unless that there is just a number for the manfacurer. But this is a way to display the attribute values for some products.

Maybe you have to set something up in the XML Layout file? Did you look there? So go into the catalog.xml and search for catalog/product_view_options_type_select. If you find a Block declaration, maybe inside is something like <action ... maybe ere you can find some speciel declarations to add attributes.

Or look what Mage_Catalog_Block_Product_View_Options_Type_Select::getOptions() makes. Maybe there you get a glue.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top