1: <?php
2:
3: namespace PHPixie\ORM\Drivers\Driver\Mongo;
4:
5: class Repository extends \PHPixie\ORM\Models\Type\Database\Implementation\Repository
6: {
7: protected $collectionName;
8: protected $dataBuilder;
9:
10: public function __construct($models, $database, $dataBuilder, $config)
11: {
12: parent::__construct($models, $database, $config);
13: $this->dataBuilder = $dataBuilder;
14: }
15:
16: protected function updateEntityData($id, $data)
17: {
18: $diff = $data->diff();
19: $set = (array) $diff->set();
20: $remove = (array) $diff->remove();
21: if(!empty($set) || !empty($remove)) {
22: $this->databaseUpdateQuery()
23: ->set($set)
24: ->_unset($remove)
25: ->where($this->config->idField, $id)
26: ->execute();
27: }
28: }
29:
30: protected function buildData($data = null)
31: {
32: if($data !== null) {
33: $data = (object) $data;
34:
35: if(property_exists($data, '_id')) {
36: $data->_id = (string) $data->_id;
37: }
38: }
39:
40: return $this->dataBuilder->diffableDocumentFromData($data);
41: }
42:
43: protected function setQuerySource($query)
44: {
45: $query->collection($this->config->collection);
46: return $query;
47: }
48:
49: protected function insertEntityData($data)
50: {
51: $data = $data->data();
52:
53: $this->databaseInsertQuery()
54: ->data((array) $data)
55: ->execute();
56: }
57: }