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

Problem With getThreads Not Sorting Right

Discussion in 'XenForo Development Discussions' started by BamaStangGuy, Jan 27, 2015.

  1. BamaStangGuy

    BamaStangGuy Well-Known Member

    I am obviously overlooking something here?

    Code:

    PHP:
            $threadModel $this->_getThreadModel();
           
            
    $threadConditions = array(
               
                
    'discussion_state' => 'visible',
                
    'discussion_open' => '1'
               
            
    );
           
            
    $threadFetchOptions = array(
                
    'order' => 'view_count',
                
    'orderDirection' => 'DESC',
                
    'limit' => '10'
            
    );
           
            
    $threads $threadModel->getThreads($threadConditions$threadFetchOptions);
            echo 
    '<pre>';
            
    var_dump($threads);
            echo 
    '</pre>';
    This should return the top 10 threads with the most views, sorted by view_count highest to lowest right?

    Right now it is not seeming to recognize the orderDirection at all.

    Sample result:

    Code:
    array(10) {
      [4894]=>
      array(22) {
        ["thread_id"]=>
        int(4894)
        ["node_id"]=>
        int(17)
        ["title"]=>
        string(25) "Reported Post by wyverary"
        ["reply_count"]=>
        int(0)
        ["view_count"]=>
        int(0)
        ["user_id"]=>
        int(1090)
        ["username"]=>
        string(8) "wyverary"
        ["post_date"]=>
        int(1385657098)
        ["sticky"]=>
        int(0)
        ["discussion_state"]=>
        string(7) "visible"
        ["discussion_open"]=>
        int(1)
        ["discussion_type"]=>
        string(0) ""
        ["first_post_id"]=>
        int(72257)
        ["first_post_likes"]=>
        int(0)
        ["last_post_date"]=>
        int(1385657098)
        ["last_post_id"]=>
        int(72257)
        ["last_post_user_id"]=>
        int(1090)
        ["last_post_username"]=>
        string(8) "wyverary"
        ["prefix_id"]=>
        int(0)
        ["cta_ft_featured"]=>
        int(0)
        ["block_adsense"]=>
        int(0)
        ["similar_threads"]=>
        string(24) "4994,4895,5177,5175,5174"
      }
      [10573]=>
      array(22) {
        ["thread_id"]=>
        int(10573)
        ["node_id"]=>
        int(50)
        ["title"]=>
        string(51) "Boy development issues with my son, advice please.."
        ["reply_count"]=>
        int(0)
        ["view_count"]=>
        int(0)
        ["user_id"]=>
        int(868)
        ["username"]=>
        string(10) "ladydragon"
        ["post_date"]=>
        int(1422330513)
        ["sticky"]=>
        int(0)
        ["discussion_state"]=>
        string(7) "visible"
        ["discussion_open"]=>
        int(1)
        ["discussion_type"]=>
        string(0) ""
        ["first_post_id"]=>
        int(187966)
        ["first_post_likes"]=>
        int(0)
        ["last_post_date"]=>
        int(1422330513)
        ["last_post_id"]=>
        int(187966)
        ["last_post_user_id"]=>
        int(868)
        ["last_post_username"]=>
        string(10) "ladydragon"
        ["prefix_id"]=>
        int(0)
        ["cta_ft_featured"]=>
        int(0)
        ["block_adsense"]=>
        int(0)
        ["similar_threads"]=>
        string(24) "9179,3341,7549,5767,1504"
      }
      [10574]=>
      array(22) {
        ["thread_id"]=>
        int(10574)
        ["node_id"]=>
        int(9)
        ["title"]=>
        string(5) "Hello"
        ["reply_count"]=>
        int(0)
        ["view_count"]=>
        int(0)
        ["user_id"]=>
        int(12605)
        ["username"]=>
        string(23) "RootingForUnderstanding"
        ["post_date"]=>
        int(1422330792)
        ["sticky"]=>
        int(0)
        ["discussion_state"]=>
        string(7) "visible"
        ["discussion_open"]=>
        int(1)
        ["discussion_type"]=>
        string(0) ""
        ["first_post_id"]=>
        int(187968)
        ["first_post_likes"]=>
        int(0)
        ["last_post_date"]=>
        int(1422330792)
        ["last_post_id"]=>
        int(187968)
        ["last_post_user_id"]=>
        int(12605)
        ["last_post_username"]=>
        string(23) "RootingForUnderstanding"
        ["prefix_id"]=>
        int(0)
        ["cta_ft_featured"]=>
        int(0)
        ["block_adsense"]=>
        int(0)
        ["similar_threads"]=>
        string(29) "10555,10513,10488,10205,10203"
      }
      [4895]=>
      array(22) {
        ["thread_id"]=>
        int(4895)
        ["node_id"]=>
        int(17)
        ["title"]=>
        string(25) "Reported Post by wyverary"
        ["reply_count"]=>
        int(0)
        ["view_count"]=>
        int(1)
        ["user_id"]=>
        int(1090)
        ["username"]=>
        string(8) "wyverary"
        ["post_date"]=>
        int(1385657194)
        ["sticky"]=>
        int(0)
        ["discussion_state"]=>
        string(7) "visible"
        ["discussion_open"]=>
        int(1)
        ["discussion_type"]=>
        string(0) ""
        ["first_post_id"]=>
        int(72259)
        ["first_post_likes"]=>
        int(0)
        ["last_post_date"]=>
        int(1385657194)
        ["last_post_id"]=>
        int(72259)
        ["last_post_user_id"]=>
        int(1090)
        ["last_post_username"]=>
        string(8) "wyverary"
        ["prefix_id"]=>
        int(0)
        ["cta_ft_featured"]=>
        int(0)
        ["block_adsense"]=>
        int(0)
        ["similar_threads"]=>
        string(24) "4994,4894,5177,5175,5174"
      }
      [10571]=>
      array(22) {
        ["thread_id"]=>
        int(10571)
        ["node_id"]=>
        int(34)
        ["title"]=>
        string(18) "Aspie reproduction"
        ["reply_count"]=>
        int(0)
        ["view_count"]=>
        int(2)
        ["user_id"]=>
        int(12454)
        ["username"]=>
        string(13) "dakota dorner"
        ["post_date"]=>
        int(1422324262)
        ["sticky"]=>
        int(0)
        ["discussion_state"]=>
        string(7) "visible"
        ["discussion_open"]=>
        int(1)
        ["discussion_type"]=>
        string(0) ""
        ["first_post_id"]=>
        int(187957)
        ["first_post_likes"]=>
        int(0)
        ["last_post_date"]=>
        int(1422324262)
        ["last_post_id"]=>
        int(187957)
        ["last_post_user_id"]=>
        int(12454)
        ["last_post_username"]=>
        string(13) "dakota dorner"
        ["prefix_id"]=>
        int(0)
        ["cta_ft_featured"]=>
        int(0)
        ["block_adsense"]=>
        int(0)
        ["similar_threads"]=>
        string(24) "1706,7227,2461,1984,9717"
      }
      [10572]=>
      array(22) {
        ["thread_id"]=>
        int(10572)
        ["node_id"]=>
        int(34)
        ["title"]=>
        string(18) "Creation theories?"
        ["reply_count"]=>
        int(0)
        ["view_count"]=>
        int(2)
        ["user_id"]=>
        int(12454)
        ["username"]=>
        string(13) "dakota dorner"
        ["post_date"]=>
        int(1422324485)
        ["sticky"]=>
        int(0)
        ["discussion_state"]=>
        string(7) "visible"
        ["discussion_open"]=>
        int(1)
        ["discussion_type"]=>
        string(0) ""
        ["first_post_id"]=>
        int(187959)
        ["first_post_likes"]=>
        int(0)
        ["last_post_date"]=>
        int(1422324485)
        ["last_post_id"]=>
        int(187959)
        ["last_post_user_id"]=>
        int(12454)
        ["last_post_username"]=>
        string(13) "dakota dorner"
        ["prefix_id"]=>
        int(0)
        ["cta_ft_featured"]=>
        int(0)
        ["block_adsense"]=>
        int(0)
        ["similar_threads"]=>
        string(9) "4260,7732"
      }
      [4994]=>
      array(22) {
        ["thread_id"]=>
        int(4994)
        ["node_id"]=>
        int(17)
        ["title"]=>
        string(25) "Reported Post by wyverary"
        ["reply_count"]=>
        int(0)
        ["view_count"]=>
        int(2)
        ["user_id"]=>
        int(1090)
        ["username"]=>
        string(8) "wyverary"
        ["post_date"]=>
        int(1387298127)
        ["sticky"]=>
        int(0)
        ["discussion_state"]=>
        string(7) "visible"
        ["discussion_open"]=>
        int(1)
        ["discussion_type"]=>
        string(0) ""
        ["first_post_id"]=>
        int(74350)
        ["first_post_likes"]=>
        int(0)
        ["last_post_date"]=>
        int(1387298127)
        ["last_post_id"]=>
        int(74350)
        ["last_post_user_id"]=>
        int(1090)
        ["last_post_username"]=>
        string(8) "wyverary"
        ["prefix_id"]=>
        int(0)
        ["cta_ft_featured"]=>
        int(0)
        ["block_adsense"]=>
        int(0)
        ["similar_threads"]=>
        string(24) "4895,4894,5177,5175,5174"
      }
      [4995]=>
      array(22) {
        ["thread_id"]=>
        int(4995)
        ["node_id"]=>
        int(17)
        ["title"]=>
        string(23) "Reported Post by Franco"
        ["reply_count"]=>
        int(0)
        ["view_count"]=>
        int(2)
        ["user_id"]=>
        int(2944)
        ["username"]=>
        string(6) "Franco"
        ["post_date"]=>
        int(1387305419)
        ["sticky"]=>
        int(0)
        ["discussion_state"]=>
        string(7) "visible"
        ["discussion_open"]=>
        int(1)
        ["discussion_type"]=>
        string(0) ""
        ["first_post_id"]=>
        int(74369)
        ["first_post_likes"]=>
        int(0)
        ["last_post_date"]=>
        int(1387305419)
        ["last_post_id"]=>
        int(74369)
        ["last_post_user_id"]=>
        int(2944)
        ["last_post_username"]=>
        string(6) "Franco"
        ["prefix_id"]=>
        int(0)
        ["cta_ft_featured"]=>
        int(0)
        ["block_adsense"]=>
        int(0)
        ["similar_threads"]=>
        string(24) "4996,5177,5175,5174,5160"
      }
      [4886]=>
      array(22) {
        ["thread_id"]=>
        int(4886)
        ["node_id"]=>
        int(17)
        ["title"]=>
        string(22) "Reported Post by Ereth"
        ["reply_count"]=>
        int(0)
        ["view_count"]=>
        int(3)
        ["user_id"]=>
        int(2762)
        ["username"]=>
        string(5) "Ereth"
        ["post_date"]=>
        int(1385471318)
        ["sticky"]=>
        int(0)
        ["discussion_state"]=>
        string(7) "visible"
        ["discussion_open"]=>
        int(1)
        ["discussion_type"]=>
        string(0) ""
        ["first_post_id"]=>
        int(72014)
        ["first_post_likes"]=>
        int(0)
        ["last_post_date"]=>
        int(1385471318)
        ["last_post_id"]=>
        int(72014)
        ["last_post_user_id"]=>
        int(2762)
        ["last_post_username"]=>
        string(5) "Ereth"
        ["prefix_id"]=>
        int(0)
        ["cta_ft_featured"]=>
        int(0)
        ["block_adsense"]=>
        int(0)
        ["similar_threads"]=>
        string(24) "5160,5134,5073,5057,4989"
      }
      [4125]=>
      array(22) {
        ["thread_id"]=>
        int(4125)
        ["node_id"]=>
        int(34)
        ["title"]=>
        string(35) "Faux Pas followed by a good outcome"
        ["reply_count"]=>
        int(0)
        ["view_count"]=>
        int(3)
        ["user_id"]=>
        int(2094)
        ["username"]=>
        string(10) "Ste11aeres"
        ["post_date"]=>
        int(1372378966)
        ["sticky"]=>
        int(0)
        ["discussion_state"]=>
        string(7) "visible"
        ["discussion_open"]=>
        int(1)
        ["discussion_type"]=>
        string(0) ""
        ["first_post_id"]=>
        int(57316)
        ["first_post_likes"]=>
        int(0)
        ["last_post_date"]=>
        int(1372378966)
        ["last_post_id"]=>
        int(57316)
        ["last_post_user_id"]=>
        int(2094)
        ["last_post_username"]=>
        string(10) "Ste11aeres"
        ["prefix_id"]=>
        int(0)
        ["cta_ft_featured"]=>
        int(0)
        ["block_adsense"]=>
        int(0)
        ["similar_threads"]=>
        string(23) "8462,9051,742,3854,8833"
      }
    }
     
  2. BamaStangGuy

    BamaStangGuy Well-Known Member

    Interesting, removing the orderDirection completely results in the correct results. I guess it should only be used if sorting ASC?
     
  3. Bob

    Bob Well-Known Member

    'orderDirection' => 'desc' <--- needs to be in lower case (or simply don't set it).

    PHP:
                if (!isset($fetchOptions['orderDirection']) || $fetchOptions['orderDirection'] == 'desc')
                {
                    
    $orderBy .= ' DESC';
                }
                else
                {
                    
    $orderBy .= ' ASC';
                }
     
    SneakyDave, LPH and BamaStangGuy like this.
  4. BamaStangGuy

    BamaStangGuy Well-Known Member

    Gotcha, thanks
     
    Bob likes this.

Share This Page