我正在为我的工作编写自定义组件。我正在使用hello组件来构建它。当我编辑表单并保存时,我收到此错误:
在非对象
上调用成员函数bind()
我的代码:
function save()
{
global $option;
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_abc'.DS.'tables');
$row =& JTable::getInstance('abc', 'Table');
if(!$row->bind(JRequest::get('post')))
{
JError::raiseError(500, $row->getError() );
}
$row->message = JRequest::getVar( 'message', '','post', 'string', JREQUEST_ALLOWRAW );
if(!$row->store()){
JError::raiseError(500, $row->getError() );
}
switch($this->_task)
{
case 'apply':
$msg = 'Change Saved';
$link = 'index.php?option='.$option.'&task=edit&cid[]='.$row->id;
break;
case 'save':
$msg = 'Saved';
$link = 'index.php?option='.$option;
break;
default:
}
$this->setRedirect($link, $msg);
}
问题在于它无法创建实例。
如果有人知道解决方案,请告诉我。
感谢。
答案 0 :(得分:1)
问题是你调用变量$ row上不存在的方法'bind'。 您将$ row定义为:$ row =& JTable :: getInstance('abc','Table');这意味着你的问题就在那里开始。它试图获取失败的数据库内容。我建议您将参数'abc'和'Table'更改为真实的,它看起来像样本数据给我。
答案 1 :(得分:1)
以下代码可以帮助您:
function addComment($option)
{
global $mainframe;
$row =& JTable::getInstance( 'comment' , 'Table' );
if (!$row->bind(JRequest::get( 'post')))
{
JError::raiseError(500, $row->getError() );
}
$row->comment_date = date ( 'Y-m-d H:i:s' );
$user =& JFactory::getUser();
if($user->id)
{
$row->user_id = $user->id;
}
if(!$row->store())
{
JError::raiseError(500, $row->getError() );
}
$link = JRoute::_( 'index.php?option='.$option.'&id='.$row->id . '&task=view' );
$mainframe->redirect( $link, 'Comment Added' );
}
答案 2 :(得分:0)
我要提取的表名数据是jos_abc
。保存功能位于my_componet/controller.php
。控制器的类名是XyzController
:
class XyzController extends JController {
function __construct() {
//Get View
if(JRequest::getCmd('view') == '') {
JRequest::setVar('view', 'default');
}
$this->item_type = 'Default';
parent::__construct();
}
function save()
{
global $option;
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_tripplanner'.DS.'tables');
$row1 =& JTable::getInstance('xyz', 'jos_abc');
if(!$row1->bind(JRequest::get('post')))
{
JError::raiseError(500, $row->getError() );
}
$row->message = JRequest::getVar( 'message', '','post', 'string', JREQUEST_ALLOWRAW );
if(!$row->store()){
JError::raiseError(500, $row->getError() );
}
switch($this->_task)
{
case 'apply':
$msg = 'Change Saved';
$link = 'index.php?option='.$option.'&task=edit&cid[]='.$row->id;
break;
case 'save':
$msg = 'Saved';
$link = 'index.php?option='.$option;
break;
default:
}
$this->setRedirect($link, $msg);
}
}
即使这样我也无法保存,我得到“在非对象上调用成员函数bind()”。