Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions dbObject.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,42 @@ $products = product::arraybuilder()->paginate($page);
echo "showing $page out of " . product::$totalPages;

```

###Hidden Fields
Sometimes it's important to block some fields that can be accessed from outside the model class (for example, the user password).

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.

For example:

```php
class User extends dbObject {
protected $dbFields = array(
'username' => array('text', 'required'),
'password' => array('text', 'required'),
'is_admin' => array('bool'),
'token' => array('text')
);

protected $hidden = array(
'password', 'token'
);
}
```

If you try to:
```php
echo $user->password;
echo $user->token;
```

Will return `null`, and also:
```php
$user->password = "my-new-password";
```

Won't change the current `password` value.

###Examples

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
11 changes: 8 additions & 3 deletions dbObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ public function __construct ($data = null) {
* @return mixed
*/
public function __set ($name, $value) {
if (property_exists ($this, 'hidden') && array_search ($name, $this->hidden) !== false)
return;

$this->data[$name] = $value;
}

Expand All @@ -135,7 +138,10 @@ public function __set ($name, $value) {
* @return mixed
*/
public function __get ($name) {
if (isset ($this->data[$name]) && $this->data[$name] instanceof dbObject)
if (property_exists ($this, 'hidden') && array_search ($name, $this->hidden) !== false)
return null;

if (isset ($this->data[$name]) && $this->data[$name] instanceof dbObject)
return $this->data[$name];

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

if (isset ($this->data[$name])) {
if (isset ($this->data[$name]))
return $this->data[$name];
}

if (property_exists ($this->db, $name))
return $this->db->$name;
Expand Down