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
To create a new model instance of the `Model` you want to work with.
268
268
```php
269
-
$doc = new Doc;
269
+
$doc = Doc::new();
270
270
```
271
271
272
272
## Finding
@@ -471,7 +471,7 @@ You can save any property explicitly set. Explicitly set properties ignore `$gua
471
471
472
472
473
473
```php
474
-
$doc = new Doc;
474
+
$doc = Doc::new();
475
475
$doc->title = 'My Title';
476
476
$doc->json = ['key'=> 'value'];
477
477
$doc->version = 'v1';
@@ -487,7 +487,7 @@ $doc->create();
487
487
If your query has one result like with `findById()`, you can save any property explicitly set.
488
488
489
489
```php
490
-
$doc = (new Doc)->findById(1);
490
+
$doc = Doc::new()->findById(1);
491
491
$doc->json = ['key'=> 'new value'];
492
492
$doc->modified_at = $doc->created_at;
493
493
$doc->update();
@@ -502,30 +502,76 @@ The `save()` method will detect if a model's primary key is set. If the key is s
502
502
If your query has one result like with `findById()`, you can delete it.
503
503
504
504
```php
505
-
$doc = (new Doc)->findById(1);
505
+
$doc = Doc::new()->findById(1);
506
506
$doc->delete();
507
507
```
508
508
509
509
You can also bulk delete items via models you create that extend the base `Model` class. Models that extend `WPPost`, `WPTerm`, `WPUser`, `WPComment`, `WPOption` do not have the ability to bulk delete.
510
510
511
511
```php
512
-
(new Doc)->delete([1,2,3]);
512
+
Doc::new()->delete([1,2,3]);
513
+
```
514
+
515
+
## Select
516
+
517
+
You can use the `select()` method to return specific columns only.
You can run the `count()` method on a model to get the number of items.
525
+
Every model object has the methods: `avg()`, `count()`, `max()`, `min()`, and `sum()`. Each of these functions accepts a single column name as a `string` or an instance of `\TypeRocket\Database\SqlRaw`.
518
526
519
527
```php
520
-
$number = $doc->findAll()->count();
528
+
use \TypeRocket\Database\SqlRaw;
529
+
530
+
// Single column
531
+
$doc->sum('id');
532
+
533
+
// Expression
534
+
$doc->sum(SqlRaw::new('`id` * `id`'));
521
535
```
522
536
523
-
## Select
524
-
525
-
You can use the `select()` method to return specific columns only.
537
+
### Avg
538
+
539
+
The `avg()` method gets the average calculated value:
540
+
541
+
```php
542
+
$number = $doc->avg('id');
543
+
```
544
+
545
+
### Count
546
+
547
+
The `count()` method gets the count:
548
+
549
+
```php
550
+
$number = $doc->count();
551
+
```
552
+
553
+
### Max
554
+
555
+
The `max()` method gets the max calculated value:
556
+
557
+
```php
558
+
$number = $doc->max('id');
559
+
```
560
+
561
+
### Min
562
+
563
+
The `min()` method gets the min calculated value:
526
564
527
565
```php
528
-
$doc = (new Doc)->select('id', 'title')->first();
566
+
$number = $doc->min('id');
567
+
```
568
+
569
+
### Sum
570
+
571
+
The `sum()` method gets the sum calculated value:
572
+
573
+
```php
574
+
$number = $doc->min('id');
529
575
```
530
576
531
577
## Get ID and Set Primary Key
@@ -614,7 +660,7 @@ class Doc extends Model
614
660
You now have access to a `slug` property on the `Doc` model.
0 commit comments