Skip to content

Commit cca4cf4

Browse files
committed
Fix validation for ORM updating
1 parent f815dba commit cca4cf4

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ if ($post) {
115115
}
116116
```
117117

118+
> The pattern is similar to [Yii2 Active Record](https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#active-record) and [Laravel Eloquent](https://laravel.com/docs/5.8/eloquent#inserting-and-updating-models)
119+
118120
### Find with Query Builder
119121

120-
The Model would defined database coonnections and table itself.
122+
Start to use CodeIgniter Query Builder from `find()` method, the Model will automatically load its own database connections and data tables.
121123

122124
```php
123125
$records = $this->Posts_model->find()

src/Model.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Base Model
99
*
1010
* @author Nick Tsai <myintaer@gmail.com>
11-
* @version 2.16.0
11+
* @version 2.16.1
1212
* @see https://github.com/yidas/codeigniter-model
1313
*/
1414
class Model extends \CI_Model implements \ArrayAccess
@@ -338,6 +338,20 @@ public function validate($attributes=[], $returnData=false)
338338
// Get validation rules from function setting
339339
$rules = $this->rules();
340340

341+
// The ORM update will only collect rules with corresponding modified attributes.
342+
if ($this->_selfCondition) {
343+
344+
$newRules = [];
345+
foreach ((array) $rules as $key => $rule) {
346+
if (isset($this->_writeProperties[$rule['field']])) {
347+
// Add into new rules for updating
348+
$newRules[] = $rule;
349+
}
350+
}
351+
// Replace with mapping rules
352+
$rules = $newRules;
353+
}
354+
341355
// CodeIgniter form_validation doesn't work with empty array data
342356
if (empty($rules) || empty($data))
343357
return ($returnData) ? $data : true;

0 commit comments

Comments
 (0)