Console error with custom param

Nudaii

Well-known member
howdy i am working on a addon for my forum to add pets to posts, the addon works as in data displays correctly, but while it allows posts to submit it causes a java console error, looking at my code i cant find whats causing it, also i know the method isnt ideal for how it picks whick pt to display isnt ideal, so any help be awesome!

Listener
Code:
<?php
  class Nudaii_Petz_Listener_PostPetz {
    public static function loadClassListener($class, &$extend) {
        if ($class == 'XenForo_ControllerPublic_Thread') {
            $extend[] = 'Nudaii_Petz_ControllerPublic_PostPetz';
        }
    }

}
?>
Controller
Code:
<?php
  class Nudaii_Petz_ControllerPublic_PostPetz extends XFCP_Nudaii_Petz_ControllerPublic_PostPetz
{

//This is the action that spawns the function, in this case viewing a post.
public function actionIndex()
{
//connect to Database
    $db = XenForo_Application::getDb();
    $Petz_in_Posts = $db->fetchAll("SELECT * FROM `xf_petz`");

    $response = parent::actionIndex();
    $response->params += array('PetzinPosts' => $Petz_in_Posts);
    //Finish up and push to Template
    return $response;
}
}
?>
Template code - not ideal, as the way to decide which pet to show kinda hacky, inline styling etc fixed later.

Code:
<xen:foreach loop="$PetzinPosts" value="$PetzinPostsVar">
<xen:if is="{$user.user_id} == {$PetzinPostsVar.user_id}">

<div class="nudaii_postbit_petz_wrapper">

<div class="nudaii_postbit_petz_stats">
<div style ="Color:white; font-size:18px;">Name: {$PetzinPostsVar.pet_name}</div>
<div style ="Color:white; font-size:18px;">Age: {$PetzinPostsVar.pet_age} Days Old</div>

  <div id="nudaii_petz_myPetzPage_pet_bars">


   <div class="nudaii_stat_mypetz_p" id="nudaii_stat_hunger">
       <b>Hunger: <font style="color:green;">{$PetzinPostsVar.pet_hunger}</font>/100</b>
   </div>
   <div class="nudaii_stat_mypetz_p" id="nudaii_stat_thirst">
       <b>Thirst: <font style="color:blue;">{$PetzinPostsVar.pet_thirst}</font>/100</b>
   </div>
   <div class="nudaii_stat_mypetz_p" id="nudaii_stat_health">
       <b>Health: <font style="color:red;">{$PetzinPostsVar.pet_health}</font>/100</b>
   </div>
   <div class="nudaii_stat_mypetz_p" id="nudaii_stat_happiness">
       <b>Happiness: <font style="color:orange;">{$PetzinPostsVar.pet_happiness}</font>/100</b>
   </div>

  </div>
</div>
<div class="nudaii_postbit_petz_avatar">
<xen:if is="{$PetzinPostsVar.pet_sex} == 0">
<img src="Nudaii/Petz/Pets/{$PetzinPostsVar.pet_type}.png" width="180px" height="180px"/>
<xen:else />
<img src="Nudaii/Petz/Pets/{$PetzinPostsVar.pet_type}_female.png" width="180px" height="180px"/>
</xen:if>
</div>
</div>

</xen:if>
</xen:foreach>
It shows the correct info per user, hippo.png, but when making a new post it causes a "console error" yet not error shows in the console.

any suggestions anyone? the error only occurs when the template (foreach) code in, so not a direct controller issue i presume, but rather something related to the param in the controller?:)
 

Lukas W.

Formerly katsulynx
You're trying to loop through a variable that might not always exist. You'll either need to catch that case in your template or append your custom information to all return-functions.

I can only recommend you to restructure your addon though. The way you fetch the information is - as you mentioned - not optimal. I'd personall extend XenForo_Model_Post::PreparePostJoinOptions to also fetch the relevant information of your addon for all posts. That way you make sure it's always there when you need it.

On a side note: The if-clause in your Listener is deprecated and replaced by the event-hint listener option in the ACP. Just drop the class you want to fire the listener on in there (XenForo_ControllerPublic_Thread in your case).
 

Nudaii

Well-known member
ok made some progress, but also hit a wall :p

Ive not worked withe xtending models before, this code works but its a core file edit which i ofcourse dont want.

Ive edited XenForo_Model_Post

