@@ -6,7 +6,7 @@ Manage your database with or without abstraction. This library is built on the P
66
77## Requirements
88
9- - PHP 7.4 and later.
9+ - PHP 8.0 and later.
1010- PHP PDO extension.
1111
1212## Supported Databases
@@ -20,15 +20,9 @@ Databases supported by PDO and suitable drivers are available at [https://www.ph
2020composer require initphp/database
2121```
2222
23- or include the ` src/init.php ` file from this repo in your system.
24-
25- ``` php
26- require_once "src/Init.php";
27- ```
28-
2923## Usage
3024
31- ### QueryBuilder and CRUD
25+ ### QueryBuilder & DBAL and CRUD
3226
3327``` php
3428require_once "vendor/autoload.php";
@@ -53,8 +47,7 @@ $data = [
5347 'content' => 'Post Content',
5448];
5549
56- $isInsert = DB::table('post')
57- ->create($data);
50+ $isInsert = DB::create('post', $data);
5851
5952/**
6053* This executes the following query.
@@ -67,8 +60,7 @@ $isInsert = DB::table('post')
6760if($isInsert){
6861 // Success
6962} else {
70- $errors = DB::getError();
71- foreach ($errors as $errMsg) {
63+ foreach (DB::getErrors() as $errMsg) {
7264 echo $errMsg;
7365 }
7466}
@@ -91,8 +83,7 @@ $data = [
9183 ],
9284];
9385
94- $isInsert = DB::table('post')
95- ->createBatch($data);
86+ $isInsert = DB::createBatch('post', $data);
9687
9788/**
9889* This executes the following query.
@@ -107,8 +98,7 @@ $isInsert = DB::table('post')
10798if($isInsert){
10899 // Success
109100} else {
110- $errors = DB::getError();
111- foreach ($errors as $errMsg) {
101+ foreach (DB::getErrors() as $errMsg) {
112102 echo $errMsg;
113103 }
114104}
@@ -119,14 +109,7 @@ if($isInsert){
119109``` php
120110use \InitPHP\Database\Facade\DB;
121111
122- DB::select('user.name as author_name', 'post.id', 'post.title')
123- ->from('post')
124- ->selfJoin('user', 'user.id=post.author')
125- ->where('post.status', 1)
126- ->orderBy('post.id', 'ASC')
127- ->orderBy('post.created_at', 'DESC')
128- ->offset(20)->limit(10);
129-
112+
130113/**
131114* This executes the following query.
132115*
@@ -136,11 +119,19 @@ DB::select('user.name as author_name', 'post.id', 'post.title')
136119* ORDER BY post ASC, post.created_at DESC
137120* LIMIT 20, 10
138121*/
139- $res = DB::read();
140122
123+ $res = DB::select('user.name as author_name', 'post.id', 'post.title')
124+ ->from('post')
125+ ->selfJoin('user', 'user.id=post.author')
126+ ->where('post.status', 1)
127+ ->orderBy('post.id', 'ASC')
128+ ->orderBy('post.created_at', 'DESC')
129+ ->offset(20)->limit(10)
130+ ->read('post');
131+
141132if($res->numRows() > 0){
142133 $results = $res->asAssoc()
143- ->results ();
134+ ->rows ();
144135 foreach ($results as $row) {
145136 echo $row['title'] . ' by ' . $row['author_name'] . '<br />';
146137 }
@@ -156,9 +147,8 @@ $data = [
156147 'content' => 'New Content',
157148];
158149
159- $isUpdate = DB::from('post')
160- ->where('id', 13)
161- ->update($data);
150+ $isUpdate = DB::where('id', 13)
151+ ->update('post', $data);
162152
163153/**
164154* This executes the following query.
@@ -170,8 +160,7 @@ $isUpdate = DB::from('post')
170160if ($isUpdate) {
171161 // Success
172162} else {
173- $errors = DB::getError();
174- foreach ($errors as $errMsg) {
163+ foreach (DB::getErrors() as $errMsg) {
175164 echo $errMsg;
176165 }
177166}
@@ -193,9 +182,8 @@ $data = [
193182 ]
194183];
195184
196- $isUpdate = DB::from('post')
197- ->where('status', 1)
198- ->updateBatch($data, 'id');
185+ $isUpdate = DB::where('status', '!=', 0)
186+ ->updateBatch('post', $data, 'id');
199187
200188/**
201189* This executes the following query.
@@ -208,13 +196,12 @@ $isUpdate = DB::from('post')
208196* content = CASE
209197* WHEN id = 5 THEN 'New Content #5'
210198* ELSE content END
211- * WHERE status = 1 AND id IN (5, 10)
199+ * WHERE status != 0 AND id IN (5, 10)
212200*/
213201if ($isUpdate) {
214202 // Success
215203} else {
216- $errors = DB::getError();
217- foreach ($errors as $errMsg) {
204+ foreach (DB::getErrors() as $errMsg) {
218205 echo $errMsg;
219206 }
220207}
@@ -225,9 +212,8 @@ if ($isUpdate) {
225212``` php
226213use \InitPHP\Database\Facade\DB;
227214
228- $isDelete = DB::from('post')
229- ->where('id', 13)
230- ->delete();
215+ $isDelete = DB::where('id', 13)
216+ ->delete('post');
231217
232218/**
233219* This executes the following query.
@@ -237,8 +223,7 @@ $isDelete = DB::from('post')
237223if ($isUpdate) {
238224 // Success
239225} else {
240- $errors = DB::getError();
241- foreach ($errors as $errMsg) {
226+ foreach (DB::getErrors() as $errMsg) {
242227 echo $errMsg;
243228 }
244229}
@@ -263,13 +248,15 @@ $res = DB::select(DB::raw("CONCAT(name, ' ', surname) AS fullname"))
263248 ->where(DB::raw("title = '' AND (status = 1 OR status = 0)"))
264249 ->limit(5)
265250 ->get('users');
251+
266252/**
267253 * SELECT CONCAT(name, ' ', surname) AS fullname
268254 * FROM users
269255 * WHERE title = '' AND (status = 1 OR status = 0)
270256 * LIMIT 5
271257 */
272- $results = $res->asAssoc()->results();
258+ $results = $res->asAssoc()
259+ ->rows();
273260foreach ($results as $row) {
274261 echo $row['fullname'];
275262}
@@ -308,10 +295,22 @@ namespace App\Model;
308295class Posts extends \InitPHP\Database\Model
309296{
310297
298+ /**
299+ * If your model will use a connection other than your global connection, provide connection information.
300+ * @var array|null <p >Default : NULL</p >
301+ */
302+ protected array $credentials = [
303+ 'dsn' => '',
304+ 'username' => 'root',
305+ 'password' => '',
306+ 'charset' => 'utf8mb4',
307+ 'collation' => 'utf8mb4_unicode_ci',
308+ ];
309+
311310 /**
312311 * If not specified, \InitPHP\Database\Entity::class is used by default.
313312 *
314- * @var \InitPHP\Database\Entity|string
313+ * @var \InitPHP\Database\Orm\ Entity|string
315314 */
316315 protected $entity = \App\Entities\PostEntity::class;
317316
@@ -320,14 +319,14 @@ class Posts extends \InitPHP\Database\Model
320319 *
321320 * @var string
322321 */
323- protected string $table = 'post ';
322+ protected string $schema = 'posts ';
324323
325324 /**
326325 * The name of the PRIMARY KEY column. If not, define it as NULL.
327326 *
328327 * @var null|string
329328 */
330- protected ?string $primaryKey = 'id';
329+ protected ?string $schemaId = 'id';
331330
332331 /**
333332 * Specify FALSE if you want the data to be permanently deleted.
@@ -357,79 +356,13 @@ class Posts extends \InitPHP\Database\Model
357356 */
358357 protected ?string $deletedField = 'deleted_at';
359358
360- /**
361- * An array that defines the columns that will be allowed to be used in Insert and Update operations.
362- * If you want to give access to all columns; You can specify it as NULL.
363- *
364- * @var null|string[]
365- */
366- protected ?array $allowedFields = [
367- 'title', 'content', // ...
368- ];
369-
370- /**
371- * Turns the use of callable functions on or off.
372- *
373- * @var bool
374- */
375- protected bool $allowedCallbacks = false;
376-
377- /**
378- * @var string[]|\Closure[]
379- */
380- protected array $beforeInsert = [];
381-
382- /**
383- * @var string[]|\Closure[]
384- */
385- protected array $afterInsert = [];
386-
387- /**
388- * @var string[]|\Closure[]
389- */
390- protected array $beforeUpdate = [];
391-
392- /**
393- * @var string[]|\Closure[]
394- */
395- protected array $afterUpdate = [];
396-
397- /**
398- * @var string[]|\Closure[]
399- */
400- protected array $beforeDelete = [];
401-
402- /**
403- * @var string[]|\Closure[]
404- */
405- protected array $afterDelete = [];
406-
407359 protected bool $readable = true;
408360
409361 protected bool $writable = true;
410362
411363 protected bool $deletable = true;
412364
413365 protected bool $updatable = true;
414-
415- protected array $validation = [
416- 'id' => ['is_unique', 'int'],
417- 'title' => ['required', 'string', 'length(0,255)'],
418- ];
419-
420- protected array $validationMsg = [
421- 'id' => [],
422- 'title' => [
423- 'required' => '{field} cannot be left blank.',
424- 'string' => '{field} must be a string.',
425- ],
426- ];
427-
428- protected array $validationLabels = [
429- 'id' => 'Post ID',
430- 'title' => 'Post Title',
431- // ...
432- ];
433366
434367}
435368```
@@ -439,7 +372,7 @@ The most basic example of a entity class would look like this.
439372``` php
440373namespace App\Entities;
441374
442- class PostEntity extends \InitPHP\Database\Entity
375+ class PostEntity extends \InitPHP\Database\ORM\ Entity
443376{
444377 /**
445378 * An example of a getter method for the "post_title" column.
@@ -466,7 +399,7 @@ class PostEntity extends \InitPHP\Database\Entity
466399}
467400```
468401
469- ## Developement Tools
402+ ## Development Tools
470403
471404Below I have mentioned some developer tools that you can use during and after development.
472405
@@ -549,16 +482,16 @@ DB::createImmutable([
549482
550483### Profiler Mode
551484
552- Profiler mode is a developer tool available in v2.2 and above. It is a feature that allows you to see the executed queries along with their execution times.
485+ Profiler mode is a developer tool available in v3 and above. It is a feature that allows you to see the executed queries along with their execution times.
553486
554487``` php
555488use InitPHP\Database\Facade\DB;
556489
557- DB::enableQueryProfiler ();
490+ DB::enableQueryLog ();
558491
559492DB::table('users')->where('name', 'John')->get();
560493
561- var_dump(DB::getProfilerQueries ());
494+ var_dump(DB::getQueryLogs ());
562495
563496/**
564497 * The output of the above example looks like this;
0 commit comments