可恢复错误:Method::__toString()必须返回字符串值



当我加载产品页面时,系统日志文件中显示如下错误..

2013-03-12T10:28:56+00:00 ERR (3): Recoverable Error: Method SM_Vendors_Model_Mysql4_Vendor_Collection::__toString() must return a string value  in C:xampphtdocsmagentoappcodelocalSMVendorsBlockAdminhtmlCatalogProductRenderVendor.php on line 21

我的代码是…

应用SM 本地代码 供应商块产品 Adminhtml 目录 渲染 vendor.php

class SM_Vendors_Block_Adminhtml_Catalog_Product_Render_Vendor extends Varien_Data_Form_Element_Abstract
{
 public function getElementHtml()
 {
 $vendorCollection = $this->getVendorCollection(); //line#21
 }
 public function getVendorCollection()
 {
  $collection = Mage::getResourceModel('smvendors/vendor_collection')->__toString();
  return $collection;
 }
}

应用程序代码 本地SM 供应商模型 Mysql4 供应商 Collection.php

class SM_Vendors_Model_Mysql4_Vendor_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract 
{
 public function _construct() 
 {
  parent::_construct();
  $this->_init('smvendors/vendor');
 }
 public function __toString()
 {
    return $this->_init('smvendors/vendor');
 }
}

我想解决这种类型的错误显示在系统日志文件在magento。

确保$this->_init('smvendor/vendor');返回一个字符串。或者返回__toString at:

中的字符串
public function __toString(){
    return $this->_init('smvendors/vendor');
}

似乎你在代码中使用了不正确的__toString()方法。
似乎这里这个方法是用来获取集合,而不是字符串。

public function getVendorCollection()
 {
  $collection = Mage::getResourceModel('smvendors/vendor_collection')->__toString();
  return $collection;
 }

这是不正确的,也是一个非常糟糕的主意。
您需要重构您的代码,否则您将永远无法摆脱此警告。

怎么做

例如,你可以这样做…将__toString法改为getCollection法,将_toString法改为getCollection法。
替换直到日志中的消息不会消失。

阅读更多关于你的问题:http://www.php.net/manual/en/language.oop5.magic.php#object.tostring

所以这里是固定的代码:

class SM_Vendors_Model_Mysql4_Vendor_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract 
{
 public function _construct() 
 {
  parent::_construct();
  $this->_init('smvendors/vendor');
 }
 public function getCollection()
 {
    return $this->_init('smvendors/vendor');
 }
 public function __toString()
 {
    return $this->_init('smvendors/vendor');
 }
}

第二类……

class SM_Vendors_Block_Adminhtml_Catalog_Product_Render_Vendor extends Varien_Data_Form_Element_Abstract
{
 public function getElementHtml()
 {
 $vendorCollection = $this->getVendorCollection(); //line#21
 }
 public function getVendorCollection()
 {
  $collection = Mage::getResourceModel('smvendors/vendor_collection')->getCollection();
  return $collection;
 }
}

最新更新