to add in the petz code and it works, but not sure how i can instead have it as a addon/extend model/listener?
Code:
    if ($fetchOptions['join'] & self::FETCH_USER || $fetchOptions['join'] & self::FETCH_NODE_PERMS)
           {
               $selectFields .= ',
                   user.*, IF(user.username IS NULL, post.username, user.username) AS username';
               $joinTables .= '
                   LEFT JOIN xf_user AS user ON
                       (user.user_id = post.user_id)';
 /* JOIN Petz */
               $selectFields .= ',
                   petz.*';
               $joinTables .= '
                   LEFT JOIN xf_petz AS petz ON
                       (petz.user_id = post.user_id)';
/* JOIN Petz */
           }
would greatly appreciate any help :)
 

Nudaii

Well-known member
with this while the data does load it causes a odd effect of avatars not loading in threads. can you spot my issue?

Code:
<?php
  class Nudaii_Petz_Model_PostPetz extends XFCP_Nudaii_Petz_Model_PostPetz
{

    public function preparePostJoinOptions(array $fetchOptions)
    {
        $postJoinOptions = parent::preparePostJoinOptions($fetchOptions);

        // Petz Table
        $postJoinOptions['selectFields'] .= ', petz.*';
        $postJoinOptions['joinTables'] .= 'LEFT JOIN xf_petz AS petz ON (petz.user_id = post.user_id)';

        return $postJoinOptions;
    }
    }
?>
update it only breaks postbits/avatrs for those without pets, is there a way to could wrap above in a IF that could avoid that?

update 2 more of a round up of info the hackey direct edit of the core file doesnt cause this issue, so clearly my model extend wrong somewhere
 
Last edited:

Nudaii

Well-known member
Made some changes with help of a well known dev here, both of us think it looks right, but still the error persists. (in basic while pet data loads and message info loads as normal for all with pets, for those without pets they lose all info, styling, avatar etc)

PHP:
<?php
class Nudaii_Petz_Model_PostPetz extends XFCP_Nudaii_Petz_Model_PostPetz
{

   public function preparePostJoinOptions(array $fetchOptions)
   {
           // alway call the parent
           $postJoinOptions = parent::preparePostJoinOptions($fetchOptions);

        if(!empty($fetchOptions['join']))  {
            if($fetchOptions['join'] & self::FETCH_USER) {

        // Join Petz Table
            $postJoinOptions['selectFields'] .= ', xf_petz.*';
            $postJoinOptions['joinTables'] .= 'LEFT JOIN xf_petz ON (xf_petz.user_id = post.user_id)';

            }
        }

        // alway return parent
        return $postJoinOptions;
   }
}
?>
Sharalily has a pet so her styling remains, thomas does not.

width the Model listener active.
enabled.png

with it Disabled the styling returns
disabled.png

the addon consists of just 2 files., a listener and the model (as the pet data pulled from tables created by a seperate addon) so the error exists somewhere in my model file, but we cant see what it is.

@Chris D can you see where i went wrong? i know in past ya managed to spot my goof lol
 

Lukas W.

Formerly katsulynx
Try to throw a blank space into your strings at the start and the end.
PHP:
$postJoinOptions['selectFields'] .= ' , xf_petz.* ';
            $postJoinOptions['joinTables'] .= ' LEFT JOIN xf_petz ON (xf_petz.user_id = post.user_id) ';
I can't guarantee it's the solution (it's likely not), but sometimes it helps.
 

Nudaii

Well-known member
sadly error still there, all in all its very paffling., yet ironiclaly the hard code edit works fine, when thats applied, but when trying to properly add the same queries as a model it gives above error.
 

Nudaii

Well-known member
well basically the addon is just that model file and the listener file, the actual databasefields comes from another addon (which doesnt have a installer etc yet) however here is the database structure that this model draws from and uses if that any help?
 

Attachments

Nudaii

Well-known member
PHP:
<?php
class Nudaii_Petz_Model_PostPetz extends XFCP_Nudaii_Petz_Model_PostPetz
{

   public function preparePostJoinOptions(array $fetchOptions)
   {
           // alway call the parent
           $postJoinOptions = parent::preparePostJoinOptions($fetchOptions);

        if(!empty($fetchOptions['join']))  {
            if($fetchOptions['join'] & self::FETCH_USER) {

        // Join Petz Table
            $postJoinOptions['selectFields'] .= ', xf_petz.*, xf_user.*';
            $postJoinOptions['joinTables'] .= 'LEFT JOIN xf_petz ON (xf_petz.user_id = post.user_id)
            LEFT JOIN xf_user ON (xf_user.user_id = post.user_id)
            ';

            }
        }

        // alway return parent
        return $postJoinOptions;
   }
}
?>
is working
 
Top