PHPackages                             mouf/database.querywriter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mouf/database.querywriter

ActiveMouf-library[Database &amp; ORM](/categories/database)

mouf/database.querywriter
=========================

This package contains classes useful to generate SQL statements such as SELECT queries, etc...

v4.1.0(7y ago)030.8k32MITPHPPHP &gt;=5.3.0

Since May 23Pushed 7y ago7 watchersCompare

[ Source](https://github.com/thecodingmachine/database.querywriter)[ Packagist](https://packagist.org/packages/mouf/database.querywriter)[ Docs](https://github.com/thecodingmachine/database.querywriter)[ RSS](/packages/mouf-databasequerywriter/feed)WikiDiscussions 4.0 Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (5)Used By (2)

What is QueryWriter?
====================

[](#what-is-querywriter)

QueryWritter is a PHP library that parses SQL queries, transforms those into an object representation, stores them in a dependency injection container, and returns them as string. It is a [Mouf plugin](http://mouf-php.com).

It is heavily based on **mouf/magic-query**. Actually, magic-query's code was extracted from query-writer.

Ok, but why would I use QueryWritter?
-------------------------------------

[](#ok-but-why-would-i-use-querywritter)

Because it is **the most effecient way to deal with queries that can have a variable number of parameters**! Think about a typical datagrid with a bunch of filter (for instance a list of products filtered by name, company, price, ...). If you have the very common idea to generate the SQL query using no PHP library, your code will look like this:

**You should not do this!**

```
// People usually write queries like this:
$sql = "SELECT * FROM products p JOIN companies c ON p.company_id = c.id WHERE 1=1 ";
// They keep testing for parameters, and concatenating strings....
if (isset($params['name'])) {
	$sql .= "AND p.name LIKE '".addslashes($params['name'])."%'";
}
if (isset($params['company'])) {
	$sql .= "AND c.name LIKE '".addslashes($params['company'])."%'";
}
// And so on... for each parameter, we have a "if" statement
```

Concatenating SQL queries is dangerous (especially if you forget to protect parameters). You can always use parameterized SQL queries, but you will still have to concatenate the filters.

To avoid concatenating strings, frameworks and libraries have used different strategies. Building a full ORM (like Doctrine or Propel) is a good idea, but it makes writing complex queries even more complex. Other frameworks like Zend are building queries using function calls. These are valid strategies, but you are no more typing SQL queries directly, and let's face it, it is always useful to use a query directly.

This is where QueryWritter becomes helpful.

How does it work?
-----------------

[](#how-does-it-work)

// TODO: schema... or even better... video!

\###1- Write your query You start by writing your query, **in plain SQL**. No ORM, no special query language (DQL or HQL anyone?), just plain and simple SQL. This is cool because everybody knows SQL. In your query, you put absolutely all the parameters you can imagine.

\###2- Store your query in Mouf In Mouf UI, go to **DB** &gt; **SQL queries** &gt; **Create SQL query**. Here, you can **copy and paste your query**. Since this is Mouf, every query is an "instance", and you have to pick a name for your query.

Behind the scenes, QueryWritter will parse your query and make sure every piece of the query (each table, each column, each filter...) is transformed into an object. But you really don't have to care about that right now.

\###3- Test your query Right from Mouf UI, you can test your query! And lo and behold! Because the query was parsed, **QueryWritter will dynamically add parts of the query depending on the parameters you decide to use**.

\###4- Use it in your code If you are not a Mouf user (if you are using Drupal, Symfony, Zend Framework...), you can directly use the query by fetching the instance from Mouf and calling the `toSql` method, passing parameters in... parameter :)

```
$mySelect = Mouf::getMySelectStatement();
$sql = $mySelect->toSql(array("status"=>1, "search"=>"hello"));

```

If you are a Mouf user, you can even directly run the query using the **QueryResult** class that executes the query directly. Or even better, use the **Evolugrid** module, and display your query result in an HTML datagrid, directly!

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 89.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~494 days

Total

5

Last Release

2764d ago

Major Versions

2.0.x-dev → 3.0.x-dev2015-05-11

3.0.x-dev → v4.0.02016-02-15

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1104771?v=4)[mouf](/maintainers/mouf)[@Mouf](https://github.com/Mouf)

---

Top Contributors

[![moufmouf](https://avatars.githubusercontent.com/u/1290952?v=4)](https://github.com/moufmouf "moufmouf (35 commits)")[![xhuberty](https://avatars.githubusercontent.com/u/8350192?v=4)](https://github.com/xhuberty "xhuberty (2 commits)")[![HugoAverty](https://avatars.githubusercontent.com/u/1681508?v=4)](https://github.com/HugoAverty "HugoAverty (1 commits)")[![nguyenk](https://avatars.githubusercontent.com/u/2227554?v=4)](https://github.com/nguyenk "nguyenk (1 commits)")

---

Tags

databasequerymouf

### Embed Badge

![Health badge](/badges/mouf-databasequerywriter/health.svg)

```
[![Health](https://phpackages.com/badges/mouf-databasequerywriter/health.svg)](https://phpackages.com/packages/mouf-databasequerywriter)
```

###  Alternatives

[aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can be used with any database connection library.

4572.9M34](/packages/aura-sqlquery)[envms/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

925511.7k13](/packages/envms-fluentpdo)[lichtner/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

921274.8k6](/packages/lichtner-fluentpdo)[bvanhoekelen/performance

PHP performance tool analyser your script on time, memory usage and db query. Support Laravel and Composer for web, web console and command line interfaces.

521774.3k4](/packages/bvanhoekelen-performance)[fpdo/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

921244.9k7](/packages/fpdo-fluentpdo)[tpetry/laravel-query-expressions

Database-independent Query Expressions as a replacement to DB::raw calls

357436.5k2](/packages/tpetry-laravel-query-expressions)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
