The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Slim with Twig and Paris - what is better to use in controler (routes)

Petre's Avatar

Petre

19 Jul, 2012 12:36 PM

Hello,
what is better to use :
#1 :

$user = Model::factory('User')->find_one($_SESSION['user_id']);
$galleries = $user->galleries()->find_many();

$app->render('user/file/input_form.php',array('action_url'=>'/profil/plik/dodaj', 'galleries' => $galleries));

#2 :

$galleries = Model::factory('Gallery')->where('idUser', $_SESSION['user_id'] )->find_many();

$app->render('user/file/input_form.php',array('action_url'=>'/profil/plik/dodaj', 'galleries' => $galleries));

What ' s your opinion ? Which option is faster and should be used ?
Thanks

  1. Support Staff 1 Posted by Brian Nesbitt on 21 Jul, 2012 03:18 AM

    Brian Nesbitt's Avatar

    The thing about database wrappers/toolkits/active records/ORMs/query builders and the like is that its important for users to understand the actual SQL they generate and execute. That is where the real (understanding) performance is going to be gained or lost. Any good data layer should not add much overhead to the SQL being executed so its the best place to look.

    Looking at your 2 examples we can loosely translate them to the following pseudo code (I haven't specifically used paris active record before so I could be wrong in my interpretation... but the code is pretty straightforward and I shouldn't be :-).

    #1:

    $user = `SELECT * FROM users WHERE id = ` . $_SESSION['user_id'];
    $galleries = `SELECT * FROM galleries WHERE user_id = ` . $user->id;
    

    Assuming there is an index on users.id and on galleries.user_id then both should be straight forward indexed queries. At the end the $galleries are only for the user represented by the user_id in the session.

    #2:

    $galleries = `SELECT * FROM galleries WHERE user_id = ` . $_SESSION['user_id'];
    

    Assuming there is an index on galleries.user_id then this is a straight forward index query. The $galleries at the end should be the same as above.

    We can ignore the render call as they are both the same code.

    So now looking at the pseudo code we can see #1 is most likely 2 queries while #2 is only 1 query. #1 has the extra overhead of loading and mapping a user just to use the user id that you already have via the relationship.

    From what I have seen Paris isn't exactly comparable to hibernate etc. but I always look back at this post when I see something like this come up. I think its worth the read either way http://openmymind.net/2011/11/18/I-Just-Dont-Like-Object-Mappers/

    (In some ORM's the first wouldn't actually cause a hit to the DB as they use proxy objects but I doubt Paris is one of them.)

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac