mark rammmy
mark rammmy

Reputation: 1498

joomla function bind Problem

I'm writing a custom component for my work. I'm using hello component for building it. When I edit a form and save it I get this error:

Call to a member function bind() on a non-object

My code:

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);
}

The problem is that it's unable to create an instance.

Please let me know if anyone knows a solution.

Thanks.

Upvotes: 0

Views: 1806

Answers (3)

mark rammmy
mark rammmy

Reputation: 1498

My table name data to be fetched is jos_abc. The save function is in my_componet/controller.php. The class name of controller is 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);
    }
}

Even then I'm unable to save, I get "Call to a member function bind() on a non-object".

Upvotes: 0

Urdesh Kumar
Urdesh Kumar

Reputation: 11

The following code will help you:

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' );

}

Upvotes: 1

Mark Bekkers
Mark Bekkers

Reputation: 437

The problem is you calling the method 'bind' that doesn't exists on the variable $row. You defined $row as: $row =& JTable::getInstance('abc', 'Table'); which means your problem starts right there. It's trying to fetch database content which fails. I suggest you change the parameters 'abc' and 'Table' to something real, it looks like sample data to me.

Upvotes: 1

Related Questions