1717 *
1818 * @package AFN-PHP-FRAMEWORK
1919 */
20- class Model
21- {
20+ class Model {
2221
2322 /**
2423 * Stores database id from config file
@@ -49,8 +48,7 @@ class Model
4948 * The credential is chosen according to database id
5049 * @param integer $dbno database id
5150 */
52- public function __construct ($ dbno = 1 )
53- {
51+ public function __construct ($ dbno = 1 ) {
5452 try {
5553 // Check if ROOT_DIR is defined then use it or add it manually for the config file path
5654 if (!defined (ROOT_DIR )) {
@@ -78,8 +76,7 @@ public function __construct($dbno = 1)
7876 * Closes database connection as soon as there are no other references
7977 * to a particular object, or in any order during the shutdown sequence.
8078 */
81- public function __destruct ()
82- {
79+ public function __destruct () {
8380 try {
8481 $ this ->conn = null ; //Closes connection
8582 } catch (PDOException $ e ) {
@@ -92,8 +89,7 @@ public function __destruct()
9289 * @param string $property
9390 * @param mixed $value
9491 */
95- public function __set ($ property , $ value )
96- {
92+ public function __set ($ property , $ value ) {
9793 $ this ->$ property = $ value ;
9894 }
9995
@@ -102,8 +98,7 @@ public function __set($property, $value)
10298 * @param string $property
10399 * @return mixed
104100 */
105- public function __get ($ property )
106- {
101+ public function __get ($ property ) {
107102 if (isset ($ this ->$ property )) {
108103 return $ this ->$ property ;
109104 }
@@ -117,8 +112,7 @@ public function __get($property)
117112 * @param boolean $count The parameter that will decide as to whether or not to calculate record count
118113 * @return boolean
119114 */
120- public function executeOnQuery ($ query , array $ params = [], $ count = false )
121- {
115+ public function executeOnQuery ($ query , array $ params = [], $ count = false ) {
122116 $ stmt = $ this ->conn ->prepare ($ query );
123117 try {
124118 $ stmt ->execute ($ params );
@@ -138,8 +132,7 @@ public function executeOnQuery($query, array $params = [], $count = false)
138132 * @param object $stmt
139133 * @return array
140134 */
141- public function getNumRows ($ stmt )
142- {
135+ public function getNumRows ($ stmt ) {
143136 return (is_resource ($ stmt ) ? $ stmt ->fetchColumn () : 0 );
144137 }
145138
@@ -148,8 +141,7 @@ public function getNumRows($stmt)
148141 * @param string $tableName
149142 * @return boolean or array
150143 */
151- public function getColumnsName (string $ tableName = "" )
152- {
144+ public function getColumnsName (string $ tableName = "" ) {
153145 if (!empty ($ this ->table ) || !empty ($ tableName )) {
154146 $ tname = !empty ($ tableName ) ? $ tableName : $ this ->table ;
155147 $ query = "DESCRIBE " . $ tname ;
@@ -168,8 +160,7 @@ public function getColumnsName(string $tableName = "")
168160 * @param array $array
169161 * @return string
170162 */
171- public function jsonTurkish (array $ array )
172- {
163+ public function jsonTurkish (array $ array ) {
173164 foreach ($ array as $ record ) {
174165 foreach ($ record as $ key => $ og ) {
175166 $ colm [] = '" ' . $ key . '":" ' . $ og . '" ' ;
@@ -187,8 +178,7 @@ public function jsonTurkish(array $array)
187178 * @param integer $id The row id of the expected content in the database
188179 * @return string The Error
189180 */
190- public function refresh ($ id )
191- {
181+ public function refresh ($ id ) {
192182 if (is_numeric ($ id )) {
193183 $ query = "SELECT * FROM " . $ this ->table . " WHERE " . key ($ this ) . " = :id " ;
194184 $ stmt = $ this ->conn ->prepare ($ query );
@@ -214,8 +204,7 @@ public function refresh($id)
214204 * @param array $params The Parameters that will be inserted to the query
215205 * @return string The error or nothing
216206 */
217- public function refreshProcedure ($ query , array $ params = [])
218- {
207+ public function refreshProcedure ($ query , array $ params = []) {
219208 if ($ query != "" ) {
220209 $ stmt = $ this ->conn ->prepare ($ query );
221210 if (!$ stmt ->execute ($ params )) {
@@ -236,8 +225,7 @@ public function refreshProcedure($query, array $params = [])
236225 * Returns property names and property values of child classes as JSON format
237226 * @return string The property names and property values as JSON format
238227 */
239- public function toJson ()
240- {
228+ public function toJson () {
241229 $ classItems = get_object_vars ($ this );
242230 $ json = "{ " ;
243231 foreach ($ classItems as $ key => $ val ) {
@@ -257,8 +245,7 @@ public function toJson()
257245 * by using new data.
258246 * @return int
259247 */
260- public function save ()
261- {
248+ public function save () {
262249 // Get class properties and pass them
263250 $ classitems = get_object_vars ($ this );
264251
@@ -288,7 +275,6 @@ public function save()
288275
289276 // Begin the transaction
290277 //$this->conn->beginTransaction();
291-
292278 // If the id exists, the process is updating. However, if not, the process is inserting.
293279 if (!is_numeric ($ this ->id ) || $ this ->id == 0 ) {
294280 $ sql = "INSERT INTO " . $ this ->table . " ( " . implode (", " , $ dataFields ) . ") VALUES ( " . implode (', ' , $ dataNames ) . ") " ;
@@ -342,21 +328,20 @@ public function save()
342328 * Generates a globally unique identifier
343329 * @return string
344330 */
345- public function customCreateGuid ()
346- {
331+ public function customCreateGuid () {
347332 if (function_exists ('com_create_guid ' )) {
348333 return com_create_guid ();
349334 } else {
350335 mt_srand ((double ) microtime () * 10000 ); // Optional for php 4.2.0 and up.
351336 $ charid = strtoupper (md5 (uniqid (rand (), true )));
352337 $ hyphen = chr (45 ); // "-"
353338 $ uuid = "" // "{"
354- . substr ($ charid , 0 , 8 ) . $ hyphen
355- . substr ($ charid , 8 , 4 ) . $ hyphen
356- . substr ($ charid , 12 , 4 ) . $ hyphen
357- . substr ($ charid , 16 , 4 ) . $ hyphen
358- . substr ($ charid , 20 , 12 )
359- . "" ; // "}"
339+ . substr ($ charid , 0 , 8 ) . $ hyphen
340+ . substr ($ charid , 8 , 4 ) . $ hyphen
341+ . substr ($ charid , 12 , 4 ) . $ hyphen
342+ . substr ($ charid , 16 , 4 ) . $ hyphen
343+ . substr ($ charid , 20 , 12 )
344+ . "" ; // "}"
360345 return $ uuid ;
361346 }
362347 }
@@ -366,8 +351,7 @@ public function customCreateGuid()
366351 * @param type $force 0 permanent deleting, 1 recoverable deleting
367352 * @return boolean The result of operation
368353 */
369- public function delete ($ force = 0 )
370- {
354+ public function delete ($ force = 0 ) {
371355 // Check requested operation and run it
372356 if ($ force == 1 ) {
373357 // Permanent deleting
@@ -399,8 +383,7 @@ public function delete($force = 0)
399383 * Deletes all records in the given table
400384 * @return boolean
401385 */
402- public function deleteAll ()
403- {
386+ public function deleteAll () {
404387 $ sql = "DELETE FROM " . $ this ->table ;
405388 $ stmt = $ this ->conn ->prepare ($ sql );
406389 try {
@@ -419,8 +402,7 @@ public function deleteAll()
419402 * Returns the count of records in the given table
420403 * @return integer The count of records
421404 */
422- public function tableRecordCount ()
423- {
405+ public function tableRecordCount () {
424406 $ query = "SELECT COUNT(*) FROM " . $ this ->table ;
425407 $ result = $ this ->conn ->query ($ query )->fetchColumn ();
426408 return $ result ;
@@ -433,17 +415,21 @@ public function tableRecordCount()
433415 * @param boolean $count
434416 * @return mixed
435417 */
436- public function fetch ($ query , array $ params = [], $ count = false )
437- {
418+ public function fetch ($ query , array $ params = [], $ count = false , $ className = null ) {
438419 $ stmt = $ this ->conn ->prepare ($ query );
420+ if (isset ($ className )) {
421+ $ stmt ->setFetchMode (PDO ::FETCH_CLASS , $ className );
422+ } else {
423+ $ stmt ->setFetchMode (PDO ::FETCH_OBJ );
424+ }
439425 try {
440426 $ stmt ->execute ($ params );
441427 if ($ count == true ) {
442428 $ this ->recordCount = $ stmt ->rowCount ();
443429 } else {
444430 $ this ->recordCount = 0 ;
445431 }
446- return $ stmt ->fetch (PDO :: FETCH_ASSOC );
432+ return $ stmt ->fetch ();
447433 } catch (PDOException $ e ) {
448434 echo $ e ->getMessage ();
449435 return false ;
@@ -457,17 +443,22 @@ public function fetch($query, array $params = [], $count = false)
457443 * @param boolean $count
458444 * @return array
459445 */
460- public function fetchAll ($ query , array $ params = [], $ count = false )
461- {
446+ public function fetchAll ($ query , array $ params = [], $ count = false , $ className = null ) {
462447 $ stmt = $ this ->conn ->prepare ($ query );
448+ if (isset ($ className )) {
449+ $ stmt ->setFetchMode (PDO ::FETCH_CLASS , $ className );
450+ } else {
451+ $ stmt ->setFetchMode (PDO ::FETCH_OBJ );
452+ }
463453 try {
464454 $ stmt ->execute ($ params );
465455 if ($ count == true ) {
466456 $ this ->recordCount = $ stmt ->rowCount ();
467457 } else {
468458 $ this ->recordCount = 0 ;
469459 }
470- return $ stmt ->fetchAll (PDO ::FETCH_ASSOC );
460+
461+ return $ stmt ->fetchAll ();
471462 } catch (PDOException $ e ) {
472463 echo $ e ->getMessage ();
473464 return false ;
@@ -480,8 +471,7 @@ public function fetchAll($query, array $params = [], $count = false)
480471 * @param array $params
481472 * @return boolean
482473 */
483- public function query ($ query , array $ params = [])
484- {
474+ public function query ($ query , array $ params = []) {
485475 $ stmt = $ this ->conn ->prepare ($ query );
486476 try {
487477 return $ stmt ->execute ($ params );
@@ -498,8 +488,7 @@ public function query($query, array $params = [])
498488 * @param boolean $count
499489 * @return boolean
500490 */
501- public function fetchColumn ($ query , array $ params = [], $ count = false )
502- {
491+ public function fetchColumn ($ query , array $ params = [], $ count = false ) {
503492 $ stmt = $ this ->conn ->prepare ($ query );
504493 try {
505494 $ stmt ->execute ($ params );
@@ -514,4 +503,5 @@ public function fetchColumn($query, array $params = [], $count = false)
514503 return false ;
515504 }
516505 }
506+
517507}
0 commit comments