Revision #105 has been created by rackycz on Sep 21, 2019, 6:31:31 AM with the memo:
edit
« previous (#104) next (#106) »
Changes
Title
unchanged
Yii v2 snippet guide
Category
unchanged
Tutorials
Yii version
unchanged
2.0
Tags
unchanged
tutorial,beginner,yii2
Content
changed
[...]
INSERT INTO `user` (`id`, `username`, `password`, `email`, `authKey`) VALUES (NULL, 'user01', '0497fe4d674fe37194a6fcb08913e596ef6a307f', 'user01@gmail.com', NULL);
```
If you must use MyISAM instead of InnoDB, just change the word InnoDB into MYISAM.
Then use GII to generate model. The GII URL will probably be
- [http://localhost/basic/web/index.php?r=gii](http://localhost/basic/web/index.php?r=gii).
- When creating the model, check this checkbox: **Enable I18N** ... we will need i18n later
- Enter table name "user" and model name "User"
- Note that there already is a model named User in the demo app. You will not need it if you want to use login-via-DB so feel free to check the overwrite-checkbox in GII. The newly created User model will now be usable only for table operations, not for login. But we will enhance it, see the snippet below.
- The new model will onreplace existing model User with following snippet
- The model was generated by Gii and originally ha
ved 3 methods: tableName(), rules(), attributeLabels()
- In order to use the DB for login, we need
ed to implement IdentityInterface which requires [5 new methods](https://www.yiiframework.com/doc/api/2.0/yii-web-identityinterface).
- Plus we add 2 methods because of the default LoginForm
otherwise the web will not work.
- Once you use the snippet below, you can create CRUD for model User.
The final model should look like this:and 1 validator.
```php[...]
```
Now you can also create Validators vs JavaScript:
- There are 2 types of validators. All of them are used in method **rules**, but as you can see, the validator **setPasswordWhenChanged** is my custom validator and needs a special method.
- If a validator does not need this special method, it is automatically converted into JavaScript and is used on the web page when you are typing.
- If a validator needs the method, it cannot be converted into JavaScript so the rule is checked only in the moment when user sends the form to the server - after successful JavaScript validation.
Now you can also create **CRUD** using GII:
- [http://localhost/basic/web/index.php?r=gii](http://localhost/basic/web/index.php?r=gii).
CRUD = Create Read Update Delete = views and controller. On the GII page enter following values:
- Model Class = app\models\User
- Search Model Class = app\models\UserSearch
- Controller Class = app\controllers\UserController
- View Path can be empty or you can set: views\user[...]