I am learning Magento and I come across this problem.
Template File code
<?php $testimonials = $this->getTestimonials(); ?>
<?php $i = 0;?>
<?php if ($testimonials->count() > 0): ?>
<div class="block testimonials_sidebar">
<div class="block-title">
<strong><span><?php echo $this->__('Testimonials') ?></span></strong>
</div>
<div class="block-content">
<?php foreach ($testimonials as $testimonial): ?>
<div class="testimonial_sidebar_box">
<div class="testimonial_sidebar_text"><?php echo $testimonial->getTestimonialText(); ?></div>
<div class="testimonial_sidebar_name"><?php echo $testimonial->getTestimonialName(); ?></div>
</div>
<?php endforeach; ?>
<div class="actions">
<a href="<?php echo $this->getUrl('testimonials'); ?>"><?php echo $this->__('View All Testimonials'); ?></a>
</div>
</div>
</div>
<?php endif;?>
When I go to block to see the First line of code in the Template file which is
<?php $testimonials = $this->getTestimonials(); ?>
And I could not find this method declared in that block class, instead I can see this method but in commented section. But This method hasn’t been declared anywhere in the module. How is this happening? Block class code below.
/**
* Frontend block for testimonials
*
* @method Turnkeye_Testimonial_Model_Mysql4_Testimonial_Collection getTestimonials()
*/
class Turnkeye_Testimonial_Block_Testimonial extends Mage_Core_Block_Template
{
/**
* Before rendering html, but after trying to load cache
*
* @return Turnkeye_Testimonial_Block_Testimonial
*/
protected function _beforeToHtml()
{
$this->_prepareCollection();
return parent::_beforeToHtml();
}
/**
* Prepare testimonial collection object
*
* @return Turnkeye_Testimonial_Block_Testimonial
*/
protected function _prepareCollection()
{
/* @var $collection Turnkeye_Testimonial_Model_Mysql4_Testimonial_Collection */
$collection = Mage::getModel("turnkeye_testimonial/testimonial")->getCollection();
if ($this->getSidebar()){
$collection->addFieldToFilter('testimonial_sidebar', '1');
}
$collection->setOrder('testimonial_position', 'ASC')
->load();
$this->setTestimonials($collection);
return $this;
}
}
If I am ctrl click on that method in template file it’s taking me to that method in comments. I can see it’s pointing towards collection so here is my collection code.
class Turnkeye_Testimonial_Model_Mysql4_Testimonial_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
/**
* Initialization here
*
*/
public function _construct()
{
parent::_construct();
$this->_init('turnkeye_testimonial/testimonial');
}
}
Magento makes uses the magic
__call()method to dynamically “create” accessor methods for private (hidden) data in Magento objects.Most classes in Magento inherit from
Varien_Object, where the magic__call()method is defined.If you want to know more about the magic
__call()function in PHP, you can read about it here: http://www.php.net/manual/en/language.oop5.overloading.php#object.call.Other magic methods can be found here: http://www.php.net/manual/en/language.oop5.magic.php. (Similar to
__call()are the magic methods__get()and__set()).I found an article that gives an explanation of how this all works in Magento: http://codemagento.com/2011/02/where-are-my-getters-and-setters/.
The comment line that you saw that starts with
@methodis a hint to document generators, IDEs, and to you, that while this method isn’t defined in the code, it should be accessible by the magic__call()method. If you’re using an IDE like Netbeans or Eclipse, you should get code completion for the method.