Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce database notifications #5

Merged
merged 1 commit into from
Jul 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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