wip
This commit is contained in:
@ -0,0 +1 @@
|
||||
**[Documentation index is here!](index.md)**
|
||||
|
||||
72
docs/enhancing-and-overriding/how-to-enhance-a-user-model.md
Normal file
72
docs/enhancing-and-overriding/how-to-enhance-a-user-model.md
Normal file
@ -0,0 +1,72 @@
|
||||
How to Enhance a User Model
|
||||
===========================
|
||||
|
||||
It is obvious that if you started your project development on Yii2 Framework, then your business and domain needs
|
||||
are going to be very customized, and more or less unique. While our extension (and of course Yii2 Framework itself!)
|
||||
provide sensible defaults where it's possible, we encourage and keep in mind user will extend classes.
|
||||
|
||||
Very often you have to override and add your own domain (or application specific code) to your user model. With this
|
||||
extension this is very easy and can be done in a few minutes!
|
||||
|
||||
For the case if you're using [Sidekit Application Template](../installation/sidekit-application-template.md) or
|
||||
[Advanced Application Template](../installation/advanced-application-template.md) create the following class file
|
||||
at the `%PROJECT_DIR%/common/models/User.php` path:
|
||||
|
||||
```php
|
||||
namespace common\models;
|
||||
|
||||
use Da\User\Model\User as BaseUser;
|
||||
|
||||
class User extends BaseUser
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
Then adjust configuration of `yii2-usuario` extension module as follows:
|
||||
|
||||
```php
|
||||
'modules' => [
|
||||
'user' => [
|
||||
'class' => Da\User\Module::class,
|
||||
'classMap' => [
|
||||
'User' => common\models\User::class,
|
||||
],
|
||||
],
|
||||
],
|
||||
```
|
||||
|
||||
Another way to do that is to use Yii2 Dependency Injection configuration as we extensively use service container
|
||||
feature. This is good approach too:
|
||||
|
||||
```php
|
||||
'container' => [
|
||||
'definitions' => [
|
||||
Da\User\Model\User::class => common\models\User::class,
|
||||
],
|
||||
],
|
||||
```
|
||||
|
||||
Finally you can now add new methods, properties, and other things to your new `User` model class:
|
||||
|
||||
```php
|
||||
// model
|
||||
class User extends BaseUser
|
||||
{
|
||||
public function addFriend(User $friend)
|
||||
{
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
// somewhere in controller
|
||||
class ProfileController extends Controller
|
||||
{
|
||||
public function actionAddFriend(int $id)
|
||||
{
|
||||
Yii::$app->user->identity->addFriend(User::findOne($id));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> This is absolutely good way to extend almost any class of the extension. For more information you could
|
||||
> check `Da\User\Bootstrap` class file to see what you have in your control.
|
||||
@ -22,6 +22,11 @@ Installation
|
||||
- [Advanced Application Template](installation/advanced-application-template.md)
|
||||
- [Basic Application Template](installation/basic-application-template.md)
|
||||
|
||||
Enhancing and Overriding
|
||||
------------------------
|
||||
|
||||
- [How to Enhance a User Model](enhancing-and-overriding/how-to-enhance-a-user-model.md)
|
||||
|
||||
Helpful Guides
|
||||
--------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user