0

I ran into a problem what i cant really solve (i am a begginer with fuelphp).

So when i place input in my model i get this error

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

when i try with session its the same thing

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

And when i try to hardcode a value in it its not inserting, debuged the query no errors. it shows the value is posted (when passing hard code value) but not inserts it in the database.

my code

model

namespace Model;
use DB;

    class Event extends \Model {

        static function send_event()
        {
            $query = DB::insert('events');

            $query->set(array(
                'user_id' => Session::get('sentry_user'),
                'event_name' => Input::post('event_name'),
                'event_desc' => Input::post('event_desc'),
                'event_start' => Input::post('event_start'),
                'event_end' => Input::post('event_end'),
            ));
        }

}

controller

function action_send_data()
{
    $response = Response::forge();
    $val = Validation::forge('events');

    $val->add_field('event_name', 'Esemény neve', 'required');
    $val->add_field('event_desc', 'Esemény leírás', 'required');
    $val->add_field('event_start', 'Esemény kezdődik', 'required');
    $val->add_field('event_end', 'Esemény kejár', 'required');  


    Event::send_event();
    $response->body(json_encode(array(
        'status' => 'ok',
    )));


    return $response;
}

could please someone give me a hint what i am doing wrong?

thank you

P.S: in the controller i removed the validation for query debuging

4

1 回答 1

5

When you declare a namespace at the top of a file as you have done with

namespace Model;

This declares the namespace for all classes called where the namespace is not explicitly defined. For example, your call to Session is actually looking in Model\Session when it actually exists in \Fuel\Core\Session

There are two ways around this. You've demonstrated an example yourself in your question with a call to the use language construct.

use DB;

This causes PHP to search for classes in this namespace as well as the one which is already being used (Model in this case).

The other way you can do it is by calling the class with the explicit namespace. For example

\Fuel\Core\Session::get();

Fuel also aliases all core classes to the root namespace for convenience. What this means is you can also directly call classes in \Fuel\Core just using \. For example

\Session::get();
于 2012-08-17T08:47:03.933 回答