Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Need Help with a PlugIn

R_JR_J Ex-FanboyMunich Admin
edited June 2013 in Vanilla 2.0 - 2.8

I really wanted to finish my plugin complete by myself, but I'm stuck and need your help. (If it is relevant: I'm using 2.0.18)

I've created a small plugin and wanted to have it as "clean" as possible by giving it a nice structure and so I've created a "models" subdirectory and saved class.pluginnamemodel.php in that directory. I've got a public static function SetSomething in this model and it works exactly as expected. I can use it in any way I want to and everything is fine. Except when I use it from my plugins Setup() function! I've coded it similar to this:

public function Setup() {
$this->DataUpdate();

protected function DataUpdate(){
PluginnameModel::SetSomething();

When I activate the plugin, it shows me the following error message:

Fatal error: Class 'PluginnameModel' not found in /vanilla/plugins/Pluginname/class.pluginname.plugin.php on line 190

Line 190 is this one with the call to the model function => PluginnameModel::SetSomething();

Can someone please explain to me, why I cannot make this function call in Setup but anywhere (or better anytime) else?

P. S.: I could easily drop my idea of using a model file. It contains only 3 functions which are in itself shorter than 10 lines and so my code is artifically blown up in a somewhat messy way with this extra file. But I've done this as an exercise and I'm so stubborn that I insist on doing it this way ;-)

Comments

  • Options

    "Class 'PluginnameModel' not found"

    to create own model at Plugin, please make sure file must in [plugin_dir]/lib/classess/models/class.namemodel.php

    filename must have model at end, for example your class name is foo so filename is class.foomodel.php

    new class model must extends from class Gdn_Model.

  • Options
    R_JR_J Ex-Fanboy Munich Admin
    edited June 2013

    Man, I'm so desperate that I've even tried it with classess instead of classes ;-)
    I'm now using the path you advised me bu it does not change my error message.

    What's making me furious is that I could use "PluginnameModel::SetSomething();" and the containing function "DataUpdate()" from any other hook of my plugin but not from the Setup function... :-/

  • Options
    lifeisfoolifeisfoo Zombie plugins finder ✭✭✭

    Do you have included class.pluginnamemodel.php in the plugin's main class file?

    There was an error rendering this rich post.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    @lifeisfoo: nope. I thought that there is an autoloading feature and I do not have to do it. And as I said before: If I use it for example in "DiscussionsController_AfterDiscussionTitle_Handler", I do not have to include it explicitly.
    Is it possible that autoloading is not done in the setup function?!

  • Options
    hgtonighthgtonight ∞ · New MVP

    @R_J Plugins are enabled and disabled through the /applications/dashboard/controllers/class.settingscontroller.php file. It in turn uses the /library/core/pluginmanager.php.

    The important thing to understand is that this doesn't initialize the plugin through the autoloader (which would load the model). It just includes all plugin files (default.php and *plugin.php) in the plugin folder and executes the Setup() method.

    Long story short, you either have to include the model in your Setup() method but not anywhere else in your plugin.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    lifeisfoolifeisfoo Zombie plugins finder ✭✭✭

    If you paste code here, can be more easier to give you help.

    There was an error rendering this rich post.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    @hgtonight: sounds logically (but to very satisfying).

    @lifeisfoo said:
    Do you have included class.pluginnamemodel.php in the plugin's main class file?

    At least I should have tried before asking...

    I'll do as soon as I can and give feedback. I thought that posting the code would be way too complicate if I just have one simple problem. If including the model in the setup function will not solve this problem, I'll post plugin and model source

  • Options

    @R_J said:
    Man, I'm so desperate that I've even tried it with classess instead of classes ;-)
    I'm now using the path you advised me bu it does not change my error message.

    sorry i missed spelling 'classess', should be 'classes' :)

    [plugin_dir]/lib/classes/models/class.namemodel.php

    its just my self experience.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    All right: I've included the model in the Setup and now everything works as expected - many thanks for your help! :-)

    Including one class in one function feels ugly, though... I was really enjoying this autoloading thing and that it is not consistently working throughout the same class is somehow awkward

  • Options
    hgtonighthgtonight ∞ · New MVP

    @R_J I agree this is a little confusing, but the setup function is designed to do what needs to be done before the plugin can be run properly the first time. You can think of it as a bootstrap function that is included in the plugin's class.

    In this case, it doesn't make sense for the model's data to be updated, since there is no model to update. I am not sure a model requiring setup like this is designed "properly." I am not sure on the specifics of your plugin, but the "proper" way to do this would have the model instantiate itself on the first run if it doesn't already exist.

    E.g.

    class MySweetModel extends VanillaModel {
      public function __construct() {
        parent::__construct('MySweetModel');
        // Check to see if the necessary data exists and use defaults if not
        if(!exists(MyNecessaryData)) {
          $this->FirstRunSetup();
        }
      }
    }
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    So it is not the autoloader implementation that is awkward, but my design? Well, I have to admit that I wouldn't bet a penny on my design and in fact you're right: my plugin extends the discussion table and I want to prefill my new column directly on setup. This is practical for development, but a catastrophe for a live board with a lot of postings!

    So on my "roadmap" (sounds way too professional) is an option for the dashboard that enables the admin to "refresh" the information. First time it is done, it is no refresh but an initialisation (I haven't thought about a nice wording yet)

    I already knew that this would be the proper way to implement it, but I haven't thought about the fact that using database access in Setup could be generally bad design. That atomises my disappointment :-)

Sign In or Register to comment.