LebGeeks Forums

A community for lebanese technology enthusiasts.

You are not logged in.

Announcement

Make sure you read the LebGeeks Forum Etiquette

#1 28-07-2010 04:30:43 pm

arithma
Member
From: Saida, Lebanon
Registered: 09-12-2009
Website

Nice little ActiveRecord Framework

http://www.phpactiverecord.org/projects … sociations
Exactly my type of framework, minimalistic, no commandline/xml mumbo jumbo, and as clean as PHP can get.
I will be using this instead of making my own!

Offline

 

#2 28-07-2010 06:05:45 pm

rolf
Member
From: Beirut
Registered: 05-04-2005

Re: Nice little ActiveRecord Framework

Thanks for sharing :) I like it, it can be easily embedded.
I wonder... do you know a framework that allows you to "bind" form fields with table fields, in just a couple of commands? Without needing to create getters and setters...


mpt.gov.lb : Sucking the fun out of Internet since 1990 !

Offline

 

#3 28-07-2010 07:11:05 pm

arithma
Member
From: Saida, Lebanon
Registered: 09-12-2009
Website

Re: Nice little ActiveRecord Framework

There are a few associative array to presentation kind of views for PHP if that's what you meant. I could use a tip of that myself though.

Offline

 

#4 28-07-2010 08:39:03 pm

rolf
Member
From: Beirut
Registered: 05-04-2005

Re: Nice little ActiveRecord Framework

arithma wrote:

There are a few associative array to presentation kind of views for PHP if that's what you meant. I could use a tip of that myself though.

I was thinking something like that... :-D  ...

Code:

<?php

include('app.php');
include('lib/magic_form.php');
magic_form_init_db("localhost", "MySqlUser", "MySqlPass", "DbName")

?>

<script src="js/magic_form.php"></script>

<form magic_form_bind_table="tableName" magic_form_match="id=<?=$id?>">
   <input magic_form_bind_field="colName">
   <input magic_form_bind_field="colName2"
</form>

This is very rough, i'm think there are better ways to do this... but the idea is that by using a js include and a php include, you create placeholders in the HTML, and "bind" these to elements in the database, and then you dont need to worry about the whole POST-Query and all that sequence. It is all handled in the "magic_form" library. If the value changes in the database, then it will change in the UI, and if the user changes it and submits, then it will also be saved in the database.

Regular php variables, ex:

Code:

<?php
$var = "Hello World";
?>
<html>
  <body>
    <h2><?php echo $var ?></h2>
  </body>
</html>

Are unidirectional, in the sense where they appear on the server-generated page, but if you want to do the same stuff but the other way round... that is have the user generate a fill a variable...then you have to go through the complicated and annoying POST stuff.

So what I'm thinking is it would be nice if it was as easy in both directions, and ever more, if the same variable (what i called placeholder earlier) could be used for both directions.

I think it can be possible if we user both PHP and JavaScript (and they communicate among themselves).


mpt.gov.lb : Sucking the fun out of Internet since 1990 !

Offline

 

#5 29-07-2010 04:57:21 pm

rolf
Member
From: Beirut
Registered: 05-04-2005

Re: Nice little ActiveRecord Framework

My post pretty much killed the thread... thought so...


mpt.gov.lb : Sucking the fun out of Internet since 1990 !

Offline

 

#6 29-07-2010 05:27:08 pm

rahmu
Moderator
From: Paris
Registered: 11-04-2009
Website

Re: Nice little ActiveRecord Framework

No it doesn't work that way.

You need a way to send data to your server so it'd be saved in a database right? The way Apache works is that it expects requests in HTTP, like GET, POST, HEAD, PUT, TRACE, ...

At one point or another, if you are sending the data to Apache (who will then send it to the DB) you have to send a POST request from your browser. As you probably know, POST could be sent from HTML form, or AJAX.

"Magical binding" cannot happen. Moreover, for security reasons, a POST request must be treated server-side before being executed. Your idea of the magic binding (if it existed) would be a great security threat. (You are basically allowing any user to input whatever he wants into your db :-/ )


"First shalt thou take out the Holy Pin, then shalt thou count to three, no more, no less. Three shall be the number thou shalt count, and the number of the counting shall be three. Four shalt thou not count, neither count thou two, excepting that thou then proceed to three. Five is right out."

Offline

 

#7 29-07-2010 05:35:03 pm

arithma
Member
From: Saida, Lebanon
Registered: 09-12-2009
Website

Re: Nice little ActiveRecord Framework

My post pretty much killed the thread... thought so...

Warnock's Dilemma applies.

@rahmu: Rolf didn't say that the post is eliminated necessarily. The code he posted could be handling the posting somewhere internally. I am sure he understands it's impossible to go around the communication back with the server.

Anyway, I started out with the framework, then decided to go back and work from scratch. As usual, I tried writing some general purpose wrapper around MySQL, and failed miserable:

Code:

<?php
function query_assoc($sql){
    $set = mysql_query($sql);
    
    if($set === false) return false;
    
    $result = array();
    while($row = mysql_fetch_assoc($set)){
        $result[] = $row;
    }
    
    return $result;
}

function sql_escape($in){
    return mysql_real_escape_string($in);
}

function sql_escape_array($in){
    return array_map("mysql_real_escape_string", $in);
}

