我正在学习 Magento,我遇到了这个问题。
模板文件代码
<?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;?>
当我去阻止查看模板文件中的第一行代码时
<?php $testimonials = $this->getTestimonials(); ?>
而且我找不到在该块类中声明的此方法,而是可以在注释部分看到此方法。但是此方法尚未在模块中的任何地方声明。这是怎么回事?下面的块类代码。
/**
* 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;
}
}
如果我是 ctrl 单击模板文件中的该方法,它会将我带到评论中的该方法。我可以看到它指向收藏,所以这是我的收藏代码。
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');
}
}