Skip to content

Commit 7a41c38

Browse files
authored
Add doc for CustomConnectionTrait and CustomTableNameTrait (#457)
1 parent a8b88b9 commit 7a41c38

File tree

8 files changed

+70
-7
lines changed

8 files changed

+70
-7
lines changed

docs/traits/array-access.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ unset($user['username']); // Unset the property or set it to null using array sy
3232
isset($user['username']); // Check if the property exists and is not null using array syntax
3333
```
3434

35-
Back to [Extending with traits](traits.md).
35+
Back to [Extending Functionality With Traits](traits.md).

docs/traits/array-iterator.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ foreach ($user as $property => $value) {
3131
}
3232
```
3333

34-
Back to [Extending with traits](traits.md).
34+
Back to [Extending Functionality With Traits](traits.md).

docs/traits/arrayable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ and relations are included in the array representation.
3838

3939
Both `fields()` and `extraFields()` may return values that are either strings or `Closure` instances.
4040

41-
Back to [Extending with traits](traits.md).
41+
Back to [Extending Functionality With Traits](traits.md).

docs/traits/custom-connection.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# CustomConnectionTrait
2+
3+
`CustomConnectionTrait` allows using a custom database connection for a model class.
4+
5+
## Methods
6+
7+
The following method is provided by the `CustomConnectionTrait`:
8+
9+
- `withConnectionName()` clones the current instance of Active Record then sets the connection name for the new instance
10+
and returns the new instance.
11+
12+
## Usage
13+
14+
```php
15+
use Yiisoft\ActiveRecord\ActiveRecord;
16+
use Yiisoft\ActiveRecord\Trait\CustomConnectionTrait;
17+
18+
final class User extends ActiveRecord
19+
{
20+
use CustomConnectionTrait;
21+
}
22+
23+
$user = new User();
24+
$users = $user->createQuery()->all(); // Uses 'default' connection
25+
$users = $user->withConnectionName('db2')->createQuery()->all(); // Uses 'db2' connection
26+
```
27+
28+
Before using `db2` connection name, it should be configured in the application using
29+
`\Yiisoft\ActiveRecord\ConnectionProvider::set($db2Connection, 'db2')` method where `$db2Connection` is an instance of
30+
`\Yiisoft\Db\Connection\ConnectionInterface`.
31+
32+
See how to [Define the Database Connection for Active Record](../define-connection.md).
33+
34+
Back to [Extending Functionality With Traits](traits.md).

docs/traits/custom-table-name.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# CustomTableNameTrait
2+
3+
`CustomTableNameTrait` allows using a custom table name for a model class.
4+
5+
## Methods
6+
7+
The following method is provided by the `CustomTableNameTrait`:
8+
9+
- `withTableName()` clones the current instance of Active Record then sets the table name for the new instance
10+
and returns the new instance.
11+
12+
## Usage
13+
14+
```php
15+
use Yiisoft\ActiveRecord\ActiveRecord;
16+
use Yiisoft\ActiveRecord\Trait\CustomTableNameTrait;
17+
18+
final class User extends ActiveRecord
19+
{
20+
use CustomTableNameTrait;
21+
}
22+
23+
$user = new User();
24+
$users = $user->createQuery()->all(); // Selects data from 'user' table
25+
$users = $user->withTableName('custom_user_table')->createQuery()->all(); // Selects data from 'custom_user_table' table
26+
```
27+
28+
Back to [Extending Functionality With Traits](traits.md).

docs/traits/traits.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ These traits can be included in your model classes to add specific behaviors or
66
- [ArrayableTrait](arrayable.md) provides `toArray()` method to convert a model to an array format;
77
- [ArrayAccessTrait](array-access.md) allows accessing model properties and relations using array syntax;
88
- [ArrayIteratorTrait](array-iterator.md) allows accessing model properties and relations iteratively;
9-
- `CustomConnectionTrait` allows using a custom database connection for a model;
10-
- `CustomTableNameTrait` allows using a custom table name for a model;
9+
- [CustomConnectionTrait](custom-connection.md) allows using a custom database connection for a model;
10+
- [CustomTableNameTrait](custom-table-name.md) allows using a custom table name for a model;
11+
- `EventsTrait` allows using event handlers for model events;
1112
- `FactoryTrait` allows creating models and relations using [yiisoft/factory](https://github.com/yiisoft/factory);
1213
- `MagicPropertiesTrait` stores properties in a private property and provides magic getters
1314
and setters for accessing the model properties and relations;

src/Trait/CustomConnectionTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Yiisoft\Db\Connection\ConnectionInterface;
1010

1111
/**
12-
* Trait to implement custom connection name for ActiveRecord.
12+
* Trait to implement a custom connection name for ActiveRecord.
1313
*
1414
* @see AbstractActiveRecord::db()
1515
*/

src/Trait/CustomTableNameTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Yiisoft\ActiveRecord\Trait;
66

77
/**
8-
* Trait to implement custom table name for ActiveRecord.
8+
* Trait to implement a custom table name for ActiveRecord.
99
*
1010
* @see ActiveRecordInterface::tableName()
1111
*/

0 commit comments

Comments
 (0)