Skip to content

Commit fd37b74

Browse files
committed
add model function docs
1 parent 1b34a75 commit fd37b74

1 file changed

Lines changed: 62 additions & 16 deletions

File tree

v5/models.md

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ class Version extends WPTerm
130130
You can query the database and retrieve the model you want. When a list of multiple items is returned a `Results` collection is returned.
131131

132132
```php
133-
$docs = (new Doc())->where('version', 'v1')->find();
133+
$docs = Doc::new()->where('version', 'v1')->find();
134134
```
135135

136136
You can also get a single item where the version is not `v1`.
137137

138138
```php
139-
$doc = (new Doc())->where('version', '!=' ,'v1')->first();
139+
$doc = Doc::new()->where('version', '!=' ,'v1')->first();
140140
```
141141

142142
And, where the ID is greater than 10.
@@ -151,7 +151,7 @@ $doc->first();
151151
Or, a single `Doc` by ID.
152152

153153
```php
154-
$doc = (new Doc())->find(1);
154+
$doc = Doc::new()->find(1);
155155
```
156156

157157
## Fillable
@@ -266,7 +266,7 @@ class Doc extends Model
266266

267267
To create a new model instance of the `Model` you want to work with.
268268
```php
269-
$doc = new Doc;
269+
$doc = Doc::new();
270270
```
271271

272272
## Finding
@@ -471,7 +471,7 @@ You can save any property explicitly set. Explicitly set properties ignore `$gua
471471

472472

473473
```php
474-
$doc = new Doc;
474+
$doc = Doc::new();
475475
$doc->title = 'My Title';
476476
$doc->json = ['key'=> 'value'];
477477
$doc->version = 'v1';
@@ -487,7 +487,7 @@ $doc->create();
487487
If your query has one result like with `findById()`, you can save any property explicitly set.
488488

489489
```php
490-
$doc = (new Doc)->findById(1);
490+
$doc = Doc::new()->findById(1);
491491
$doc->json = ['key'=> 'new value'];
492492
$doc->modified_at = $doc->created_at;
493493
$doc->update();
@@ -502,30 +502,76 @@ The `save()` method will detect if a model's primary key is set. If the key is s
502502
If your query has one result like with `findById()`, you can delete it.
503503

504504
```php
505-
$doc = (new Doc)->findById(1);
505+
$doc = Doc::new()->findById(1);
506506
$doc->delete();
507507
```
508508

509509
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.
510510

511511
```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.
518+
519+
```php
520+
$doc = Doc::new()->select('id', 'title')->first();
513521
```
514522

515-
## Count
523+
## Functions
516524

517-
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`.
518526

519527
```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`'));
521535
```
522536

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:
526564

527565
```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');
529575
```
530576

531577
## Get ID and Set Primary Key
@@ -614,7 +660,7 @@ class Doc extends Model
614660
You now have access to a `slug` property on the `Doc` model.
615661

616662
```php
617-
$doc = new Doc;
663+
$doc = Doc::new();
618664
echo $doc->slug;
619665
```
620666

0 commit comments

Comments
 (0)