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
Copy file name to clipboardExpand all lines: src/docs/database/index.md
+23-4Lines changed: 23 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,9 @@ Databases are essential for most applications, as they help you store and retrie
51
51
52
52
## Leaf MVC + DB
53
53
54
-
Leaf MVC comes with built-in support for models which are a way to programmatically represent resources in your database using PHP classes. For that reason, you have no real need for this module unless you want to use Leaf Auth. If you choose to use Leaf DB in your MVC application, we have already set up everything for you. All you need to do is to head over to your `.env` file and set up your database connection details. Here are a few example connections:
54
+
Leaf MVC comes with built-in support for models which are a way to programmatically represent resources in your database using PHP classes. For that reason, you have no real need to use the `db()` function unless you want to quickly run a query without creating a model. Still, everything has been set up for you and Leaf DB will use the default database connection details in your `.env` file.
Remember to head over to `public/index.php` and uncomment the line that says `\Leaf\Database::initDb();`. This will automatically connect to your database using the details in your environment file.
84
-
85
-
You can safely skip the "Connecting to a database" section.
85
+
You can skip the DB connection section: Leaf MVC sets up a deferred connection for you. This means that the connection will only be made when you run a query.
In a lot of cases, your application may have other routes that don't need a database connection, but popping up a connection before the route is hit can be a waste of resources. Leaf DB now allows you to defer your database connection until you actually need it. Here's how you can defer your database connection:
141
+
142
+
```php
143
+
db()->load([
144
+
'dbtype' => '...',
145
+
'charset' => '...',
146
+
'port' => '...',
147
+
'unixSocket' => '...',
148
+
'host' => '...',
149
+
'username' => '...',
150
+
'password' => '...',
151
+
'dbname' => '...',
152
+
]);
153
+
```
154
+
155
+
It takes in the same arguments as `connect()`, but it doesn't connect to the database immediately. It only connects when you run a query.
156
+
138
157
## Writing simple queries
139
158
140
159
Once you've connected to a database, you can start writing queries to interact with it. Queries are the commands you run on your database to get, insert, update or delete data. Leaf DB provides a simple way to run queries using the query builder, but also allows you to run raw SQL queries.
0 commit comments