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

Pull first_post_likes based on known thread_id

Discussion in 'XenForo Development Discussions' started by LPH, Apr 27, 2015.

  1. LPH

    LPH Well-Known Member

    The thread_id is known and I'd like to pull the first_post_likes based on that thread_id.

    This code does the above through an SQL statement. Since I'm trying to learn the "XenForo way" ... the next step is to take this code and change it to using XenForo_Model_Thread and getThreads or should it be getThreadsID?

    Here is working code:

    PHP:
    <?php

    /**
    * Filename: class-xenword-postratings.php
    *
    * Description: Shows the post ratings of the first post in a thread on the WordPress post.
    *
    * @since 2.5.1.01
    *        Initial release
    *
    */

    /**
    * This section is for security. Do not modify this part:
    * @ignore
    */

    if ( ! defined'ABSPATH' ) ) exit;

    /**
    * Pull the likes from the first_post likes in the xf_thread table based on the thread_id
    * then return the number of likes to the WordPress $content
    *
    * @param $content
    *
    * @return string
    * @throws Zend_Exception
    */
    function show_postratings$content ) {

        
    /** Load the XenWord options set in XenWord settings panel */
        
    $xenword_options get_option'xenword_options' );

        if ( 
    $xenword_options['use_postratings'] == true ) {

            if ( 
    is_singular() && is_main_query() ) {

                
    $post_id '';

                
    $post get_post$post_idARRAY_A );
                if ( 
    $post['thread_id'] != '0' ) {

                    
    // Show Post Ratings

                    
    $thread_id $post['thread_id'];

                    
    $first_post_id_qry "

                        SELECT `thread_id`, `first_post_likes` FROM `xf_thread`
                        WHERE `thread_id` = 
    $thread_id

                    "
    ;

                    
    $xen_post_id XenForo_Application::get'db' )->fetchAll$first_post_id_qry );

                    
    $xen_first_post_likes$xen_post_id[0]['first_post_likes'];

                    
    $xenword_postratings '
                        <span style="font-size: x-small; float: right">Post Ratings:<br />'
                        
    $xen_first_post_likes
                        
    ' likes</span>
                        '
    ;

                    
    $content .= $xenword_postratings;

                }
            }
        }

        
    /** return the WordPress post content with the post ratings included */

        
    return $content;

    }

    add_filter'the_content''show_postratings' );

    // End class-xenword-postratings.php
    The place to focus:

    PHP:
                    $thread_id $post['thread_id'];

                    
    $first_post_id_qry "
                        SELECT `thread_id`, `first_post_likes` FROM `xf_thread`
                        WHERE `thread_id` = 
    $thread_id
                    "
    ;

                    
    $xen_post_id XenForo_Application::get'db' )->fetchAll$first_post_id_qry );
    I'm trying to learn this one from scratch rather than copy pasting from previous pulling of thread content.

    Somehow I want to grab XenForo_Model_Thread::FETCH_FIRSTPOST, correct?
     
  2. Robust

    Robust Well-Known Member

    Well, I'm kinda a novice so my advice might be wrong but as far as the query itself goes you should use fetchOne right?

    I mean, you could create the model (XenForo_Model::create) and use a function from the model and I guess that'd be the right way to do it, I can't take a look at which function you'd need though, but you could just make your own model function if none exactly match what you're going for.
     
  3. LPH

    LPH Well-Known Member

    Yes, the model would be created first but I'm trying to figure out whether to use XenForo_Model_Thread::FETCH_FIRSTPOST or if the first_post_likes can be pulled a different way. I'm very weak on XF models and usually copy from previous work. This time I'm trying to reason things out.
     
  4. Daniel Hood

    Daniel Hood Well-Known Member

    This should work for you:

    PHP:
    $threadModel XenForo_Model::create('XenForo_Model_Thread');
    $thread $threadModel->getThreadById($threadId);
    Zend_Debug::dump($thread['first_post_likes']);
     
    LPH and Robust like this.
  5. LPH

    LPH Well-Known Member

    @Daniel Hood - thank you.

    Interesting that PhpStorm doesn't show getThreadById in the dropdown for that model. "Method getThreadById not found in XenForo_Model" -- which is true because it should pull from XenForo_Model_Thread. A setting must not be right :(

    Oh - and good one on Zend_Debug since I'm used to var_dump !
     
  6. Daniel Hood

    Daniel Hood Well-Known Member

    In order for it to do that, you have to do:

    PHP:
    /* @var $threadModel XenForo_Model_Thread */
                
    $threadModel XenForo_Model::create('XenForo_Model_Thread');
    phpstorm can't just 'know' which model you're loading like that, you have to give it a hint.
     
  7. LPH

    LPH Well-Known Member

    @Daniel Hood

    OMG. A simple comment allows all the methods to be shown !!

    I had no clue ... this is going to change so much of my code. Wow. :cool:

    Thank you so much. This simple thing opens a door to a whole new way of coding. (y)
     

    Attached Files:

    Daniel Hood likes this.

Share This Page