function sql_insert($table, &$in){
    if(empty($in)) return;
    
    $keys =
        join(
            ", ",
            array_map(
                "sql_tick",
                sql_escape_array(array_keys($in))
            )
        );
    $values =
        join(
            ", ",
            array_map(
                "sql_quote",
                sql_escape_array(array_values($in))
            )
        );
    
    $sql = "INSERT INTO `$table` ($keys) VALUES ($values)";
    mysql_query($query);
    $in['id'] = mysql_insert_id();
}

function sql_update($from, $row, $condition){
    $sql = "UPDATE $from SET ";
    
    $assigns = array();
    foreach($row as $key => $val){
        $key = sql_tick(sql_escape($key));
        $val = sql_quote(sql_escape($val));
        $assigns[] = "$key = $val";
    }
    
    $sql .= implode(", ", $assigns);
    $sql .= $condition;
}

function sql_select(){
}

function sql_tick($str){
    return "`$str`";
}

function sql_quote($str){
    return "'$str'";
}

/*END OF FILE*/

So I reverted to writing things from scratch. I should be posting all the code somewhere as I've been intending to make it out into a community effort.

Offline

 

#8 29-07-2010 06:58:17 pm

Kassem
Member
From: Baabda
Registered: 02-03-2010
Website

Re: Nice little ActiveRecord Framework

Nice idea arithma, I will try to help in case you actually make it a community effort. For a start, replace your MySQL specific code with PDO code.

Offline

 

#9 30-07-2010 12:23:54 am

rolf
Member
From: Beirut
Registered: 05-04-2005

Re: Nice little ActiveRecord Framework

rahmu wrote:

No it doesn't work that way.

You need a way to send data to your server so it'd be saved in a database right? The way Apache works is that it expects requests in HTTP, like GET, POST, HEAD, PUT, TRACE, ...

At one point or another, if you are sending the data to Apache (who will then send it to the DB) you have to send a POST request from your browser. As you probably know, POST could be sent from HTML form, or AJAX.

"Magical binding" cannot happen. Moreover, for security reasons, a POST request must be treated server-side before being executed. Your idea of the magic binding (if it existed) would be a great security threat. (You are basically allowing any user to input whatever he wants into your db :-/ )

I think the only real reason why this wouldnt be possible is mentioned after "Moreover". Yep, validation...
You can escape data for MySQL, but if you validate, you'll need a way to send feedback to the user if the validation fails, and I'm still scratching my head over this one. The best I can come up with is sending a success/failure signal back to the javascript to handle.

(BTW what is the TRACE HTTP header?)


mpt.gov.lb : Sucking the fun out of Internet since 1990 !

Offline

 

#10 30-07-2010 08:19:06 am

Kassem
Member
From: Baabda
Registered: 02-03-2010
Website

Re: Nice little ActiveRecord Framework

rolf wrote:

you'll need a way to send feedback to the user if the validation fails, and I'm still scratching my head over this one. The best I can come up with is sending a success/failure signal back to the javascript to handle.

Honestly, I still do not get your point rolf. You just want to send a signal back to the client? Why not use AJAX or a WebSocket?

Offline

 

#11 30-07-2010 08:29:44 am

xterm
Wiki Moderator
Registered: 18-02-2009

Re: Nice little ActiveRecord Framework

You're presuming forms will always be a 1 to 1 mapping with the model in the database which is almost never the case.

Last edited by xterm (30-07-2010 12:01:43 pm)


λ

Online

 

#12 01-08-2010 11:37:57 am

arithma
Member
From: Saida, Lebanon
Registered: 09-12-2009
Website

Re: Nice little ActiveRecord Framework

You can perhaps reuse phpMyAdmin's code for dealing with tables, or modify it to more suite the needs of the application (and the added references between tables..) This could be an almost ready backend for all your admin needs (almost).

Offline

 

#13 01-08-2010 02:02:02 pm

rolf
Member
From: Beirut
Registered: 05-04-2005

Re: Nice little ActiveRecord Framework

Kassem wrote:

rolf wrote:

you'll need a way to send feedback to the user if the validation fails, and I'm still scratching my head over this one. The best I can come up with is sending a success/failure signal back to the javascript to handle.

Honestly, I still do not get your point rolf. You just want to send a signal back to the client? Why not use AJAX or a WebSocket?

Yes, I think Ajax would be good. Something like XMPP would be great (because of the ability to "push"). But I'm more interested in the "1 to 1 mapping" (like xterm calls it), I don't really care about how it's done (ajax, posts or whatever).

xterm wrote:

You're presuming forms will always be a 1 to 1 mapping with the model in the database which is almost never the case.

I'm not presuming, but I think most of the time the mapping would be pretty simple, maybe related tables, not much more. Also there might be "conversion" to be made (that's the most annoying thing actually) but nothing that can't be handled by a proper callback system. No? At least that's what I'm thinking.

I know it's not a one to one thing, but it's not rocket science either, that's why I think it is worth trying.

arithma wrote:

You can perhaps reuse phpMyAdmin's code for dealing with tables, or modify it to more suite the needs of the application (and the added references between tables..) This could be an almost ready backend for all your admin needs (almost).

Thanks about the tip. I've thought about that, and I've tried a simpler equivalent of phpMyAdmin (because of the complexity of PMA), but it didnt satisfy me because I found the structure too rigid for my taste, even though it could be themed.

Last edited by rolf (01-08-2010 02:05:13 pm)


mpt.gov.lb : Sucking the fun out of Internet since 1990 !

Offline

 

Board footer

(CC) 2004-2010 LebGeeks.com
Part of the LebGeeks Network.