Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Model / Namespace question
  • Hey all!  Just started using FuelPHP, and so far it looks AMAZING!!  I apologize in advance if this question has been asked before, but I simply can not find the solution.  Currently I have an ORM model (http://bin.fuelphp.com/snippet/view/a2) and the default test controller (http://bin.fuelphp.com/snippet/view/a1).  Basically what I'd like to do is be able to call my model by simply calling Airport::someFunction from the controller.  I know I need to use namespaces to achieve this, but I have tried every possible combination and I keep getting "Class Airport not found".  Any help would be greatly appreciated!!
  • Also on a similar note, do I need to include `id` in my properties array if it is already defined as the primary key?
  • Your class is called Model_Airport, so you need to use Model_Airport::somefunction(). You can put it in the Model namespace, like so

    namespace Model;
    class Airport extends Orm\Model {}

    but then you'll have to use \Model\Airport::somefunction(), unless you put this in your controller:

    use \Model\Airport as Airport;

    And yes, all properties have to be defined, including your primary key.

  • Hmm...still doesn't seem to be working.  I'm now getting:

    ErrorException [ Error ]: Class 'Model\Orm\Model' not found

    I can rename my model to Airport, but I prefixed it with the Model_ because that seemed to be the standard according to the docs.
  • HarroHarro
    Accepted Answer
    Sorry, copied your original class definition, but that isn't correct too:

    class Airport extends \Orm\Model {}

    It was missing the backslash in front of Orm.

  • Because of how the autoloader works, both Model_Airport and \Model\Airport point to the same file.

    Which notation you choose is personal preference, some prefer the namespaced approach. For a coding point of view, it doesn't matter, both work.
  • Thanks!!  One more quick question ... that worked, but I then got an error saying class Log could not be found.  I fixed it by prefixing a backslash (\Log).  Does this mean the Log class is located in the Model namespace?
  • No, all Fuel core classes are in the \Fuel\Core namespace, but are aliased to the global namespace, hence the leading backslash.

    So all Fuel classes require the leading backslash, unless you're using them in a class that is not namespaced (like app controllers). All non-namespaced classes are in the global namespace, and you don't need to specify a namespace when you call a class in the current namespace.

Howdy, Stranger!

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

In this Discussion