2

私は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');
}

}

4

2 に答える 2

6

Magento makeは、魔法のメソッドを使用して__call()、Magentoオブジェクト内のプライベート(非表示)データのアクセサーメソッドを動的に「作成」します。

MagentoのほとんどのクラスはVarien_Object、マジック__call()メソッドが定義されているから継承します。

PHPのマジック関数について詳しく知りたい場合は、 http__call()://www.php.net/manual/en/language.oop5.overloading.php#object.callで読むことができます。

他の魔法の方法はここで見つけることができます:http ://www.php.net/manual/en/language.oop5.magic.php 。(と同様に__call()、魔法のメソッド__get()__set())。

これがすべてMagentoでどのように機能するかを説明する記事を見つけました:http://codemagento.com/2011/02/where-are-my-getters-and-setters/

で始まるコメント行@methodは、ドキュメントジェネレータ、IDEへのヒントであり、このメソッドはコードで定義されていませんが、マジック__call()メソッドでアクセスできる必要があります。NetbeansやEclipseなどのIDEを使用している場合は、メソッドのコード補完を取得する必要があります。

于 2012-10-03T05:42:49.997 に答える
4

参照してくださいVarien_Object::__call()-Magentoのいわゆるマジックゲッターとセッターの基礎。

于 2012-10-03T04:13:00.247 に答える