ElkArte Community

Elk Development => Feature Discussion => Topic started by: inter on December 16, 2013, 05:29:37 am

Title: new db style
Post by: inter on December 16, 2013, 05:29:37 am
long ago I wanted to offer new style of access to a database

Example.

old style
Code: [Select]
<?php
# ...

    $db = database();

    # ...

    // Grab the categories sorted by cat_order.
    $request = $db->query('', '
        SELECT id_cat, cat_order
        FROM {db_prefix}categories
        ORDER BY cat_order',
        array(
        )
    );
    while ($row = $db->fetch_assoc($request))
    {
            # ...
            $cat_order[$row['id_cat']] = $row['cat_order'];
    }
    $db->free_result($request);

    # ...
   
new style
Code: [Select]
<?php

    use Elkarte\DB;

    # ...

        // Grab the categories sorted by cat_order.
        $q = DB::query('
            SELECT id_cat, cat_order
            FROM {db_prefix}categories
            ORDER BY cat_order',
            []
        );
        while ($row = $q->fetch_assoc) {
            # ...
            $cat_order[$row['id_cat']] = $row['cat_order'];
        }
        $q->free;

        # ...

DB class here (https://github.com/interlab/cool-db-smf)
Title: Re: new db style
Post by: emanuele on December 16, 2013, 05:37:24 am
If I understand it correctly the use of Closure means PHP > 5.3, right?
Title: Re: new db style
Post by: inter on December 16, 2013, 05:40:53 am
it is possible to make on another:
is_callable (...

the main idea in that that was easier to perceive a code (probably I am not right therefore any judgement is interesting to me)
Title: Re: new db style
Post by: emanuele on December 16, 2013, 05:56:07 am
ohhh.. now I think I see what you mean (was paying attention to other details and not the main one).

It is indeed easier to read and I thought about something like that from time to time, dunno about the fact that it has to re-instantiate the class for each and every query, seems a lot of overhead, but code design is not my field, so I don't know.
Title: Re: new db style
Post by: inter on December 16, 2013, 06:01:42 am
the class completely isn't completed - there shall be still methods
I simply showed - someone can will want to use it
If someone has ideas - will be glad to look