Skip to content

Commit 9e255f4

Browse files
committed
✨ add protected fields functionality
Signed-off-by: otengkwame <[email protected]>
1 parent e06f58b commit 9e255f4

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

Core/core/Models/EasyModel.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class EasyModel extends Model
6565
*/
6666
public $returnAs = 'object';
6767

68+
/**
69+
* Protected fields
70+
*
71+
* @var array
72+
*/
73+
public $protected = [];
74+
6875
/**
6976
* Construct the CI_Model
7077
*/
@@ -416,6 +423,25 @@ public function getRowResult($query, $last = false)
416423
return $query->{$mode}('object');
417424
}
418425

426+
/**
427+
* Protect fields by removing them from $row
428+
*
429+
* @param array|object $row
430+
* @return mixed
431+
*/
432+
public function protectFields($row)
433+
{
434+
foreach ($this->protected as $attr) {
435+
if (is_object($row)) {
436+
unset($row->$attr);
437+
} else {
438+
unset($row[$attr]);
439+
}
440+
}
441+
442+
return $row;
443+
}
444+
419445
/**
420446
* Grabs data from a table
421447
* OR a single record by passing $id,
@@ -750,8 +776,17 @@ public function saveOnly($data, $show = false)
750776
*/
751777
public function create($data)
752778
{
779+
780+
753781
$this->db->set($data)->insert($this->table);
754-
return $this->selectLast('*');
782+
783+
$data = $this->selectLast('*');
784+
785+
if (!empty($this->protected)) {
786+
return $this->protectFields($data);
787+
}
788+
789+
return $data;
755790
}
756791

757792
/**

0 commit comments

Comments
 (0)