fuel / docs

Fuel PHP Framework - Fuel v1.x documentation
http://fuelphp.com/docs
Other
168 stars 234 forks source link

Confusion creating Model Classes #247

Closed zoe-edwards closed 12 years ago

zoe-edwards commented 12 years ago

On the Models page, the instructions for creating a Model are as such:

namespace Model;

class Welcome extends Model {

    public static function get_results()
    {
        // Database interactions
    }

}

Which I’ve used in a Model I’ve made like so:

namespace Model;

class Users extends Model
{

    public static function create($pairs)
    {
        return DB::insert('users')->set($pairs)->execute();
    }

}

First error is ErrorException [ Error ]: Class 'Model\Model' not found which is reasonable, as I haven’t created a Model class to extend from. So I remove that line.

class Users

Then I get ErrorException [ Error ]: Class 'Model\DB' not found referring to the line calling DB::insert. That seems reasonable, because I’m in the Model namespace? But obviously I remove that, then the Controller can’t find the Model.

I’m not sure what the correct way of managing it is?

Using 1.1 RC1

kenjis commented 12 years ago

Yes, this is confusing.

crynobone commented 12 years ago
namespace Model;

use DB;

class Users extends \Model
{

    public static function create($pairs)
    {
        return DB::insert('users')->set($pairs)->execute();
    }

}

or alternatively

class Model_Users extends \Model
{

    public static function create($pairs)
    {
        return \DB::insert('users')->set($pairs)->execute();
    }

}

[edited by Jelmer: take DB from global, not Fuel\Core\DB and you forgot the backslash prefix in the second example.]

jschreuder commented 12 years ago

I'm sorry guys, but this is REALLY basic namespaces stuff in PHP - the For Noobs section. Prefix a backslash to take classes from global instead of the current namespace: http://nl.php.net/manual/en/language.namespaces.basics.php#example-225

kenjis commented 12 years ago

The documentation is confusing for beginners of fuel.

http://www.fuelphp.com/dev-docs/general/models.html at least it is below, isn't it?

namespace Model;

class Welcome extends \Model {

    public static function get_results()
    {
        // Database interactions
    }

}
jschreuder commented 12 years ago

Ah, I didn't see it was actually missing in the examples. Though understanding this type of error should still be common knowledge. I added a note about it, but I can't promise it'll stay - it's about the most basic mistake one can make when using namespaces.

[edit] Just to clarify: Fuel docs shouldn't do a namespaces 101, anyone who doesn't have a clue about them should use the php.net docs - we're not here to teach basic PHP.