You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
QueryWritter is a PHP library that parses SQL queries, transforms those into an object representation, stores them in a
5
-
dependency injection container, and returns them as string. It is a [Mouf plugin](http://mouf-php.com).
4
+
Magic-query is a PHP library that helps you work with complex queries that require
5
+
a variable number of parameters.
6
6
7
-
Ok, but why would I use QueryWritter?
8
-
-------------------------------------
7
+
Why should I care?
8
+
------------------
9
9
10
-
Because it is **the most effecient way to deal with queries that can have a variable number of parameters**!
10
+
Because it is **the most efficient way to deal with queries that can have a variable number of parameters**!
11
11
Think about a typical datagrid with a bunch of filter (for instance a list of products filtered by name, company, price, ...).
12
12
If you have the very common idea to generate the SQL query using no PHP library, your code will look like this:
13
13
@@ -23,50 +23,53 @@ if (isset($params['name'])) {
23
23
if (isset($params['company'])) {
24
24
$sql .= "AND c.name LIKE '".addslashes($params['company'])."%'";
25
25
}
26
+
if (isset($params['country'])) {
27
+
$sql .= "AND c.country LIKE '".addslashes($params['country'])."%'";
28
+
}
26
29
// And so on... for each parameter, we have a "if" statement
27
30
```
28
31
29
-
30
-
31
-
Concatenating SQL queries is dangerous (especially if you forget to protect parameters).
32
+
Concatenating SQL queries is **dangerous** (especially if you forget to protect parameters).
32
33
You can always use parameterized SQL queries, but you will still have to concatenate the filters.
33
34
34
35
To avoid concatenating strings, frameworks and libraries have used different strategies. Building a full ORM (like
35
36
Doctrine or Propel) is a good idea, but it makes writing complex queries even more complex. Other frameworks like
36
37
Zend are building queries using function calls. These are valid strategies, but you are no more typing SQL queries
37
38
directly, and let's face it, it is always useful to use a query directly.
38
39
39
-
This is where QueryWritter becomes helpful.
40
+
This is where Magic-query becomes helpful.
40
41
41
42
How does it work?
42
43
-----------------
43
-
// TODO: schema... or even better... video!
44
-
45
-
###1- Write your query
46
-
You start by writing your query, **in plain SQL**. No ORM, no special query language (DQL or HQL anyone?), just plain and simple SQL.
47
-
This is cool because everybody knows SQL. In your query, you put absolutely all the parameters you can imagine.
48
-
49
-
###2- Store your query in Mouf
50
-
In Mouf UI, go to **DB** > **SQL queries** > **Create SQL query**.
51
-
Here, you can **copy and paste your query**. Since this is Mouf, every query is an "instance", and you have to pick
52
-
a name for your query.
53
44
54
-
Behind the scenes, QueryWritter will parse your query and make sure every piece of the query (each table, each column, each filter...) is transformed
55
-
into an object. But you really don't have to care about that right now.
45
+
Easy! You write the query with all possible parameters.
56
46
57
-
###3- Test your query
58
-
Right from Mouf UI, you can test your query! And lo and behold! Because the query was parsed, **QueryWritter will dynamically
59
-
add parts of the query depending on the parameters you decide to use**.
60
-
61
-
###4- Use it in your code
62
-
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 <code>toSql</code> method, passing
0 commit comments