0

製品ページをロードすると、このようなシステムログファイルにエラーが表示されます。

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:\xampp\htdocs\magento\app\code\local\SM\Vendors\Block\Adminhtml\Catalog\Product\Render\Vendor.php on line 21

私のコードは次のようなものです。

app \ code \ local \ SM \ Vendors \ Block \ Adminhtml \ Catalog \ Product \ Render \ 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;
 }
}

app \ code \ local \ SM \ Vendors \ Model \ Mysql4 \ Vendor \ 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のシステムログファイルに表示されるこの種のエラーを解決したいのですが、ご存知の場合はご返信ください。

4

2 に答える 2

0

$this->_init('smvendors/vendor'); であることを確認してください。文字列を返します。または、次の __toString の文字列である何かを返します。

public function __toString(){
    return $this->_init('smvendors/vendor');
}
于 2013-03-12T11:30:45.357 に答える
0

__toString()コード内のメソッドの使い方が間違っているようです。
ここでは、このメソッドは文字列ではなくコレクションを取得するために使用されているようです。

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

それは正しくなく、非常に悪い考えです。
コードをリファクタリングする必要があります。そうしないと、この警告が消えることはありません。

何をすべきか

たとえば、次のように実行できます。__toStringメソッドの名前を に変更し、が使用されgetCollectionた場所を に置き換えます。 ログのメッセージが消えなくなるまで交換してください。 _toStringgetCollection

問題の詳細については、こちらをご覧ください: 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;
 }
}
于 2013-03-12T11:31:12.080 に答える