Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product
Vanilla 2.6 is here! It includes security fixes and requires PHP 7.0. We have therefore ALSO released Vanilla 2.5.2 with security patches if you are still on PHP 5.6 to give you additional time to upgrade.

How can I access DiscussionModel data in DiscussionController Object?

R_JR_J Cheerleader & TroubleshooterMunich Moderator

I have following "problem": I want to access the info from table Media that are connected to a special discussion.

At first I thought I have to query the Media table, but I found out that all the relevant informations are already in the DiscussionController. Look at what I get when I do a decho($sender) from any DiscussionController event:

<br />  DEBUG: DiscussionController Object
    [DiscussionModel] => DiscussionModel Object
        [Watching] => 
        [Data] => 
        [Database] => Gdn_Database Object
            [ClassName] => Gdn_Database
            [_CurrentResultSet:Gdn_Database:private] => Gdn_DataSet Object
                [Connection] => PDO Object

                [_Cursor:Gdn_DataSet:private] => -1
                [_DatasetType:protected] => array
                [_EOF:protected] => 
                [_PDOStatement:Gdn_DataSet:private] => 
                [_Result:protected] => Array
                    [0] => Array
                        [MediaID] => 5

As you can see $sender->DiscussionModel->DataBase->_CurrentResultSet->_Result shows information about the connected Medias (in fact all relevant informations are already there!). But since they are protected, I do not have access to them :(

I do not want to query the db for information of the Media if it is already availabe in the DiscussionController, but I cannot find a way to use this information. Does anybody has a hint for me?



  • oliverraduneroliverraduner Contributing to Vanilla since 2010 Switzerland ✭✭


    Aside your question which I cannot contribute to, just wanted to thank you for posting this helpful code snippet to inspect $Sender objects :+1:

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Thanks @peregrine, I'll try to understand that.
    @x00: do you mean that if I find a DiscussionModel as a part of the DiscussionController, I could access that DiscussionModel also on another way? Is there a way to examine "the whole scope" - do you have a hint for a starting point?

    In the meantime I do not need that anymore, but I'm still curious.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    @oliverraduner said:
    Aside your question which I cannot contribute to, just wanted to thank you for posting this helpful code snippet to inspect $Sender objects :+1:

    Sometimes you will not be able to see the output unless you stop the process: decho($sender); die; But for sure you cannot do this in your live system.

    You can also write to your file system file_put_contents('/var/log/nginx/vanilla.log', print_r($sender, true));. Certainly you need to change the file path according to your needs.

    And while writing this down, I thought of creating a helper function for myself! I've added this function to my /conf/bootstrap.before.php file:

    function d($output = '', $destination = '/var/log/nginx/vanilla.log') {
        if ($output === '') {
            $output = debug_backtrace();
        } elseif (!is_string($output)) {
            $output = print_r($output, true);
        file_put_contents($destination, $output);

    Not yet tested it and I'm not sure if using d() without parameters will give useful information, but you can use d($sender) to dump the contents of the variable to a file if decho() is not usable.

Sign In or Register to comment.