Permalink
Browse files

updating README with another example

  • Loading branch information...
enygma committed Jun 6, 2011
1 parent 3df6df5 commit 19c60743a6aef606724e4ac3ffdbd71d60c4b88c
Showing with 58 additions and 0 deletions.
  1. +58 −0 README.md
View
@@ -25,3 +25,61 @@ $dm->execute();
echo 'Should be "test": '.$dm->first_map;
?>
```
+This also could potentially be useful if you're pulling data into a model and want to remap it every time:
+
+```php
+<?php
+include_once 'lib/DataMapper.php';
+include_once 'lib/ExpandObject.php';
+include_once 'lib/MapItem.php';
+
+class UserModel {
+
+ private $_map = null;
+
+ public function __construct()
+ {
+ $this->_map = new DataMapper();
+ $this->_map = $this->_map->configure(array(
+ 'title' => new MapItem(array(
+ 'map:input' => 'details->name',
+ 'map:output' => 'username',
+ 'alias' => 'uname'
+ ))
+ ));
+ }
+
+ public function fetchUsers()
+ {
+ // Make us some sample users...
+ $users = array();
+ for($i=0;$i<5;$i++){
+ $users[]=(object)array('details' => (object)array(
+ 'name' => 'user'.$i
+ )
+ );
+ }
+
+ $results = array();
+ $dm = new DataMapper();
+ foreach($users as $key => $user){
+ // a sample class...
+ $target = new stdClass();
+
+ $results[$key] = $this->_map
+ ->setSource($user)
+ ->setTarget($target)
+ ->execute();
+ }
+
+ return $results;
+ }
+}
+
+$um = new UserModel();
+$users = $um->fetchUsers();
+foreach($users as $user){
+ echo 'from $user::details->name: '.$user->username."\n";
+}
+?>
+```

0 comments on commit 19c6074

Please sign in to comment.