Commit b6545106 authored by Alexander Butenko's avatar Alexander Butenko Committed by GitHub

Merge pull request #587 from Skazzino/patch-1

Add "hidden" columns/fields into dbObject
parents 678d6363 f844bdc3
...@@ -295,6 +295,42 @@ $products = product::arraybuilder()->paginate($page); ...@@ -295,6 +295,42 @@ $products = product::arraybuilder()->paginate($page);
echo "showing $page out of " . product::$totalPages; 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 ###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 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
...@@ -124,6 +124,9 @@ class dbObject { ...@@ -124,6 +124,9 @@ class dbObject {
* @return mixed * @return mixed
*/ */
public function __set ($name, $value) { public function __set ($name, $value) {
if (property_exists ($this, 'hidden') && array_search ($name, $this->hidden) !== false)
return;
$this->data[$name] = $value; $this->data[$name] = $value;
} }
...@@ -135,7 +138,10 @@ class dbObject { ...@@ -135,7 +138,10 @@ class dbObject {
* @return mixed * @return mixed
*/ */
public function __get ($name) { 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]; return $this->data[$name];
if (property_exists ($this, 'relations') && isset ($this->relations[$name])) { if (property_exists ($this, 'relations') && isset ($this->relations[$name])) {
...@@ -159,9 +165,8 @@ class dbObject { ...@@ -159,9 +165,8 @@ class dbObject {
} }
} }
if (isset ($this->data[$name])) { if (isset ($this->data[$name]))
return $this->data[$name]; return $this->data[$name];
}
if (property_exists ($this->db, $name)) if (property_exists ($this->db, $name))
return $this->db->$name; return $this->db->$name;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment