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

getChild()->setData() variable not goind trough! 
 
laccas
Member
 
Total Posts:  39
Joined:  2010-08-08
 

Hi there!

I\’m on to create an advanced review module. But I\’m facing with a getChild()->setData() parent to child variable passing issue.

I\’m using this code to pass a variable to another block:

$this->getChild(\'ratereview\')->setData(\'review\',1); ?>
echo $this->getChildHtml(\'ratereview\',false);

this is the block code:

<block type=\"review/product_view_list\" name=\"product.info.product_additional_data\" as=\"product_additional_data\" template=\"review/product/view/list.phtml\">
 <
block type=\"tlreview/ratereview\" name=\"ratereview\" as=\"ratereview\"/ >
</
block>

and the child block:

class TLSoft_RateReview_Block_Ratereview extends Mage_Core_Block_Template
{
    
protected $_review;
    public function 
__construct()
    
{
        parent
::__construct();
        
$this->_review=$this->getData($review);
        
$this->setTemplate(\'tlsoft/review/product/view/list/ratereview.phtml\');
    
}

    
public function getReview()
    
{
        
return $this->_review;
    
}
}

And when I\’m wanted to use this variable in the related .phtml file, it\’s not getting trough!

$_review=$this->getReview();
print_r($_review);

My only idea is because setTemplate calling in constructor, but it\’s just a hard guessing.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello
Here is your problem.
You call this

$this->_review=$this->getData($review);
in the constructor.
First o all the variable $review is not defined.
I assume you wanted to say
$this->_review=$this->getData('review');
but even if you do this it won’t work because when calling the constructor _data[’review’] is not set.

When you call

$this->getChild('ratereview')->setData(...)
your constructor has already been called.

Here is what you can do.
remove this line from the constructor because it’s useless:

$this->_review=$this->getData($review);

You can either remove the method getReview() from your class. If you do this, then when you call getReview it will call automatically getData(’review’).

or add the following method to your class:

public function setReview($review)
    
{
        $this
->_review $review;
        return 
$this;
    
}

Let me know how it turns out.
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
kiatng
Enthusiast
 
Total Posts:  875
Joined:  2008-09-03
Kuala Lumpur, Malaysia
 

Marius is right, you can’t access the object property in the constructor since the setter is yet to be executed. In your case, it is not necessary to set the local var $_review. You can simply do a getter in the phtml, since by then the setter would have been executed:

<?php echo $this->getReview() ?>

There is no need for the local var $_review and to setData in the block.

 
Magento Community Magento Community
Magento Community
Magento Community
 
laccas
Member
 
Total Posts:  39
Joined:  2010-08-08
 

Thank you for your answers! I’ve figured it out the problem before I get answers, but now I know why it didn’t work.

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