You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-***[Read & Write Splitting](#read--write-connections)** for Replications*
27
27
28
28
This package provide Base Model which extended `CI_Model` and provided full CRUD methods to make developing database interactions easier and quicker for your CodeIgniter applications.
29
29
@@ -52,6 +52,7 @@ OUTLINE
52
52
-[delete()](#delete)
53
53
-[getLastInsertID()](#getlastinsertid)
54
54
-[getAffectedRows()](#getaffectedrows)
55
+
-[count()](#count)
55
56
-[setAlias()](#setalias)
56
57
-[Active Record (ORM)](#active-record-orm)
57
58
-[Inserts](#inserts)
@@ -69,6 +70,15 @@ OUTLINE
69
70
-[Query Scopes](#query-scopes)
70
71
-[Configuration](#configuration-2)
71
72
-[Methods](#method-3)
73
+
-[Validation](#validation)
74
+
-[Validating Input](#validating-input)
75
+
-[validate()](#validate)
76
+
-[getErrors()](#geterrors)
77
+
-[Declaring Rules](#declaring-rules)
78
+
-[rules()](#rules)
79
+
-[Error Message with Language](#error-message-with-language)
> After starting `find()` from a model, it return original `CI_DB_query_builder` for chaining. The query builder could refer [CodeIgniter Query Builder Class Document](https://www.codeigniter.com/userguide3/database/query_builder.html)
381
+
368
382
##### Query Builder Implementation
369
383
370
384
You could assign Query Builder as a variable to handle add-on conditions instead of using `$this->Model->getBuilder()`.
As a rule of thumb, you should never trust the data received from end users and should always validate it before putting it to good use.
873
+
874
+
The ORM Model validation integrates [CodeIgniter Form Validation](https://www.codeigniter.com/userguide3/libraries/form_validation.html) that provides consistent and smooth way to deal with model data validation.
875
+
876
+
### Validating Input
877
+
878
+
Given a model populated with user inputs, you can validate the inputs by calling the `validate()` method. The method will return a boolean value indicating whether the validation succeeded or not. If not, you may get the error messages from `getErrors()` method.
879
+
880
+
#### `validate()`
881
+
882
+
Performs the data validation with filters
883
+
884
+
> ORM only performs validation for assigned properties.
885
+
886
+
```php
887
+
public boolean validate($data=[], $returnData=false)
888
+
```
889
+
890
+
*Exmaple:*
891
+
892
+
```php
893
+
$this->load->model('PostsModel');
894
+
895
+
if ($this->PostsModel->validate($inputData)) {
896
+
// all inputs are valid
897
+
} else {
898
+
// validation failed: $errors is an array containing error messages
899
+
$errors = $this->PostsModel->getErrors();
900
+
}
901
+
```
902
+
903
+
> The methods of `yidas\Model` for modifying such as `insert()` and `update()` will also perform validation. You can turn off `$runValidation` parameter of methods if you ensure that the input data has been validated.
904
+
905
+
*Exmaple of ORM Model:*
906
+
907
+
```php
908
+
$this->load->model('PostsModel');
909
+
$post = new PostsModel;
910
+
$post->title = '';
911
+
// ORM assigned or modified attributes will be validated by calling `validate()` without parameters
912
+
if ($post->validate()) {
913
+
// Already performing `validate()` so that turn false for $runValidation
914
+
$result = $post->save(false);
915
+
} else {
916
+
// validation failed: $errors is an array containing error messages
917
+
$errors = post->getErrors();
918
+
}
919
+
```
920
+
921
+
> A ORM model's properties will be changed by filter after performing validation. If you have previously called `validate()`.
922
+
You can turn off `$runValidation` of `save()` for saving without repeated validation.
923
+
924
+
### getErrors()
925
+
926
+
Validation - Get error data referenced by last failed Validation
927
+
928
+
```php
929
+
public array getErrors()
930
+
```
931
+
932
+
### Declaring Rules
933
+
934
+
To make `validate()` really work, you should declare validation rules for the attributes you plan to validate. This should be done by overriding the `rules()` method.
935
+
936
+
#### `rules()`
937
+
938
+
Returns the validation rules for attributes.
939
+
940
+
```php
941
+
public array rules()
942
+
```
943
+
944
+
*Example:*
945
+
946
+
```php
947
+
class PostsModel extends yidas\Model
948
+
{
949
+
protected $table = "posts";
950
+
951
+
/**
952
+
* Override rules function with validation rules setting
953
+
*/
954
+
public function rules()
955
+
{
956
+
return [
957
+
[
958
+
'field' => 'title',
959
+
'rules' => 'required|min_length[3]',
960
+
],
961
+
];
962
+
}
963
+
}
964
+
```
965
+
966
+
> The validation rules and pattern could refer [CodeIgniter Rule Reference](https://www.codeigniter.com/userguide3/libraries/form_validation.html#rule-reference)
967
+
968
+
#### Error Message with Language
969
+
970
+
When you are dealing with i18n issue of validation's error message, you can integrate [CodeIgniter language class](https://www.codeigniter.com/userguide3/libraries/language.html) into rules. The following sample code is available for you to implement:
In above case, the language file could be `application/language/en-US/error_messages_lang.php`:
995
+
996
+
```php
997
+
$lang['required'] = '`%s` is required';
998
+
$lang['min_length'] = '`%s` requires at least %d letters';
999
+
```
1000
+
1001
+
After that, the `getErrors()` could returns field error messages with current language.
1002
+
1003
+
### Filters
1004
+
1005
+
User inputs often need to be filtered or preprocessed. For example, you may want to trim the spaces around the username input. You may declare filter rules in `filter()` method to achieve this goal.
1006
+
1007
+
> In model's `validate()` process, the `filters()` will be performed before [`rules()`](#declaring-rules), which means the input data validated by [`rules()`](#declaring-rules) is already be filtered.
1008
+
1009
+
To enable filters for `validate()`, you should declare filters for the attributes you plan to perform. This should be done by overriding the `filters()` method.
1010
+
1011
+
#### `filters()`
1012
+
1013
+
Returns the filter rules for validation.
1014
+
1015
+
```php
1016
+
public array filters()
1017
+
```
1018
+
1019
+
*Example:*
1020
+
1021
+
```php
1022
+
public function filters()
1023
+
{
1024
+
return [
1025
+
[['title', 'name'], 'trim'], // Perform `trim()` for title & name input data
1026
+
[['title'], 'static::method'], // Perform `public static function method($value)` in this model
> The filters format: `[[['attr1','attr2'], callable],]`
1035
+
839
1036
840
1037
---
841
1038
842
-
Read & Write Connections
1039
+
READ & WRITE CONNECTIONS
843
1040
------------------------
844
1041
845
1042
Sometimes you may wish to use one database connection for `SELECT` statements, and another for `INSERT`, `UPDATE`, and `DELETE` statements. This Model implements Replication and Read-Write Splitting, makes database connections will always be used while using Model usages.
0 commit comments