Skip to content

Commit 503c5e8

Browse files
committed
Merge branch 'twig' of github.com:moufmouf/magic-query into twig
2 parents a1506ee + 9ac6e67 commit 503c5e8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,34 @@ into the SQL query. You should use classic SQL parameters for this. This means t
135135

136136
Want to know more? <a class="btn btn-primary" href="doc/magic_twig.md">Check out the MagicTwig guide!</a>
137137

138+
<a name="twig"></a>
139+
Use Twig templating in your SQL queries!
140+
----------------------------------------
141+
142+
Discarding unused parameters and auto-joining keys is not enough? You have very specific needs? Say hello to
143+
Twig integration!
144+
145+
Using Twig integration, you can directly add Twig conditions right into your SQL.
146+
147+
```php
148+
use Mouf\Database\MagicQuery;
149+
150+
$sql = "SELECT users.* FROM users {% if isAdmin %} WHERE users.admin = 1 {% endif %}";
151+
152+
$magicQuery = new MagicQuery();
153+
// By default, Twig integration is disabled. You need to enable it.
154+
$magicQuery->setEnableTwig(true);
155+
156+
$completeSql = $magicQuery->build($sql, ['isAdmin' => true]);
157+
// Parameters are passed to the Twig SQL query, and the SQL query is returned.
158+
```
159+
160+
<div class="alert alert-info"><strong>Heads up!</strong> The Twig integration cannot be used to insert parameters
161+
into the SQL query. You should use classic SQL parameters for this. This means that instead if writing
162+
<code>{{ id }}</code>, you should write <code>:id</code>.</div>
163+
164+
Want to know more? <a class="btn btn-primary" href="doc/magic_twig.md">Check out the MagicTwig guide!</a>
165+
138166
Is it a MySQL only tool?
139167
------------------------
140168

0 commit comments

Comments
 (0)