在非对象上调用成员函数select()

时间:2015-06-30 10:17:26

标签: joomla joomla2.5 joomla1.5

我正在尝试使用ajax检索一些信息。但是,当我调用select()方法时,系统会返回致命错误。这是什么原因?

这是我的代码:

     define( '_JEXEC', 1 );
     define( 'DS', DIRECTORY_SEPARATOR );
     define( 'JPATH_BASE', realpath(dirname(__FILE__).DS.'..' ));


     require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
     require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $query->select($db->quoteName(array('id', 'product_id')));
     $query->from($db->quoteName('sample_table'));
      // ->where('application_id = 11');


     $db->setQuery($query);
     $results = $db->loadObjectList();

我已经在其他版本上多次使用过这段代码而且它有效。我认为它可能与版本有关..

提前感谢。

2 个答案:

答案 0 :(得分:0)

根据您的代码,我认为您正在尝试在外部函数中使用Joomla函数。该错误表示您正在使用工厂对象而不包括工厂类。尝试包含所有这些文件:

 define('_JEXEC', 1);
 define('DS', DIRECTORY_SEPARATOR);
 define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . DS . '');
 require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
 require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
 require_once (JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');

 $db = JFactory::getDbo();
 $query = $db->getQuery(true);
 $query->select($db->quoteName(array('id', 'product_id')));
 $query->from($db->quoteName('sample_table'));
  // ->where('application_id = 11');
 $db->setQuery($query);
 $results = $db->loadObjectList();

答案 1 :(得分:0)

那是你的所有代码吗?它看起来像你没有创建数据库连接。你需要像

这样的东西
    jimport('joomla.database.database');
    // System configuration.
    $config = JFactory::getConfig();
    // Note, this will throw an exception if there is an error
    // Creating the database connection.
    $this->dbo = JDatabase::getInstance(
        array(
            'driver' => $config->get('dbtype'),
            'host' => $config->get('host'),
            'user' => $config->get('user'),
            'password' => $config->get('password'),
            'database' => $config->get('db'),
            'prefix' => $config->get('dbprefix'),
        )
    );

总体而言,独立应用程序首次使用时需要几分钟,但我认为您最好使用API​​并将其置于构造函数中。以下是https://github.com/elinw/AssetFix/blob/j3/assetfix.php#L65

的示例