@@ -23,7 +23,7 @@ composer require initphp/database
2323or include the ` src/init.php ` file from this repo in your system.
2424
2525``` php
26- require_once "src/init .php";
26+ require_once "src/Init .php";
2727```
2828
2929## Usage
@@ -32,33 +32,30 @@ require_once "src/init.php";
3232
3333``` php
3434require_once "vendor/autoload.php";
35- use \InitPHP\Database\DB;
35+ use \InitPHP\Database\Facade\ DB;
3636
3737// Connection
38- $db = new DB ([
38+ DB::createImmutable ([
3939 'dsn' => 'mysql:host=localhost;port=3306;dbname=test;charset=utf8mb4',
4040 'username' => 'root',
4141 'password' => '',
4242 'charset' => 'utf8mb4',
4343 'collation' => 'utf8mb4_general_ci',
4444]);
45-
46- // If you are working with a single database, do not forget to make your connection global.
47- $db->connectionAsGlobal();
4845```
4946
5047#### Create
5148
5249Single Row :
5350
5451``` php
52+ use \InitPHP\Database\Facade\DB;
5553$data = [
5654 'title' => 'Post Title',
5755 'content' => 'Post Content',
5856];
5957
60- /** @var $db \InitPHP\Database\DB */
61- $isInsert = $db->table('post')
58+ $isInsert = DB::table('post')
6259 ->create($data);
6360
6461/**
@@ -77,6 +74,8 @@ if($isInsert){
7774Multi Row:
7875
7976``` php
77+ use \InitPHP\Database\Facade\DB;
78+
8079$data = [
8180 [
8281 'title' => 'Post Title 1',
@@ -89,8 +88,7 @@ $data = [
8988 ],
9089];
9190
92- /** @var $db \InitPHP\Database\DB */
93- $isInsert = $db->table('post')
91+ $isInsert = DB::table('post')
9492 ->create($data);
9593
9694/**
@@ -111,11 +109,12 @@ if($isInsert){
111109#### Read
112110
113111``` php
114- /** @var $db \InitPHP\Database\DB */
115- $db->select('user.name as author_name', 'post.id', 'post.title')
112+ use \InitPHP\Database\Facade\DB;
113+
114+ DB::select('user.name as author_name', 'post.id', 'post.title')
116115 ->from('post')
117116 ->selfJoin('user', 'user.id=post.author')
118- ->where('post.status', true )
117+ ->where('post.status', 1 )
119118 ->orderBy('post.id', 'ASC')
120119 ->orderBy('post.created_at', 'DESC')
121120 ->offset(20)->limit(10);
@@ -129,9 +128,10 @@ $db->select('user.name as author_name', 'post.id', 'post.title')
129128* ORDER BY post ASC, post.created_at DESC
130129* LIMIT 20, 10
131130*/
132- $res = $db->read();
133- if($db->numRows() > 0){
134- foreach ($res as $row) {
131+ $res = DB::read();
132+
133+ if($res->numRows() > 0){
134+ foreach ($res->toAssoc() as $row) {
135135 echo $row['title'] . ' by ' . $row['author_name'] . '<br />';
136136 }
137137}
@@ -140,13 +140,13 @@ if($db->numRows() > 0){
140140#### Update
141141
142142``` php
143+ use \InitPHP\Database\Facade\DB;
143144$data = [
144145 'title' => 'New Title',
145146 'content' => 'New Content',
146147];
147148
148- /** @var $db \InitPHP\Database\DB */
149- $isUpdate = $db->from('post')
149+ $isUpdate = DB::from('post')
150150 ->where('id', 13)
151151 ->update($data);
152152
@@ -165,8 +165,9 @@ if ($isUpdate) {
165165#### Delete
166166
167167``` php
168- /** @var $db \InitPHP\Database\DB */
169- $isDelete = $db->from('post')
168+ use \InitPHP\Database\Facade\DB;
169+
170+ $isDelete = DB::from('post')
170171 ->where('id', 13)
171172 ->delete();
172173
@@ -180,6 +181,36 @@ if ($isUpdate) {
180181}
181182```
182183
184+ ### RAW
185+
186+ ``` php
187+ use \InitPHP\Database\Facade\DB;
188+
189+ $res = DB::query("SELECT id FROM post WHERE user_id = :id", [
190+ ':id' => 5
191+ ]);
192+ ```
193+
194+ #### Builder for RAW
195+
196+ ``` php
197+ use \InitPHP\Database\Facade\DB;
198+
199+ $res = DB::select(DB::raw("CONCAT(name, ' ', surname) AS fullname"))
200+ ->where(DB::raw("title = '' AND (status = 1 OR status = 0)"))
201+ ->limit(5)
202+ ->get('users');
203+ /**
204+ * SELECT CONCAT(name, ' ', surname) AS fullname
205+ * FROM users
206+ * WHERE title = '' AND (status = 1 OR status = 0)
207+ * LIMIT 5
208+ */
209+ foreach ($res->toAssoc() as $row) {
210+ echo $row['fullname'];
211+ }
212+ ```
213+
183214### Model and Entity
184215
185216Model and Entity; are two common concepts used in database abstraction. To explain these two concepts in the roughest way;
0 commit comments