Reputation: 1400
I have the following code:
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'status'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($catid));
$_helper = $this->helper('catalog/output');
foreach ($_productCollection as $_product):
echo $_product->getProductUrl();
endforeach
I want to learn how I discover the methods I can use on an object.
For example $_product->getProductUrl() is using the method getProductUrl() to get the url, but I need price and have no idea what method calls that. Using a print_r doesn't provide enough info for me to discover what they are. I presume they are in controllers that are located in the MAGE core. I have commerce bug and I have tired looking at: http://docs.magentocommerce.com/ But I find myself lost at times.
Does anyone know a good tutorial on this or can give me direction to figuring this out?
Upvotes: 8
Views: 12516
Reputation: 26016
Here is a simple direction to find the class files:-
1. Collection
$_productCollection = Mage::getResourceModel('catalog/product_collection')
The above code means that we are calling the product collection class. The class file can be found in:-
app\code\core\Mage\Catalog\Model\Resource\Eav\Mysql4\Product\Collection.php
In Collection.php you will find the following class:-
class Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
extends Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
So, your required function can be in Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
class.
If it is not there then it can be in the parent class, i.e. Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
2. Model
Similarly,
$_product = Mage::getModel('catalog/product')
For the above code, the class file is:-
app\code\core\Mage\Catalog\Model\Product.php
3. Helper
For helper file,
$_helper = $this->helper('catalog/output');
The above helper file is:-
app\code\core\Mage\Catalog\Helper\Output.php
4. Finally
Finally, using an IDE like Eclipse or Netbeans will help you learn and write code quickly and efficiently.
Upvotes: 4
Reputation: 653
I'm a total novice, but I used the info on this page to get all the methods available to me for a certain object and send them to a log.
For example the category_collection:
$collection = Mage::getResourceModel('catalog/category_collection');
Mage::log(
"Methods for class ".get_class($collection)." ".print_r(get_class_methods($menu),true),
null,
'log_name.log'
);
$parent = get_parent_class($collection);
while ($parent) {
Mage::log(
"Methods for parent ".$parent." ".print_r(get_class_methods($parent),true),
null,
'log_name.log'
);
$parent = get_parent_class($parent);
}
Upvotes: 0
Reputation: 166166
First, in models and blocks, any method with get
or a set
may actually be a magic method that's pulling from the object's _data
array. You can see all the data in an object like this
var_dump($object->getData());
So if this array had a key named some_data
, you could call a method named getSomeData
echo $object->getSomeData();
Remember though, some methods will have actual methods that start with get
and set
, so always check the class definition.
Secondly, you can use PHP reflection functions (or the more complete but complicated PHP Reflection Class API) to see what class an object is, and then get a list of methods on that class
First, use get_class to get the name of an object's class.
$class_name = get_class($object);
Then, pass that get_class_methods to get a list of all the callable methods on an object
$class_name = get_class($object);
$methods = get_class_methods($class_name);
echo "Methods for class $class_name \n<br />\n";
foreach($methods as $method)
{
var_dump($method);
}
This will give you a list of all the class methods. You can then use the Class/URI Lookup tab of Commercebug bug to quickly zero in on which file a class is defined in to look at the method definitions. Remember, some methods will be defined in ancestor classes. Investing the time to learn an IDE or a program like ctags is well worth the investment, ad they'll let you quickly jump to individual class definitions.
Upvotes: 22