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
$sql = "SELECT * FROM products p JOIN companies c ON p.company_id = c.id WHERE
86
-
(p.name LIKE :name OR p.altname LIKE :name)
87
-
AND c.name LIKE :company
88
-
AND c.country LIKE :country";
91
+
And the code is so simple!
89
92
90
-
$magicQuery = new MagicQuery();
91
-
$sql = $magicQuery->build($sql, $params);
92
-
```
93
+
```php
94
+
use Mouf\Database\MagicQuery;
93
95
94
-
###Other alternatives
96
+
$sql = "SELECT users.* FROM MAGICJOIN(users) WHERE groups.name = 'Admins' AND country.name='France'";
95
97
96
-
To avoid concatenating strings, frameworks and libraries have used different strategies. Using a full ORM (like
97
-
Doctrine or Propel) is a good idea, but it makes writing complex queries even more complex. Other frameworks like
98
-
Zend are building queries using function calls. These are valid strategies, but you are no more typing SQL queries
99
-
directly, and let's face it, it is always useful to use a query directly.
98
+
// Get a MagicQuery object.
99
+
// $conn is a Doctrine DBAL connection.
100
+
$magicQuery = new MagicQuery($conn);
100
101
101
-
How does it work under the hood?
102
-
--------------------------------
102
+
$completeSql = $magicQuery->build($sql);
103
+
// $completeSql contains the complete SQL request, with all joins.
104
+
```
103
105
104
-
A lot happens to your SQL query. It is actually parsed (thanks to a modified
105
-
version of the php-sql-parser library) and then changed into a tree.
106
-
The magic happens on the tree where the node containing unused parameters
107
-
are simply discarded. When it's done, the tree is changed back to SQL and
108
-
"shazam!", your SQL query is purged of useless parameters!
106
+
Want to know more? <aclass="btn btn-primary"href="doc/magic_join.md">Check out the MagicJoin guide!</a>
109
107
110
108
Is it a MySQL only tool?
111
109
------------------------
112
110
113
111
No. By default, your SQL is parsed and then rewritten using the MySQL dialect, but you use any kind of dialect
114
-
known by Doctrine DBAL. Magic-query optionally uses Doctrine DBAL. You can pass a `Connection` object
112
+
known by [Doctrine DBAL](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/). Magic-query optionally uses Doctrine DBAL. You can pass a `Connection` object
115
113
as the first parameter of the `MagicQuery` constructor. Magic-query will then use the matching dialect.
Copy file name to clipboardExpand all lines: composer.json
+7-4Lines changed: 7 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
{
2
2
"name": "mouf/magic-query",
3
-
"description": "A very clever library to generate PHP prepared statement with a variable number of parameters... and much more!",
3
+
"description": "A very clever library to help you with SQL: generate prepared statements with a variable number of parameters, automatically writes joins... and much more!",
0 commit comments