Skip to content

Commit

Permalink
Introduce database notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
tuyakhov committed Jul 8, 2018
1 parent 018d7b1 commit 67e0df2
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 1,788 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
vendor
composer.phar
composer.phar
.DS_Store
composer.lock
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Notifier is often used as an application component and configured in the applica
'accountSid' => '...',
'authToken' => '...',
'from' => '+1234567890'
],
'database' => [
'class' => '\tuyakhov\notifications\channels\ActiveRecordChannel'
]
],
],
Expand Down Expand Up @@ -117,4 +120,42 @@ You may use the NotifiableInterface and NotifiableTrait on any of your models:
return $this->email;
}
}
```
```

#### Database notifications

The `database` notification channel stores the notification information in a database table.
You can query the table to display the notifications in your application's user interface. But, before you can do that, you will need to create a database table to hold your notifications. To do this, you can use the migration that comes with this extension:
```
yii migrate --migrationPath=@vendor/tuyakhov/yii2-notifications/src/migrations
```

**Accessing The Notifications**
Once notifications are stored in the database, you need a convenient way to access them from your notifiable entities. The `NotifiableTrait`, which comes with this extension, includes a notifications relationship that returns the notifications for the entity.
To fetch notifications, you may access this method like any other `ActiveRecord` relationship.
```php
$model = User::findOne(1);
foreach($model->notifications as $notification) {
echo $notification->subject;
}
```
If you want to retrieve only the "unread" notifications, you may use the `unreadNotifications` relationship.
```php
$model = User::findOne(1);
foreach($model->unreadNotifications as $notification) {
echo $notification->subject;
}
```
**Marking Notifications As Read**
Typically, you will want to mark a notification as "read" when a user views it. The `ReadableBehavior` in `Notification` model provides a `markAsRead` method, which updates the read_at column on the notification's database record:
```php
$model = User::findOne(1);
foreach($model->unreadNotifications as $notification) {
$notification->markAsRead();

// the following methods are also available
$notification->markAsUnread();
$notification->isUnread();
$notification->isRead();
}
```
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
},
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "*",
"yiisoft/yii2": "~2.0.13",
"yiisoft/yii2-httpclient": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "5.5.*"
"phpunit/phpunit": "*"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit 67e0df2

Please sign in to comment.