Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to extend on classes in packages?
  • I'm trying to extend the Orm\Query class but the usual method doesn't seem to work. Probably because the package has it's own bootstrap. Can anyone tell me how I would go about doing this? Thanks!
  • First you must include the class in your config.php file (autoloader) or \Fuel::add_package('orm'); From there you must make sure you use the full namespaced class name...
    class MyQuery extends Orm\Query {}
    
  • That creates a second class that's not actually used by the ORM though. I want to actually extend the ORM Query class so that any queries made over the ORM include my modifications.
  • It mostly depends on what you're trying to do with it. If you want to utilize different ORM functionality, one alternative would be to create a secondary package the extends the current ORM.
    PKGPATH/my-orm
    

    Create identical class names for everything, under the new namespace, and extend the ORM. Once you've done that, inside of your models, you will need to call them the same way you would've done with the origional orm package.
    class Model_Whatever extends MyOrm\Model {
    
    }
    

    Not sure if this is the best implementation of it. Could you elaborate a bit more on what you're trying to do?
  • Hi, The method nerdsrescueme has described is correct. Are you talking about overloading existing methods declared within the ORM class specifically?
  • Sorry I should have clarified. What I'm trying to do is add a new method "get_array" to the Query class which will return the results in a different format. What I'm currently doing with other classes such as the Arr class is this:
    class Arr extends \Fuel\Core\Arr {}
    

    And then in my bootstrap file:
    Autoloader::add_classes(array(
     'Arr'     => APPPATH . 'classes/extend/arr.php'
    ));
    
    Basically I want the equivalent of this for the ORM query class, but since it resides inside a package I am not sure on how to proceed. I suppose copying the ORM class altogether and making my modifications that way would work, but it would be a pain to update when there's a new version of the ORM class.
  • I believe Jelmer is looking to add this. Checkout the issues for the ORM class I think I saw it listed there.
  • Ah, I see now. I have no idea how that would be done to be honest. Perhaps instead of directly editing the query.php file... make an extended class inside of the package and drop it into the bootstrap file... then have the query.php file inherit from that? Could work possibly.
  • Frank Bardon wrote on Thursday 14th of July 2011:
    Ah, I see now. I have no idea how that would be done to be honest. Perhaps instead of directly editing the query.php file... make an extended class inside of the package and drop it into the bootstrap file... then have the query.php file inherit from that? Could work possibly.

    Yeah I guess for now I'll do something along these lines. Shouldn't be too hard to keep track of changes when updating. Thanks guys

Howdy, Stranger!

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

In this Discussion