1: <?php
2:
3: namespace PHPixie\ORM\Models\Type\Database\Implementation;
4:
5: abstract class Repository implements \PHPixie\ORM\Models\Type\Database\Repository
6: {
7: 8: 9:
10: protected $databaseModel;
11:
12: 13: 14:
15: protected $database;
16: protected $config;
17:
18: public function __construct($databaseModel, $database, $config)
19: {
20: $this->databaseModel = $databaseModel;
21: $this->database = $database;
22: $this->config = $config;
23: }
24:
25: public function config()
26: {
27: return $this->config;
28: }
29:
30: public function modelName()
31: {
32: return $this->config->model;
33: }
34:
35: public function query()
36: {
37: return $this->databaseModel->query($this->modelName());
38: }
39:
40: public function create($data = null)
41: {
42: return $this->entity($data);
43: }
44:
45: public function load($data)
46: {
47: return $this->entity($data, false);
48: }
49:
50: protected function entity($data = null, $isNew = true)
51: {
52: $modelName = $this->modelName();
53: $data = $this->buildData($data);
54: return $this->databaseModel->entity($modelName, $data, $isNew);
55: }
56:
57: public function connection()
58: {
59: return $this->database->connection($this->config->connection);
60: }
61:
62: public function delete($entity)
63: {
64: if ($entity->isDeleted())
65: throw new \PHPixie\ORM\Exception\Entity("This model has already been deleted.");
66:
67: if (!$entity->isNew()) {
68: $this->query()->in($entity)->delete();
69: }
70:
71: $entity->setIsDeleted(true);
72: }
73:
74: public function save($entity)
75: {
76: if ($entity->isDeleted())
77: throw new \PHPixie\ORM\Exception\Entity("Deleted models cannot be saved.");
78:
79: $data = $entity->data();
80: $idField = $this->config->idField;
81:
82: if($entity->isNew()){
83:
84: $this->insertEntityData($data);
85: if($entity->id() === null) {
86: $id = $this->connection()->insertId();
87: $entity->setId($id);
88: }
89:
90: $entity->setIsNew(false);
91: } else {
92: $this->updateEntityData($entity->id(), $data);
93: }
94:
95: $data->setCurrentAsOriginal();
96: }
97:
98: protected function insertEntityData($data)
99: {
100: $this->databaseInsertQuery()
101: ->data((array) $data->data())
102: ->execute();
103: }
104:
105: public function databaseSelectQuery()
106: {
107: return $this->setQuerySource($this->connection()->selectQuery());
108: }
109:
110: public function databaseUpdateQuery()
111: {
112: return $this->setQuerySource($this->connection()->updateQuery());
113: }
114:
115: public function databaseDeleteQuery()
116: {
117: return $this->setQuerySource($this->connection()->deleteQuery());
118: }
119:
120: public function databaseInsertQuery()
121: {
122: return $this->setQuerySource($this->connection()->insertQuery());
123: }
124:
125: public function databaseCountQuery()
126: {
127: return $this->setQuerySource($this->connection()->countQuery());
128: }
129:
130: abstract protected function setQuerySource($query);
131: abstract protected function updateEntityData($id, $data);
132:
133: }
134: