Skip to content

Commit 61a326f

Browse files
committed
Optimize
1 parent 5891ac0 commit 61a326f

File tree

4 files changed

+15
-37
lines changed

4 files changed

+15
-37
lines changed

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
CodeIgniter 3 Active Record (ORM) Standard Model supported Read & Write Connections
1010

1111
[![Latest Stable Version](https://poser.pugx.org/yidas/codeigniter-model/v/stable?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-model)
12-
[![Latest Unstable Version](https://poser.pugx.org/yidas/codeigniter-model/v/unstable?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-model)
1312
[![License](https://poser.pugx.org/yidas/codeigniter-model/license?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-model)
1413

1514
This ORM Model extension is collected into [yidas/codeigniter-pack](https://github.com/yidas/codeigniter-pack) which is a complete solution for Codeigniter framework.
@@ -931,7 +930,7 @@ public array getErrors()
931930

932931
### Declaring Rules
933932

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.
933+
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 with returning [CodeIgniter Rules](https://www.codeigniter.com/userguide3/libraries/form_validation.html#setting-rules-using-an-array).
935934

936935
#### `rules()`
937936

@@ -963,7 +962,7 @@ class PostsModel extends yidas\Model
963962
}
964963
```
965964

966-
> The validation rules and pattern could refer [CodeIgniter Rule Reference](https://www.codeigniter.com/userguide3/libraries/form_validation.html#rule-reference)
965+
> The returning array format could refer [CodeIgniter - Setting Rules Using an Array](https://www.codeigniter.com/userguide3/libraries/form_validation.html#setting-rules-using-an-array), and the rules pattern could refer [CodeIgniter - Rule Reference](https://www.codeigniter.com/userguide3/libraries/form_validation.html#rule-reference)
967966
968967
#### Error Message with Language
969968

@@ -1027,6 +1026,7 @@ public function filters()
10271026
[['name'], function($value) { // Perform defined anonymous function. 'value' => '[Filtered]value'
10281027
return "[Filtered]" . $value;
10291028
}],
1029+
[['content'], [$this->security, 'xss_clean']], // Perform CodeIgniter XSS Filtering for content input data
10301030
];
10311031
}
10321032
```

Diff for: example/userACL/My_model.php

+9-31
Original file line numberDiff line numberDiff line change
@@ -76,34 +76,12 @@ class My_model extends yidas\Model
7676
*/
7777
protected $deletedUserAttribute = 'deleted_by';
7878

79-
/**
80-
* @var int Application ACL
81-
*/
82-
protected $companyID;
83-
84-
/**
85-
* @var int Application User
86-
*/
87-
protected $userID;
88-
8979
function __construct()
9080
{
9181
parent::__construct();
9282

93-
// Assgin UserID and CompanyID from your own App mechanism
94-
$this->loadACL();
95-
}
96-
97-
/**
98-
* Load ACL from application
99-
*
100-
* @param int $companyID
101-
* @param int $userID
102-
*/
103-
public function loadACL($companyID=NULL, $userID=NULL)
104-
{
105-
$this->companyID = ($companyID) ? $companyID : $this->config->item('sessionCompanyID');
106-
$this->userID = ($userID) ? $userID : $this->config->item('sessionUserID');
83+
// Load your own user library for companyID and userID data
84+
$this->load->library("user");
10785
}
10886

10987
/**
@@ -115,15 +93,15 @@ protected function _globalScopes()
11593

11694
$this->getBuilder()->where(
11795
$this->_field($this->companyAttribute),
118-
$this->companyID
96+
$this->user->getCompanyID();
11997
);
12098
}
12199

122100
if ($this->userAttribute) {
123101

124102
$this->getBuilder()->where(
125103
$this->_field($this->userAttribute),
126-
$this->userID
104+
$this->user->getID();
127105
);
128106
}
129107
return parent::_globalScopes();
@@ -136,16 +114,16 @@ protected function _attrEventBeforeInsert(&$attributes)
136114
// Auto Company
137115
if ($this->companyAttribute && !isset($attributes[$this->companyAttribute])) {
138116

139-
$attributes[$this->companyAttribute] = $this->companyID;
117+
$attributes[$this->companyAttribute] = $this->user->getCompanyID();;
140118
}
141119
// Auto User
142120
if ($this->userAttribute && !isset($attributes[$this->userAttribute])) {
143121

144-
$attributes[$this->userAttribute] = $this->userID;
122+
$attributes[$this->userAttribute] = $this->user->getID();;
145123
}
146124
// Auto created_by
147125
if ($this->createdUserAttribute && !isset($attributes[$this->createdUserAttribute])) {
148-
$attributes[$this->createdUserAttribute] = $this->userID;
126+
$attributes[$this->createdUserAttribute] = $this->user->getID();
149127
}
150128

151129
return parent::_attrEventBeforeInsert($attributes);
@@ -157,7 +135,7 @@ public function _attrEventBeforeUpdate(&$attributes)
157135
{
158136
// Auto updated_by
159137
if ($this->updatedUserAttribute && !isset($attributes[$this->updatedUserAttribute])) {
160-
$attributes[$this->updatedUserAttribute] = $this->userID;
138+
$attributes[$this->updatedUserAttribute] = $this->user->getID();
161139
}
162140
return parent::_attrEventBeforeUpdate($attributes);
163141
}
@@ -168,7 +146,7 @@ public function _attrEventBeforeDelete(&$attributes)
168146
{
169147
// Auto deleted_by
170148
if ($this->deletedUserAttribute && !isset($attributes[$this->deletedUserAttribute])) {
171-
$attributes[$this->deletedUserAttribute] = $this->userID;
149+
$attributes[$this->deletedUserAttribute] = $this->user->getID();
172150
}
173151
return parent::_attrEventBeforeDelete($attributes);
174152
}

Diff for: example/userACL/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ By default, `My_model` assumes that each row data in model is belong to a user,
4747
```php
4848
class Post_model extends My_model
4949
{
50-
protected $userAttribute = NULL;
50+
protected $userAttribute = false;
5151
}
5252
```
5353

@@ -67,7 +67,7 @@ By default, `My_model` assumes that each row data in model is belong to a compan
6767
```php
6868
class Post_model extends My_model
6969
{
70-
protected $companyAttribute = NULL;
70+
protected $companyAttribute = false;
7171
}
7272
```
7373

Diff for: src/Model.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ public function batchInsert($data, $runValidation=true)
625625

626626
// Validation
627627
if ($runValidation && false===$attributes=$this->validate($attributes, true))
628-
return false;
628+
return false;
629629

630630
$this->_attrEventBeforeInsert($attributes);
631631
}

0 commit comments

Comments
 (0)