1: <?php
2:
3: namespace PHPixie\ORM\Mappers;
4:
5: class Update
6: {
7: public function map($updateDatabaseQuery, $update)
8: {
9: $set = array();
10: $remove = array();
11: $increments = array();
12:
13: foreach($update->updates() as $field => $value) {
14: if($value instanceof \PHPixie\ORM\Values\Update\Remove) {
15: $remove[] = $field;
16:
17: }elseif($value instanceof \PHPixie\ORM\Values\Update\Increment) {
18: $increments[$field] = $value->amount();
19:
20: }else{
21:
22: $set[$field] = $value;
23: }
24: }
25:
26: $updateDatabaseQuery->set($set);
27:
28: if(!empty($increments)) {
29: if(!($updateDatabaseQuery instanceof \PHPixie\Database\Query\Type\Update\Incrementable))
30: throw new \PHPixie\ORM\Exception\Mapper("Database query does not support increments");
31:
32: $updateDatabaseQuery->increment($increments);
33: }
34:
35: if(!empty($remove)) {
36: if(!($updateDatabaseQuery instanceof \PHPixie\Database\Query\Type\Update\Unsetable))
37: throw new \PHPixie\ORM\Exception\Mapper("Database query does not support unsetting fields");
38:
39: $updateDatabaseQuery->_unset($remove);
40: }
41:
42:
43: }
44:
45:
46: }