File tree Expand file tree Collapse file tree 8 files changed +70
-7
lines changed Expand file tree Collapse file tree 8 files changed +70
-7
lines changed Original file line number Diff line number Diff line change @@ -32,4 +32,4 @@ unset($user['username']); // Unset the property or set it to null using array sy
32
32
isset($user['username']); // Check if the property exists and is not null using array syntax
33
33
```
34
34
35
- Back to [ Extending with traits ] ( traits.md ) .
35
+ Back to [ Extending Functionality With Traits ] ( traits.md ) .
Original file line number Diff line number Diff line change @@ -31,4 +31,4 @@ foreach ($user as $property => $value) {
31
31
}
32
32
```
33
33
34
- Back to [ Extending with traits ] ( traits.md ) .
34
+ Back to [ Extending Functionality With Traits ] ( traits.md ) .
Original file line number Diff line number Diff line change @@ -38,4 +38,4 @@ and relations are included in the array representation.
38
38
39
39
Both ` fields() ` and ` extraFields() ` may return values that are either strings or ` Closure ` instances.
40
40
41
- Back to [ Extending with traits ] ( traits.md ) .
41
+ Back to [ Extending Functionality With Traits ] ( traits.md ) .
Original file line number Diff line number Diff line change
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 ) .
Original file line number Diff line number Diff line change
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 ) .
Original file line number Diff line number Diff line change @@ -6,8 +6,9 @@ These traits can be included in your model classes to add specific behaviors or
6
6
- [ ArrayableTrait] ( arrayable.md ) provides ` toArray() ` method to convert a model to an array format;
7
7
- [ ArrayAccessTrait] ( array-access.md ) allows accessing model properties and relations using array syntax;
8
8
- [ 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;
11
12
- ` FactoryTrait ` allows creating models and relations using [ yiisoft/factory] ( https://github.com/yiisoft/factory ) ;
12
13
- ` MagicPropertiesTrait ` stores properties in a private property and provides magic getters
13
14
and setters for accessing the model properties and relations;
Original file line number Diff line number Diff line change 9
9
use Yiisoft \Db \Connection \ConnectionInterface ;
10
10
11
11
/**
12
- * Trait to implement custom connection name for ActiveRecord.
12
+ * Trait to implement a custom connection name for ActiveRecord.
13
13
*
14
14
* @see AbstractActiveRecord::db()
15
15
*/
Original file line number Diff line number Diff line change 5
5
namespace Yiisoft \ActiveRecord \Trait ;
6
6
7
7
/**
8
- * Trait to implement custom table name for ActiveRecord.
8
+ * Trait to implement a custom table name for ActiveRecord.
9
9
*
10
10
* @see ActiveRecordInterface::tableName()
11
11
*/
You can’t perform that action at this time.
0 commit comments