Skip to content

Commit fad5bfc

Browse files
authored
Merge pull request #8252 from dereuromark/docs-default-table-property
Document Controller::$defaultTable property
2 parents 41a594e + ea8596d commit fad5bfc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/en/controllers.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,36 @@ $recentArticles = $this->fetchTable('Articles')->find('all',
559559
)->all();
560560
```
561561

562+
### The defaultTable Property
563+
564+
By convention, controllers are associated with a primary model table. For
565+
example, `PostsController` will have `Posts` as its default table. You can
566+
customize this by setting the `$defaultTable` property:
567+
568+
```php
569+
class PostsController extends AppController
570+
{
571+
// Use Comments as the default table instead of Posts.
572+
protected ?string $defaultTable = 'Comments';
573+
}
574+
```
575+
576+
When `$defaultTable` is set, calling `fetchTable()` without arguments will
577+
return that table. If your controller doesn't use a table, you can set
578+
`$defaultTable` to an empty string:
579+
580+
```php
581+
class DashboardController extends AppController
582+
{
583+
// This controller doesn't use a table.
584+
protected ?string $defaultTable = '';
585+
}
586+
```
587+
588+
Setting `$defaultTable` to an empty string also prevents potential naming
589+
conflicts when loading components that share the same alias as the default
590+
table name.
591+
562592
### fetchModel()
563593

564594
`method` Cake\\Controller\\Controller::**fetchModel**(?string $modelClass = null, ?string $modelType = null): object

0 commit comments

Comments
 (0)