Getting Started with Medoo - Examples of Use

Originally published at: http://www.sitepoint.com/getting-started-medoo-examples-use/

In this article I’m going to walk you through Medoo, a lightweight database abstraction library for PHP. Its main features include:



  • Support for multiple databases – it supports MySQL, MariaDB, Sybase, MS SQL, PostgreSQL, and Oracle.

  • Secure – prevents SQL injection, it uses PDO.

  • Easy to use – its API is very intuitive.


While Medoo is nothing revolutionary, and the fact that it sports a very small filesize matters little to few, it’s still an interesting project that went from being outright dismissed to vaguely accepted, as evident in these threads. It’s on its way up, and that’s our reason for taking a look at it.


Installation


Even though the website recommends installing it by downloading a file and including it in your project, you should use Composer to do so.

Continue reading this article on SitePoint

I read the article, if that's all there is to this library it's a complete waste of time. It offers no advantages over straight SQL if you know what you're doing. You end up spending time learning their syntax instead of learning PDO + SQL's syntax to do the same thing. For example how is this?

$db->update(    
  'trainers',     
  array('pokemon_count[+]' => 3),     
  array('id' => $trainer_id)
);

Any better than this?

$pdo->prepare( 'UPDATE trainers SET pokemon_count = pokemon_count + ?, id = ?')->execute([3,$trainer_id]);

I've written a better PDO wrapper than this and actually posted it on the old forum.

Agree 100%

Why bother abstracting away SQL?

I do like a little sugar like:

$db->write('
    UPDATE trainers
    SET pokemon_count = pokemon_count + :amount
    WHERE id = :id;
', [
    ':id' => $this->getID(),
    ':amount' => 69,
]);

Well, either I use a very sophisticated DBAL, like Propel, Doctrine, or I go directly with Native SQL.

A Lisp-like grammar "AND(a, b)" is NOT intuitive at all. It may be easier for the wrapper to parse, but it is against all human reading and SQL grammar presentation.

This looks a lot like Zend Framework 1.x's ZendDb layer.
If your query is built using complex logic, abstracting the query into an object is pretty helpful.

I probably won't be using this.
This is too complex for the simple case and not that different from Zend Db.

Serious missing feature - get generated sql without actually running it.
last_query() is good, getSql() is better.

Michael, would you mind sharing the latest version of your PDO wrapper?