Skip to content

Commit b654510

Browse files
authored
Merge pull request #587 from Skazzino/patch-1
Add "hidden" columns/fields into dbObject
2 parents 678d636 + f844bdc commit b654510

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

dbObject.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,42 @@ $products = product::arraybuilder()->paginate($page);
295295
echo "showing $page out of " . product::$totalPages;
296296

297297
```
298+
299+
###Hidden Fields
300+
Sometimes it's important to block some fields that can be accessed from outside the model class (for example, the user password).
301+
302+
To block the access to certain fields using the `->` operator, you can declare the `$hidden` array into the model class. This array holds column names that can't be accessed with the `->` operator.
303+
304+
For example:
305+
306+
```php
307+
class User extends dbObject {
308+
protected $dbFields = array(
309+
'username' => array('text', 'required'),
310+
'password' => array('text', 'required'),
311+
'is_admin' => array('bool'),
312+
'token' => array('text')
313+
);
314+
315+
protected $hidden = array(
316+
'password', 'token'
317+
);
318+
}
319+
```
320+
321+
If you try to:
322+
```php
323+
echo $user->password;
324+
echo $user->token;
325+
```
326+
327+
Will return `null`, and also:
328+
```php
329+
$user->password = "my-new-password";
330+
```
331+
332+
Won't change the current `password` value.
333+
298334
###Examples
299335

300336
Please look for a use examples in <a href='tests/dbObjectTests.php'>tests file</a> and test models inside the <a href='tests/models/'>test models</a> directory

dbObject.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ public function __construct ($data = null) {
124124
* @return mixed
125125
*/
126126
public function __set ($name, $value) {
127+
if (property_exists ($this, 'hidden') && array_search ($name, $this->hidden) !== false)
128+
return;
129+
127130
$this->data[$name] = $value;
128131
}
129132

@@ -135,7 +138,10 @@ public function __set ($name, $value) {
135138
* @return mixed
136139
*/
137140
public function __get ($name) {
138-
if (isset ($this->data[$name]) && $this->data[$name] instanceof dbObject)
141+
if (property_exists ($this, 'hidden') && array_search ($name, $this->hidden) !== false)
142+
return null;
143+
144+
if (isset ($this->data[$name]) && $this->data[$name] instanceof dbObject)
139145
return $this->data[$name];
140146

141147
if (property_exists ($this, 'relations') && isset ($this->relations[$name])) {
@@ -159,9 +165,8 @@ public function __get ($name) {
159165
}
160166
}
161167

162-
if (isset ($this->data[$name])) {
168+
if (isset ($this->data[$name]))
163169
return $this->data[$name];
164-
}
165170

166171
if (property_exists ($this->db, $name))
167172
return $this->db->$name;

0 commit comments

Comments
 (0)