Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Unclear on how to do this:
  • I have a model with the following function:
     public static function get_advice($id)
     {
      $result = DB::select('*')->from('advice')->where('id', $id)->execute();
      if ( DB::count_last_query() > 0 )
      {
       $result_array = $result->as_array();
       foreach($result_array as $row)
       {
         // return one row of advice
        return($row);
       }
      }
       // row not found
      return(FALSE);
     }
    
    I get this error message, pointing at the DB::select... in the above function
    ErrorException [ Error ]: Class 'Model\DB' not found
    Why is it not recognizing the call to the DB class?
  • Because your model is in the "Model" namespace, and the "DB" class is in global? If you don't specify a namespace, the class will be searched for in the current namespace. To avoid issues like this, I always prefix core classes with a backslash, indicating I want to load from global. Even if it's not really needed, but the habbit avoids issues like this...
  • I assume you mean something like this:
     $result = \DB::select('*')->from('advice')->where('id', $id)->execute();
    
    Which doesn't stop the error from occurring. I miss your hobbit reference, too obtuse... How would you recommend "avoiding" this case?
  • I retract my statement.
    It works when you locate every class reference in the file and add the backslash....
    Thanks for the assist.
  • Yeah. What I meant was if you develop the habbit of writing \DB for every core class, no matter whether your in the global namespace or not, this problem can never happen. Alternatively, you can do
    use \DB as DB;
    
    at the top of your file, under the namespace declaration, and then you can continue to use DB without the backslash. But you'll have to do that for all core classes you use.
  • Thanks, this could be developed a little more in the docs, but I understand your explanation. The docs don't address it at all. Hopefully this thread will be read by others trying to switch to Fuel. Sorry also, it's habit, and I read it as hobbit... need new glasses.
  • lol. And I need to hold back on using the letter 'b' too often. We assume the FuelPHP dev is familiar with namespaces (it's a prerequisite for using Fuel), so it's not documented as such.

Howdy, Stranger!

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

In this Discussion