1

I often make small projects for friends or for people on forum I visit. For instance, it's create a user space, or chat page, or stuff like that.

That's why I think Frameworks/ORM is not suitable for the size of these projects, the code is often less than 300 lines.

But in all cases, I use PDO, and it's really using to write the SQL string, check the docs to make a INSERT, .. So I was thinking of extending the PDO Class with a chaining approach, like this:

$pdo->create('users', array(
    'id' => 'int',
    'username' => 'text',
    'password' => 'text'
));

$pdo->insert(array(
    'username' => $user,
    'password' => md5($password)
))->in('users');

$pdo->update(array(
    'username' => $new_user
))->in('users')->where('id', $user_id);

$pdo->select()->from('users')->where('id', $user_id)->row();

$pdo->select('username')->from('users')->rows();

$pdo->drop('users');

$pdo->close();

And if PDO or the way of storing (mysql/sqlite/...) changes, I just have to edit the class and it's done.

So, does a similar class exist ? And is it a good idea to make things like this ?

EDIT: I'm sorry to bump this thread, but I'm afraid that no-one has made a thing like this..

MTC
  • 21
  • 1
  • 3
  • 3
    First, consider the frameworks anyways. Small projects have a habit of not staying small over time. Laravel's lovely database ORM can be used without the overall Laravel framework and does this sort of chaining. http://stackoverflow.com/questions/16633893/using-eloquent-orm-without-laravel – ceejayoz Jan 28 '15 at 16:00
  • 3
    Offtopic but the security of the MD5 hash function is severely compromised I would use another hash. – Daan Jan 28 '15 at 16:00
  • 1
    @ceejayoz: but the PHP scripts are often hosted on free host, which are very slow and limited, so forget about frameworks.. @Daan: I use `hash('sha256', $data);` but the code above was just for example – MTC Jan 28 '15 at 16:05
  • 2
    Loading in a framework like Laravel to solve a simple problem is the right way to do it. Thinking that it's "not suitable for the size of these projects" is completely backwards: The smaller the project, the more help you'll get from a framework. It's enormously complicated applications that often exceed the capabilities of a framework, not simple things. Laravel also has a built-in [authentication system](http://laravel.com/docs/4.2/security) meaning your 300 lines of code turns out to be maybe 50. People have done a thing like this, it's called a *framework*. – tadman Feb 05 '15 at 20:23
  • @daan If someone's talking about writing an authentication system, MD5 is very much on-topic. Use [`password_hash`](http://php.net/manual/en/function.password-hash.php) at the very least. There is no excuse for using MD5. – tadman Feb 05 '15 at 20:26
  • 2
    I agree with the rest here, you *should* use a framework. Also, these days we have `composer` so you can just use parts of frameworks - such as laravel's Eloquent or Doctrine, or Propel. Also, if your friends host their stuff on free hosts, PHP is probably the last thing that will be the bottleneck. MySQL or any other persistent storage is the first thing that affects the performance, and that's because of hardware mostly (to be precise - because of HDD). So, if you believe that you spending hours instead of minutes will bring performance - it won't. – N.B. Feb 05 '15 at 22:55
  • @N.B.: To be fair something like an ORM (as opposed to just DBAL) can potentially be resource intensive if there are a lot of entities and depending on which pattern it takes (ie. Mapper, ActiveRecord, Gateway, etc.) and the nature of the app (this is aside from the actual DB side of it). That said i generally agree with you though I dont know that i would use something like Laravel for what hes talking about... id probably stick with a micro like Slim or Silex... that said i generally agree with what you're saying. – prodigitalson Feb 05 '15 at 23:00
  • @prodigitalson - for example, I use Eloquent on a site and it allowed me to be extremely quick with coding and not worrying about db access. It's not hugely complex project, but using ORM helped me to do stuff fast and not worry about preparing statements, caching them etc. There are 0 problems with it. At one point I rewrote a few models to use custom-coded PDO statements - I quite literally got NO performance out of it. That's why I don't really think it's worth spending time. However, one can only measure and believe in his judgement when it comes to coding such things. I opt for ORMs :) – N.B. Feb 05 '15 at 23:05
  • Well its not about that part of the performance gain, its about object being expensive in terms of memory. I mean there is somewhat of a perf gain when you get in to bigger things depending on the ORM internals but that wasnt what i was talking about. I have many a war story from Propel 1.2, Doctrine 1, and Doctrine 2 on both aspects, but those were admittedly pretty large projects which isnt what we are talking about. – prodigitalson Feb 05 '15 at 23:19

1 Answers1

2

You are contradicting yourself... You are talking about writing a QueryBuilder and/or DBAL on top of PDO (which is a pretty complex thing to do in terms of the amount of code needed to support multiple DB vendors) Yet you shy away from a framework. Doesn't make much sense.

That said there are DBAL and QueryBuilders out there that can be used outside of a greater framework. Personally I'd say go with Doctrine DBAL. An example of using its QueryBuilder:

$conn = DriverManager::getConnection($connectionInfo);
$queryBuilder = $conn->createQueryBuilder();

$queryBuilder
    ->select('*')
    ->from('users')
    ->where('id = ?')
    ->setParameter(0, $user_id);

As for a framework there are also "micro frameworks" which serve as a simple MVC stack (when compared to something like Laravel, Cake, Symfony2, etc.). For projects of the nature you are talking I would in fact probably use Silex in combination with Doctrine DBAL.

prodigitalson
  • 60,050
  • 10
  • 100
  • 114