Skip to content

Commit

Permalink
Add wrappers for as_array method to base Model class
Browse files Browse the repository at this point in the history
  • Loading branch information
j4mie committed Nov 1, 2010
1 parent f6c3081 commit b961e70
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.markdown
Expand Up @@ -318,6 +318,25 @@ To delete the database row associated with an instance of your model, call its `
$user = Model::factory('User')->find_one($id);
$user->delete();

You can also get the all the data wrapped by a model subclass instance using the `as_array` method. This will return an associative array mapping column names (keys) to their values.

The `as_array` method takes column names as optional arguments. If one or more of these arguments is supplied, only matching column names will be returned.

class Person extends Model {
}

$person = Model::factory('Person')->create();

$person->first_name = 'Fred';
$person->surname = 'Bloggs';
$person->age = 50;

// Returns array('first_name' => 'Fred', 'surname' => 'Bloggs', 'age' => 50)
$data = $person->as_array();

// Returns array('first_name' => 'Fred', 'age' => 50)
$data = $person->as_array('first_name', 'age');

### A word on validation ###

It's generally considered a good idea to centralise your data validation in a single place, and a good place to do this is inside your model classes. This is preferable to handling validation alongside form handling code, for example. Placing validation code inside models means that if you extend your application in the future to update your model via an alternative route (say a REST API rather than a form) you can re-use the same validation code.
Expand Down
8 changes: 8 additions & 0 deletions paris.php
Expand Up @@ -341,6 +341,14 @@ public function set($property, $value) {
$this->orm->set($property, $value);
}

/**
* Wrapper for Idiorm's as_array method.
*/
public function as_array() {
$args = func_get_args();
return call_user_func_array(array($this->orm, 'as_array'), $args);
}

/**
* Save the data associated with this model instance to the database.
*/
Expand Down

0 comments on commit b961e70

Please sign in to comment.