Fatal error: Call to a member function result_metadata() on a non-object

AndyB

Well-known member
Hello,

In my add-on called Search Log v2.5 located here:

https://xenforo.com/community/resources/search-log.2539/

In a table called xf_search_log, I try to update a table field called title_constraints. The value of title_constraints is added by the following code:

PHP:
$titleConstraints = new XenForo_Phrase('searchlog_title_only');

then I use an insert like this:

PHP:
        // run query
        $db->query("
            INSERT INTO xf_search_log
                (search_date, user_id, username, search_query, forum_constraints, user_constraints, title_constraints, newer_than, result_count)
            VALUES
                (?, ?, ?, ?, ?, ?, ?, ?, ?)
        ", array($searchDate, $userId, $username, $searchQuery, $forumConstraints, $userConstraints, $titleConstraints, $newerThan, $resultCount));

I get the following error message when a search is made and the add-on code is executed:
Fatal error: Call to a member function result_metadata() on a non-object in /home/southbay/www/forums/library/Zend/Db/Statement/Mysqli.php on line 220

If I don't use a phrase and just assign the $titleConstraints variable a value there is no error.

What do I need to do to use a phrases in this situation?

Thank you.
 
Hello,

In my add-on called Search Log v2.5 located here:

https://xenforo.com/community/resources/search-log.2539/

In a table called xf_search_log, I try to update a table field called title_constraints. The value of title_constraints is added by the following code:

PHP:
$titleConstraints = new XenForo_Phrase('searchlog_title_only');

then I use an insert like this:

PHP:
        // run query
        $db->query("
            INSERT INTO xf_search_log
                (search_date, user_id, username, search_query, forum_constraints, user_constraints, title_constraints, newer_than, result_count)
            VALUES
                (?, ?, ?, ?, ?, ?, ?, ?, ?)
        ", array($searchDate, $userId, $username, $searchQuery, $forumConstraints, $userConstraints, $titleConstraints, $newerThan, $resultCount));

I get the following error message when a search is made and the add-on code is executed:


If I don't use a phrase and just assign the $titleConstraints variable a value there is no error.

What do I need to do to use a phrases in this situation?

Thank you.

A phrase isn't a string, I believe DataWriters take care of this for you, but if you really don't want to use a DataWriter you can replace your query with this:

Code:
        // run query
        $db->query("
            INSERT INTO xf_search_log
                (search_date, user_id, username, search_query, forum_constraints, user_constraints, title_constraints, newer_than, result_count)
            VALUES
                (?, ?, ?, ?, ?, ?, ?, ?, ?)
        ", array($searchDate, $userId, $username, $searchQuery, $forumConstraints, $userConstraints, $titleConstraints->render(), $newerThan, $resultCount));

Really though, inserting phrase values into the database isn't the best idea. May be better to insert the phrase key so translation will still work after the data is inserted. Otherwise it'll be a mess.
 
Hi Jake,

Thank you for your help.

If I change the variable in the Insert code to:

$titleConstraints->render()

if $titleConstraints has no value I get the following error:
Fatal error: Call to a member function render() on a non-object in /home/southbay/www/forums/library/Andy/SearchLog/ControllerPublic/Search.php on line 267
 
Got it. I just need to render the variable like this:

PHP:
        //########################################
        // title constraints
       
        if ($input['title_only'] == 1)
        {
            $titleConstraints = new XenForo_Phrase('searchlog_title_only');
            $titleConstraints = $titleConstraints->render();
        }
        else
        {
            $titleConstraints = '';
        }

The query part of the code remains as I have it in post #1.
 
Should add the phrase key if anything, and fetch the phrase.

Edit: nvm, you're inserting to default XF table and prob need the value of it.
 
Thank you, Daniel. Your suggestion works perfect:

PHP:
		//########################################
		// title constraints
		
		if ($input['title_only'] == 1)
		{
			$titleConstraints = (string) new XenForo_Phrase('searchlog_title_only');
		}
		else
		{
			$titleConstraints = '';
		}
 
Top Bottom