Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Restfull API, with Form
  • Try to avoid DB::query(), use it as an absolute last resort. It is complex, it causes high-maintenance, and it is potentionally insecure.

    Instead, use

    $library = \DB::select("id", "title", "author", "type")
        ->from("library")
        ->where("id_author", "=", $id_author)
        ->order_by("id", "asc")
        ->execute();
  • I already try this, i will keep using orm for this i guess
  • Why?

    If you want to use DB without the ORM abstraction layer, that code example I gave you should work perfectly fine...
  • No, i already try this and got always a :

    Fuel\Core\Database_Exception [ 42000 ]:
    SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id' at line 1 with query: "SELECT DISTINCT id, title, author, type FROM library WHERE id_author= ORDER BY id"
  • That is not SQL generated by Fuel's query builder.

    Or your db configuration is wrong, for MySQL and MariaDB, column names are generated between backticks.

        /**
         * Base PDO config
         */
        'default' => array(
            'type'        => 'pdo',
            'connection'  => array(
                'persistent' => false,
                'compress'   => false,
            ),
            'identifier'   => '`',
            'table_prefix' => '',
            'charset'      => 'utf8',
            'collation'    => false,
            'enable_cache' => true,
            'profiling'    => false,
            'readonly'     => false,
        ),

    Your db config needs this "identifier" value.
  • Everything fine now :) 
    Thanks to you

    TABLE Book 
    - ID
    - Name
    - Author

    How can i do with an orm request for have : 
    /api/library/?bID=7&bname=Millenium

    bID ---> it's my book ID 
    bname ---> it's my book name
  • I don't understand the question. What does a URL with GET variables have to do with ORM?
  • If i do something like that :

    public function get_infos($id = 0){

    $infos = \Model_Library::find('all', array(
         'select' => array ('name'),
         'where' => array( array( 'id', $id),
        ),
       ));


    If i do that i can go to : http:localhost:8888/api/infos/?id=2
    And i want to do something more obvious..
    Like : http://localhost:8888/api/infos/?bookID=2
  • HarroHarro
    Accepted Answer
    You can't.

    FuelPHP is designed to work with URI segments, not with GET variables. Extra URI segments are passed on as arguments for the action called.

    So if your URL is http:localhost:8888/api/infos/2, $id will contain "2".

    If you want to use GET variables, you have to fetch them manually:

    public function get_infos()
    {
        $id = \Input::get('id', 0);
        // or
        $id = \Input::get('bookid', 0);
        ...

    You can emulate this via URI segments, when using something like http:localhost:8888/api/infos/id/2/name/something and then do

    public function get_infos()
    {
        $vars = \Uri::to_assoc();
        ...

    which would give you

    array(
        'id' => '2',
        'name' => 'something'
    )
  • I find an other way without \Uri::to_assoc();

    Thank you

Howdy, Stranger!

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

In this Discussion