HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Getting protected info from Sender object. DEMystification help assist needed.

Ok.

I have

$results = $Sender->DiscussionModel->Database;

a var_dump of

$results is composed of this...

object(Gdn_Database)#25 (14) {
  ["ClassName"]=>
  string(12) "Gdn_Database"
  ["_CurrentResultSet":"Gdn_Database":private]=>
  object(Gdn_DataSet)#87 (6) {
    ["Connection"]=>
    object(PDO)#28 (0) {
    }
    ["_Cursor":"Gdn_DataSet":private]=>
    int(-1)
    ["_DatasetType":protected]=>
    string(5) "array"
    ["_EOF":protected]=>
    bool(false)
    ["_PDOStatement":"Gdn_DataSet":private]=>
    NULL
    ["_Result":protected]=>
    array(1) {
      [0]=>
      array(53) {
        ["UserID"]=>
        string(1) "2"
        ["Name"]=>
        string(1) "Me"
        ["Password"]=>
        string(34) "$P$B2E0DsEIaOd7jIPGPmgjfmRKIP8TuK."
        ["HashMethod"]=>
        string(7) "Vanilla"
        ["Photo"]=>
        string(0) ""
        ["About"]=>
        string(66) "the next xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        ["Email"]=>
        string(10) "Me@none.com"
        ["ShowEmail"]=>
        string(1) "0"
        ["Gender"]=>
        string(1) "m"
        ["CountVisits"]=>
        string(2) "32"
        ["MysteryVariable"]=>
        string(1) "How do I pull out this value"
      }
    }
  }

How can I get the info in "MysteryVariable" which is the last item in a series of protected and private arrays.

If you have a function that retrieves the value - I would be unmystefied.

I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

Comments

  • You can use the Result method to access the data returned from the query.

    $results = $Sender->DiscussionModel->Database->Result();
    

    Add Pages to Vanilla with the Basic Pages app

  • peregrineperegrine MVP
    edited December 2013

    the php code in a simple plugin is this

    the data I want is floating around in the Sender object. I just want to pull the user info without doing a separate sql call or switching the vent or caching it and retrieving it.

     public function DiscussionController_AfterCommentBody_Handler($Sender) { 
    //this produces the above info.... in previous comment.
    $results = $Sender->DiscussionModel->Database;
    
    }
    

    Thanks Shadowdare....

    if I use the code you suggested Shadowdare - it produces this.... (we may be talking about different animals).

     public function DiscussionController_AfterCommentBody_Handler($Sender) { 
        $newresults = $Sender->DiscussionModel->Database->Result();
     }
    

    it produces this error....

    Fatal error: Call to undefined method Gdn_Database::Results() in /var/www/vanilla/plugins/Newbie/default.php on line 27 Call Stack: 0.0003 339768 1. {main}() /var/www/vanilla/index.php:0 0.6478 9072212 2.

    I don't even need a vanilla built in method - the question is how can I dive deeper in to the object and get the info when it is protected and private.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • by the way guilty: I'm testing in 2.0.18.8. I could shift to 2.1b2 but I thought I'd try it here first. I suspect the same error in 2.1b2. but I'll give it a try there.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Ok - I now realize the user info was stashed into the sender array by another plugin.

    I was just trying to figure out how to retrieve it in this one, since it was floating around in sender anyway. I guess it is put in the sender array by SymbolEdit plugin.

    When I disabled SymbolEdit the data was no longer available.

    But the question still remains. if the Data is floating around in Sender as above, is there a way to retrieve it.

    So in a nutshell if symboledit plugin is used - it adds the data in to Sender object.

    and if i use this plugin in addition - how can i get the protected and private data.

    <?php if (!defined('APPLICATION')) exit();
    
    
    $PluginInfo['Newbie'] = array(
       'Name' => 'Newbie',
       'Description' => 'Newbie',
       'Version' => '1.0',
       'Author' => "Peregrine",
       'SettingsUrl' => '/dashboard/plugin/newbie'
    );
    
    
    class NewbiePlugin extends Gdn_Plugin {
    
    
    
     public function DiscussionController_AfterCommentBody_Handler($Sender) { 
      $results = $Sender->DiscussionModel->Database;
       var_dump($results);
    }
    
    }
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • ShadowdareShadowdare r_j MVP
    edited December 2013

    You're right that what I posted won't work. I couldn't check half an hour ago. The error shows up in 2.0.18.8 and 2.1b2 as well if you use that code.

    The var_dump you had in the first post shows user data results. That doesn't seem to be there by default in either 2.0.18.8 or 2.1b2 and looks like something you injected in. Would you post your code for injecting your user data into that object?

    Edit: Saw your last post after refreshing the page. Just to confirm, by "if i use this plugin in addition," do you mean you still want to retrieve the same data you showed in your first post that was returned by SymbolEdit in $Sender->DiscussionModel->Database? If so, would you post the code you are using in SymbolEdit that's injecting the user data into $Sender->DiscussionModel->Database?

    Add Pages to Vanilla with the Basic Pages app

  • peregrineperegrine MVP
    edited December 2013

    thx for assist @Shadowdare

    Can i get protected and private info out of it and how? Or does protected and private absolutely prevent that ability.

    Its kind of immaterial whether Symboledit or any plugin put the info into the Sender array.

    The bigger question I have - once its there - how can I access it from the Sender object, once its there.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • ShadowdareShadowdare r_j MVP
    edited December 2013

    In any case, you need to determine if you need to (1) only retrieve and make a copy of the "MysteryVariable" or (2) modify the "MysteryVariable" by reference so that any other code that has access to $Sender->DiscussionModel->Database will see any changes you made to the "MysteryVariable."

    A solution to this problem is to avoid injecting the data into the DiscussionModel->Database instance as SymbolEdit is currently doing. To do this, you can modify SymbolEdit to pass data with EventArguments in the controller or pass in the results of a SQL query by the Result method into the controller's data object which then can be accessed by $Sender->Data('NameOfDataSet').

    To answer your question of "how to access protected and private info?", there is a way in PHP to do so called "reflection." The reflection features were added since one version of PHP 5 and let you access all the data of an object that is an instance of a class, but it is better to stick with the idea of encapsulation, which is what Vanilla is doing with the Result method stuff. Reflection should be used as tool and not as a standard way of doing things, because it goes against the purpose of making things in the class protected or private.

    Add Pages to Vanilla with the Basic Pages app

  • peregrineperegrine MVP
    edited December 2013

    thanks much @Shadowdare

    :):D (*)

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • ShadowdareShadowdare r_j MVP
    edited December 2013

    This is a quick example of using EventArguments:

    Somewhere in the SymbolEdit code where you are hooking into an event that the controller or its view will fire, you can add something like this to assign an event argument:

    $Sender->EventArguments['SymbolEdit']['ExampleVariable'] = "An example.";
    

    To retrieve the variable, you would hook into an event in the same controller and use something like:

    $result = $Sender->EventArguments['SymbolEdit']['ExampleVariable'];
    

    Add Pages to Vanilla with the Basic Pages app

  • peregrineperegrine MVP
    edited December 2013

    thanks much. Shadow dare. Since I started on the whole premise that the all the user info was provided by the core and not by my other plugin, I'm going to scrap the idea of retrieving data, because I don't want to require the SymbolEdit plugin. But I learned a bunch of stuff on the way

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

Sign In or Register to comment.