1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'XenForo Development Discussions' started by AndyB, Dec 15, 2015.

  1. AndyB

    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:
    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.
     
  2. Jake B.

    Jake B. Well-Known Member

    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.
     
  3. AndyB

    AndyB Well-Known Member

    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:
     
  4. AndyB

    AndyB Well-Known Member

    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.
     
  5. Robust

    Robust Well-Known Member

    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.
     
  6. Daniel Hood

    Daniel Hood Well-Known Member

    Could also really just cast the variable to string and it'd handle all that for you.

    PHP:
    $string = (string) new XenForo_Phrase('asd');
     
    Robust and HWS like this.
  7. AndyB

    AndyB Well-Known Member

    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 '';
            }
     
  8. Daniel Hood

    Daniel Hood Well-Known Member

    Better suggestion, which has been made numerous times by numerous people including myself, use DataWriters and Models.
     
    Martok and HWS like this.

Share This Page