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

Extend XenForo Model

Discussion in 'XenForo Development Discussions' started by Uniphix, Apr 30, 2013.

  1. Uniphix

    Uniphix Active Member

    Is there a way to extend the XenForo Model? I am looking at how to do this but when I try to get the model from the cache (create etc.) it never calls the XenForo_Model class even though it is the base apparently from what I see there may not be a way to actually extend the XenForo Model which I really need to do...
  2. Uniphix

    Uniphix Active Member

    I'm trying to create an output of something simular:

        [0] => MyAddon_Extend_Model_Catalog
        [1] => XFCP_MyAddon_Extend_Model_Catalog
        [2] => MyAddon_Model_Catalog
        [3] => MyAddon_Extend_Model
        [4] => XFCP_MyAddon_Extend_Model
        [5] => XenForo_Model
  3. Mike

    Mike XenForo Developer Staff Member

    You can only extend the "leaves" of the inheritance tree.
  4. ManOnDaMoon

    ManOnDaMoon Well-Known Member

    Could it be feasible to listen for the load_class_model code event and actually not filter anything but extend each model class with the same custom class?
    tyteen4a03 likes this.
  5. Uniphix

    Uniphix Active Member

    It won't work like this because it will attempt to create another XFCP_MyAddon_Extend_Model everytime... Which will break.
  6. Jeremy

    Jeremy Well-Known Member

    What are you trying to do?
  7. Uniphix

    Uniphix Active Member

    Whatever I really want to do is not going to work because...

    If I extend XenForo_Model_User for example: MyAddon_Extend_Model_User extends XFCP_MyAddon_Extend_Model_User

    calling XenForo_Model::create('XenForo_Model_User'); will do the following

    MyAddon_Extend_Model_User extends XFCP_MyAddon_Extend_Model_User
    which because I called ::create it will create a proxy class:

    class XFCP_MyAddon_Extend_Model_User extends XenForo_Model_User
    There is no way to extend XenForo_Model which is being used by XenForo_Model_User...

    XenForo_Model_User extends XenForo_Model
    Thus ::

    My concept will not work because in the code bit XenForo_Model_User extends XenForo_Model would actually need to be my new extended method IE:

    XenForo_Model_User extends XFCP_MyAddon_Extend_Model

    XFCP_MyAddon_Extend_Model extends XenForo_Model
    In this case it is going to be impossible to extend XenForo_Model correctly when its already being extended by....

    The one thing I could do is make a new abstract class calling it MyAddon_Model extends XenForo_Model and have all my classes base off of MyAddon_Model...

    The only issue is that I cannot extend MyAddon_Model with the same situation... More than likely I may have to use Interfaces to resolve my issues...
  8. tyteen4a03

    tyteen4a03 Well-Known Member

    You only explained why this is not going to work, but not what you are trying to achieve.

    Sample code please?
  9. Uniphix

    Uniphix Active Member

    Due to the NDA I cannot provide any direct code examples, We have basically a framework, kinda like XenForo, that overrides a lot of the default systems of XenForo, but for us to really achieve our goals we've had to modify XenForo Framework to suit what we need.

    tyteen4a03 and Bob like this.
  10. tyteen4a03

    tyteen4a03 Well-Known Member

    Not sure why you can't extend every class that uses XenForo_Model since every model will extend this class. If you need to directly access a grandparent's (or even more levels up) static method, you can directly use the class name, PHP will recognize.

    class A
      protected function something()
        echo "And I";
    class B extends A
      protected function something()
        echo "Myself, ";
    class C extends B
      protected function something()
        echo "Me, ";
      public function call()
        echo self::something(); // self
        echo parent::something(); // parent
        echo foo::something(); // grandparent
    $obj = new C();
    Output: "Me, Myself, And I"

    But well, if they pay you well enough for the job, I'd say edit XF code, you won't be able to easily upgrade to 1.2 anyways :p
  11. Jeremy

    Jeremy Well-Known Member

    At a high level, what functionality do you need to override? There may be ways to achieve what you'd like to do without extending XenForo_Model directly, which, as Mike said, is impossible with the system.
    tyteen4a03 likes this.
  12. Uniphix

    Uniphix Active Member

    Yup Mike is correct and my version of XenForo is very heavy modified, and when it comes time to upgrade i'll just have to do some merging lol
  13. Uniphix

    Uniphix Active Member

    To answer your questions we don't want to really override any methods we want to add more methods to XenForo_Model that is used in about 90% of the system... I've had to create my own "XenForo_Model" that extends that XenForo_Model then had my addon base off that
  14. Marcel

    Marcel Active Member

    At the risk of getting this completely arse-ended and looking incredibly stupid...

    Could you just not extend a current default model...but not actually use it?
    For example, you're creating MyModel_Model, which extends XenForo_Model_User, which extends XenForo_Model.

    You then have access to the grandparent XenForo_Model, and nothing says you have to use anything from the parent XenForo_Model_User

    Or have I got it wrong?

    Edit : Yup, I've got it wrong. I think I understand. You're trying to extend XenForo_Model in order to overwrite methods within XenForo_Model, and using the method I describe you can't overwrite grandparent methods, only parents?
  15. xf_phantom

    xf_phantom Well-Known Member

    Wouldn't it be better to create your own model with the methods and extend XenForo_Model from your?
    It's ONLY 1 fileedit which shouldn't be problematic with vcs tools!

    abstract class XenForo_Model extends Your_Model
    that's it:)
    now you have access to ALL your own methods in all Models.. One could even do this with XenForo_View and XenForo_Controller:D

    Edit: Sorry, it seems that you're doing this already:oops:

  16. Liam W

    Liam W Well-Known Member

    Technically you could do it without code edits, just new files...

    Create a file with a custom class: XenForo_Model_Custom (example name, will obviously be different).

    This class extends your one, and is basically identical to the main XenForo_Model class.

    Then, use the init_dependencies hook with the following code:

    eval("class XenForo_Model extends XenForo_Model_Custom {}");
    That way the actual library/XenForo/Model.php file is never read.

    This is probably very, very hacky, but if it's only internal it works :p


Share This Page