1: <?php
2:
3: namespace zsql;
4:
5: class Model
6: {
7: /**
8: * @var \Engine\Database
9: **/
10: protected $database;
11:
12: /**
13: * Name of the primary key column for a table, if one exists
14: *
15: * @var string
16: **/
17: protected $primaryKey;
18:
19: /**
20: * Class to use for results
21: *
22: * @var string
23: */
24: protected $resultClass;
25:
26: /**
27: * Name of the table associated with the model
28: *
29: * @var string
30: */
31: protected $tableName;
32:
33: /**
34: * @param \Engine\Database $database
35: */
36: public function __construct(Database $database)
37: {
38: $this->setDatabase($database)
39: ->init();
40: }
41:
42: /**
43: * Post initialization hook
44: *
45: * @return void
46: */
47: public function init() {}
48:
49: /**
50: * Find a record by primary key
51: *
52: * @param mixed $identity
53: * @return \stdClass
54: */
55: public function find($identity)
56: {
57: if( !$this->primaryKey ) {
58: throw new Exception('No primary key!');
59: }
60: return $this->select()
61: ->where($this->primaryKey, $identity)
62: ->limit(1)
63: ->query()
64: ->fetchRow();
65: }
66:
67: /**
68: * Find many records by identity
69: *
70: * @param array $identities
71: * @return array
72: */
73: public function findMany(array $identities)
74: {
75: if( !$this->primaryKey ) {
76: throw new Exception('No primary key!');
77: }
78: return $this->select()
79: ->whereIn($this->primaryKey, $identities)
80: ->query()
81: ->fetchAll();
82: }
83:
84: /**
85: * Getter method for the database adapter
86: *
87: * @return \Engine\Database
88: */
89: public function getDatabase()
90: {
91: return $this->database;
92: }
93:
94: /**
95: * Setter method for the database adapter
96: *
97: * @param \Engine\Database $database
98: * @return \Engine\Model
99: */
100: public function setDatabase(Database $database)
101: {
102: $this->database = $database;
103: return $this;
104: }
105:
106: /**
107: * Getter method for the $tableName property.
108: *
109: * @return string
110: */
111: public function getTableName()
112: {
113: return $this->tableName;
114: }
115:
116: /**
117: * Setter method for the $tableName property.
118: *
119: * @param string $table
120: * @return \Engine\Model
121: */
122: public function setTableName($table)
123: {
124: $this->tableName = $table;
125: return $this;
126: }
127:
128: /**
129: * Getter method for the $primaryKey property
130: *
131: * @return string
132: */
133: public function getPrimaryKey()
134: {
135: return $this->primaryKey;
136: }
137:
138: /**
139: * Setter method for the $primaryKey property
140: *
141: * @param string $primaryKey
142: * @return \Engine\Model
143: */
144: public function setPrimaryKey($primaryKey)
145: {
146: $this->primaryKey = $primaryKey;
147: return $this;
148: }
149:
150: /**
151: * Helper function the provides the \zsql\Select object with the table name
152: * pre-populated.
153: *
154: * @return \zsql\Select
155: */
156: public function select()
157: {
158: if( !$this->tableName ) {
159: throw new Exception('No table name specified');
160: }
161: return $this->getDatabase()
162: ->select()
163: ->table($this->tableName);
164: }
165:
166: /**
167: * Insert a row into the model's table. If no table name is specified an
168: * exception will be thrown
169: *
170: * @return \zsql\Insert
171: */
172: public function insert()
173: {
174: if( !$this->tableName ) {
175: throw new Exception('No table name specified');
176: }
177: return $this->getDatabase()
178: ->insert()
179: ->table($this->tableName);
180: }
181:
182: /**
183: * Update existing rows in the model's table. If no table name is specified an
184: * exception will be thrown
185: *
186: * @return \zsql\Update
187: */
188: public function update()
189: {
190: if( !$this->tableName ) {
191: throw new Exception('No table name specified');
192: }
193: return $this->getDatabase()
194: ->update()
195: ->table($this->tableName);
196: }
197:
198: /**
199: * Deletes existing rows from the model's table. If no table name is specified
200: * an exception will be thrown
201: *
202: * @return \zsql\Delete
203: */
204: public function delete()
205: {
206: if( !$this->tableName ) {
207: throw new Exception('No table name specified');
208: }
209: return $this->getDatabase()
210: ->delete()
211: ->table($this->tableName);
212: }
213: }
214: