There are several ActiveRecord styled query builder libraries out there. Some are stand alone and some come built into frameworks. However, they really have trouble with WHERE a
You may consider SQLBuilder, written in PHP and it can generate cross platform SQL for MySQL and PostgreSQL by setting different query driver.
The use case is here: https://github.com/c9s/SQLBuilder/blob/2.0/tests/SQLBuilder/Query/SelectQueryTest.php