Overview

Classes

  • Ws_logged_inModel
  • Ws_permissionsModel
  • Ws_role_permModel
  • Ws_rolesModel
  • Ws_user_roleModel
  • Ws_userModel
  • WsAuth
  • WsauthController
  • WsChart
  • WsConfig
  • WsController
  • WsDatabase
  • WsForm
  • WsImage
  • WsLocalize
  • WsModel
  • WsModelForm
  • WsModelGridView
  • WsUrl

Functions

  • __autoload
  • callHook
  • WsErrorHandler
  • Overview
  • Class
  1: <?php
  2: /**
  3:  * WsModel
  4:  * Is model part of Model-View-Controller. WsModel is the base
  5:  * class providing the common features needed by data model objects.
  6:  *
  7:  * Example usage:
  8:  *
  9:  * <code>
 10:  * class TestModel extends WsModel
 11:  * {
 12:  *     public function __construct()
 13:  *     {
 14:  *         // autodetect columns, column types, etc
 15:  *         parent::__construct();
 16:  *
 17:  *         // change some column types
 18:  *         // change column type for column
 19:  *         $this->columnType['enter_date'] = 'date_type';
 20:  *         // change column type for other column
 21:  *         $this->columnType['is_checked'] = 'bool_type';
 22:  *         // leave other column types unchanged (autodetected)
 23:  *     }
 24:  * }
 25:  *
 26:  * // use model
 27:  * $m = new TestModel();
 28:  *
 29:  * // get one record with ID 100 from database
 30:  * $m->getOne(100);
 31:  * // show field value for retrived record
 32:  * echo $m->name.'\n';
 33:  *
 34:  * // change value of one column in record with ID 100
 35:  * $m->name = 'New Name';
 36:  * // save changes
 37:  * $m->save();
 38:  *
 39:  * // delete record with ID 100 from database
 40:  * $m->id = 100;
 41:  * $m->delete();
 42:  * // same as above
 43:  * $m->delete(100);
 44:  * </code>
 45:  *
 46:  */
 47: class WsModel extends WsDatabase
 48: {
 49:     /**
 50:      * @var string $className Name of model
 51:      *
 52:      */
 53:     public $className = '';
 54:     /**
 55:      * @var string $tableName Name of database table
 56:      *
 57:      */
 58:     public $tableName = '';
 59:     /**
 60:      * @var string $metaName Long name (description) of database table
 61:      * @see setTableName()
 62:      *
 63:      */
 64:     public $metaName = '';
 65:     /**
 66:      * List of table columns. This list is automaticaly populated during the
 67:      * class initialization.
 68:      *
 69:      * @var array $columns List of table columns
 70:      *
 71:      */
 72:     public $columns = array();
 73:     /**
 74:      * @var string $primary_key Column that is primary key
 75:      *
 76:      */
 77:     public $primary_key = 'id';
 78:     /**
 79:      * @var array $columnCanBeNull List of columns that can store NULL values
 80:      *
 81:      */
 82:     public $columnCanBeNull = array();
 83:     /**
 84:      * @var array $columnType List of column types
 85:      * @todo MySQL/MariaDB don't detect boolean type propperly so you must set
 86:      * type for these columns by hand.
 87:      *
 88:      */
 89:     public $columnType = array();
 90:     /**
 91:      * @var array $columnHeaders List of column headers
 92:      * @see WsModelForm
 93:      * @see WsModelGridView
 94:      *
 95:      */
 96:     public $columnHeaders = array();
 97:     /**
 98:      * List of arrays wich represents relations between two WsModel classes.
 99:      * This list is automaticaly populated during the class initialization
100:      * with regresion that display properti is referencing to foreign column
101:      * name.
102:      *
103:      * Usage:
104:      * <code>
105:      * $foreignKeys = array(
106:      *     'column_name' => array(
107:      *         'table' => 'foreign_table_name',
108:      *         'column' => 'foreign_column_name',
109:      *         'display' => 'foreign_column_that_would_be_used_for_display'
110:      *     )
111:      * );
112:      * </code>
113:      * @var array $foreignKeys
114:      *
115:      */
116:     public $foreignKeys = array();
117:     /**
118:      * @var array $hiddenColumns List of columns that are hidden from user
119:      * @see WsModelForm
120:      * @see WsModelGridView
121:      *
122:      */
123:     public $hiddenColumns = array();
124: 
125: 
126:     public function __construct()
127:     {
128:         parent::__construct();
129: 
130:         /*
131:          * get default database table name from Model name. Table name would
132:          * be same as model without "Model" suffix.
133:          */
134:         $this->className = get_class($this);
135:         $name = strtolower(substr($this->className, 0,
136:             strpos($this->className, 'Model')));
137: 
138:         // get colums and set default names for columns
139:         $this->setTableName($name);
140: 
141:         // at beginning metaName is same as tableName
142:         $this->metaName = $name;
143:     }
144: 
145: 
146:     /**
147:      * Sets name of database table on which model references. Also updates all
148:      * propertyes that hold informations about table columns:
149:      *      $column
150:      *      $columnCanBeNull
151:      *      $columnHeaders
152:      *      $foreignKeys
153:      *      $primaryKey
154:      *
155:      * @param string $name Name of database table
156:      *
157:      */
158:     public function setTableName($name)
159:     {
160:         $this->tableName = $name;
161: 
162:         // get foreign keys from table in PostgreSQL
163:         if (WsConfig::get('db_driver') === 'pgsql') {
164:             $query = '
165: SELECT
166:     att2.attname AS "child_column",
167:     cl.relname AS "parent_table",
168:     att.attname AS "parent_column"
169: FROM
170:     (SELECT
171:         UNNEST(con1.conkey) AS "parent",
172:         UNNEST(con1.confkey) AS "child",
173:         con1.confrelid,
174:         con1.conrelid
175:     FROM
176:         pg_class cl
177:     JOIN pg_namespace ns ON cl.relnamespace = ns.oid
178:     JOIN pg_constraint con1 ON con1.conrelid = cl.oid
179:     WHERE
180:         cl.relname = :table_name
181:             AND con1.contype = \'f\'
182:     ) con
183:     JOIN pg_attribute att on
184:         att.attrelid = con.confrelid AND att.attnum = con.child
185:     JOIN pg_class cl ON cl.oid = con.confrelid
186:     JOIN pg_attribute att2 ON
187:         att2.attrelid = con.conrelid AND att2.attnum = con.parent;
188: ';
189:         // get forign keys from table in MySQL or MariaDB
190:         } else if (WsConfig::get('db_driver') === 'mysql') {
191:             $query = '
192: SELECT
193:     column_name AS "child_column",
194:     referenced_table_name AS "parent_table",
195:     referenced_column_name AS "parent_column"
196: FROM information_schema.key_column_usage
197: WHERE table_name = :table_name
198: AND referenced_table_name IS NOT NULL;
199: ';
200:         } else if (WsConfig::get('db_driver') === 'sqlite') {
201:             $query = 'PRAGMA foreign_key_list(:table_name)';
202:         }
203: 
204:         // clear array of foreign keys
205:         $this->foreignKeys = array();
206: 
207:         $results = $this->query($query, array(
208:             'table_name' => $this->tableName
209:         ));
210: 
211:         // foreign key detection for PostgreSQL and MySQL/MariaDB
212:         if (WsConfig::get('db_driver') !== 'sqlite') {
213:             foreach ($results as $result) {
214:                 $this->foreignKeys[$result['child_column']] = array(
215:                     'table' => $result['parent_table'],
216:                     'column' => $result['parent_column'],
217:                     'display' => $result['parent_column']
218:                 );
219:             }
220:         }
221: 
222:         // get column names, types, nulls
223:         if (WsConfig::get('db_driver') === 'sqlite') {
224:             $query = 'PRAGMA table_info(:table_name)';
225:         } else {
226:             $query = '
227: SELECT column_name, is_nullable, data_type
228: FROM information_schema.columns
229: WHERE table_name= :table_name';
230:         }
231: 
232:         $results2 = $this->query($query, array(
233:             'table_name' => $this->tableName
234:         ));
235: 
236:         // clear all values that are defined before
237:         $this->columns = array();
238:         $this->columnHeaders = array();
239:         $this->columnIsNull = array();
240:         $this->columnType = array();
241: 
242:         foreach ($results2 as $result) {
243:             array_push($this->columns, $result['column_name']);
244:             // create all model variables from column
245:             $this->columnCanBeNull[$result['column_name']] =
246:                 (($result['is_nullable'] =='YES') ? true: false);
247:             // column headers
248:             $this->columnHeaders[$result['column_name']] =
249:                 $this->tableName.'-'.$result['column_name'];
250: 
251:             // column type by database column type
252:             if (stripos($result['data_type'], 'int') !== false) {
253:                 // INTEGER, SMALLINT, BIGINT, TINYINT, MEDIUMINT
254:                 $this->columnType[$result['column_name']] = 'int_type';
255:             } else if (stripos($result['data_type'], 'float') !== false
256:                 or stripos($result['data_type'], 'real') !== false
257:                 or stripos($result['data_type'], 'double') !== false
258:                 or stripos($result['data_type'], 'dec') !== false
259:                 or stripos($result['data_type'], 'fixed') !== false
260:                 or stripos($result['data_type'], 'numeric') !== false
261:             ) {
262:                 // NUMERIC, DOUBLE, REAL FLOAT, DECIMAL, FIXED numeric types
263:                 $this->columnType[$result['column_name']] = 'numeric_type';
264:             } else if (stripos($result['data_type'], 'datetime') !== false
265:                 or stripos($result['data_type'], 'timestamp') !== false
266:             ) {
267:                 // DATETIME or TIMESTAMP type
268:                 $this->columnType[$result['column_name']] = 'timestamp_type';
269:             } else if (stripos($result['data_type'], 'date') !== false) {
270:                 // DATE type
271:                 $this->columnType[$result['column_name']] = 'date_type';
272:             } else if (stripos($result['data_type'], 'time') !== false) {
273:                 // TIME type
274:                 $this->columnType[$result['column_name']] = 'time_type';
275:             } else if (stripos($result['data_type'], 'text') !== false) {
276:                 // TEXT type
277:                 $this->columnType[$result['column_name']] = 'textarea_type';
278:             } else if (stripos($result['data_type'], 'bool') !== false) {
279:                 // BOOLEAN type
280:                 $this->columnType[$result['column_name']] = 'bool_type';
281:             } else {
282:                 // column type by column name
283:                 if (stripos($result['column_name'], 'pass') !== false) {
284:                     // field is password
285:                     $this->columnType[$result['column_name']] = 'password_type';
286:                 } else if (stripos($result['column_name'], 'url') !== false) {
287:                     // field is url address
288:                     $this->columnType[$result['column_name']] = 'url_type';
289:                 } else if (stripos($result['column_name'], 'mail') !== false) {
290:                     // field is e-mail address
291:                     $this->columnType[$result['column_name']] = 'mail_type';
292:                 } else if (stripos($result['column_name'], 'phone') !== false) {
293:                     // field is phone number
294:                     $this->columnType[$result['column_name']] = 'phone_type';
295:                 } else {
296:                     // column type can't be determinate
297:                     $this->columnType[$result['column_name']] = 'misc_type';
298:                 }
299:             }
300:         }
301: 
302:         unset($query, $results, $results2);
303:     }
304: 
305: 
306:     /**
307:      * Check if record, which primary key has value of parameter $id, exists
308:      * in database.
309:      *
310:      * @param integer $id Value of primary key
311:      * @return boolean True if record exists or false if not.
312:      *
313:      */
314:     public function idExists($id)
315:     {
316:         /*
317:          * fetch only record with specific ID
318:          */
319:         if (func_num_args() == 1) {
320:             $id = func_get_arg(0);
321:         } else {
322:             header('HTTP/1.1 500 Internal Server Error');
323:             trigger_error($this->className
324:                 .': invalid number of parameters in idExists()',
325:                 E_USER_ERROR);
326:             return false;
327:         }
328: 
329:         // construct query string
330:         $query = 'SELECT '.$this->primary_key;
331:         $from = $this->tableName;
332:         $where = $this->tableName.'.'.$this->primary_key.' = :id_parameter';
333: 
334:         // final query
335:         $query .= ' FROM '.$from.' WHERE '.$where.' LIMIT 1';
336: 
337:         $result = $this->query($query, array(
338:             'id_parameter' => intval($id)
339:         ));
340: 
341:         // check for one result
342:         if ($this->nRows == 1) {
343:             $result = true;
344:         } else {
345:             $result = false;
346:         }
347: 
348:         unset($query);
349: 
350:         return $result;
351:     }
352: 
353: 
354:     /**
355:      * This method is invoked before deleting a record from database.
356:      *
357:      * @return boolean True on success or false otherwise
358:      *
359:      */
360:     public function beforeDelete()
361:     {
362:         return true;
363:     }
364: 
365: 
366:     /**
367:      * Delete record from database.
368:      *
369:      * @return boolean True on success or false otherwise
370:      *
371:      */
372:     public function delete()
373:     {
374:         /*
375:          * delete only record with specific ID
376:          */
377:         if (func_num_args() == 1) {
378:             $id = func_get_arg(0);
379:         } else if(property_exists($this, 'id')) {
380:             if (isset($this->id)) {
381:                 $id = $this->id;
382:             } else {
383:                 $id = '-1';
384:             }
385:         } else {
386:             $id = '-1';
387:         }
388: 
389:         if ($id != '-1') {
390:             $this->getOne($id);
391:         }
392: 
393:         if (!$this->beforeDelete()) {
394:             return false;
395:         }
396: 
397:         if ($id != '-1') {
398:             // prepare query for delete operation
399:             $query = 'DELETE FROM '.$this->tableName.' WHERE '
400:                 .$this->primary_key.'=:id';
401:             if($this->execute($query, array('id' => $id))) {
402:                 unset($query, $id);
403:                 return true;
404:             } else {
405:                 header('HTTP/1.1 500 Internal Server Error');
406:                 trigger_error($this->className
407:                     .': error occurred while deleting record from model',
408:                     E_USER_ERROR);
409:                 unset($query, $id);
410:                 return false;
411:             }
412:         }
413:     }
414: 
415: 
416:     /**
417:      * This method is invoked before saving a record.
418:      *
419:      * @return boolean True on success or false otherwise
420:      *
421:      */
422:     public function beforeSave()
423:     {
424:         return true;
425:     }
426: 
427: 
428:     /**
429:      * Saves record to database. If primary_key column with specified value
430:      * allready exists then perform update of record and if not then perform
431:      * insert of new record.
432:      *
433:      * @return boolean True on success or false on error
434:      *
435:      */
436:     public function save()
437:     {
438:         $query = '';
439:         $values = array(); // values for fields
440:         $fields = '';
441:         $field_val = '';
442: 
443:         /*
444:          * fetch only record with specific ID
445:          */
446:         if(property_exists($this, 'id')) {
447:             if (isset($this->id)) {
448:                 $id = $this->id;
449:             } else {
450:                 $id = '-1';
451:             }
452:         } else {
453:             $id = '-1';
454:         }
455: 
456:         if(!$this->beforeSave()) {
457:             return false;
458:         }
459: 
460:         if ($this->idExists($id)) {
461:             // ID is set, perform UPDATE
462:             $updates = '';
463:             foreach ($this->columns as $column) {
464:                 if (property_exists($this, $column)) {
465:                     if(trim($this->$column) !== '') {
466:                         $updates .= $column.' = :'.$column.', ';
467:                         $values[$column] = $this->$column;
468:                     }
469:                 }
470:             }
471: 
472:             // remove last "," from updates
473:             $updates = substr($updates, 0, -2);
474: 
475:             // query for UPDATE
476:             $query = 'UPDATE '.$this->tableName.' SET '
477:                 .$updates.' WHERE '.$this->primary_key.'=:id';
478: 
479:             unset($updates);
480:         } else {
481:             // ID is not set, perform INSERT
482:             foreach ($this->columns as $column) {
483:                 if (property_exists($this, $column)) {
484:                     if(trim($this->$column) !== '') {
485:                         $fields .= $column.', ';
486:                         $field_val .= ':'.$column.', ';
487:                         $values[$column] = $this->$column;
488:                     }
489:                 }
490:             }
491: 
492:             // remove last ","
493:             $fields = substr($fields, 0, -2);
494:             $field_val = substr($field_val, 0, -2);
495: 
496:             // query for INSERT
497:             $query = 'INSERT INTO '.$this->tableName.' ('.$fields
498:                 .') VALUES ('.$field_val.')';
499: 
500:             unset($fields, $field_val);
501:         }
502: 
503:         if($this->execute($query, $values)) {
504:             unset($query, $values);
505:             return true;
506:         } else {
507:             header('HTTP/1.1 500 Internal Server Error');
508:             trigger_error($this->className
509:                 .': error occurred while saving record to model',
510:                 E_USER_ERROR);
511:             unset($query, $values);
512:             return false;
513:         }
514:     }
515: 
516: 
517:     /**
518:      * Get one record from table.
519:      *
520:      * @param integer $id Value of primary_key column which will be returned
521:      * @return array $results if record is found or false
522:      *
523:      */
524:     public function getOne()
525:     {
526:         /*
527:          * fetch only record with specific ID
528:          */
529:         if(property_exists($this, 'id')) {
530:             if (isset($this->id)) {
531:                 $id = $this->id;
532:             } else {
533:                 $this->nRows = 0;
534:                 return false;
535:             }
536:         } else if (func_num_args() == 1) {
537:             $id = func_get_arg(0);
538:         } else {
539:             $this->nRows = 0;
540:             return false;
541:         }
542: 
543:         // construct query string
544:         $query = 'SELECT ';
545:         $from = $this->tableName.', ';
546:         $where = $this->tableName.'.'.$this->primary_key.'=:id_parameter AND ';
547:         foreach ($this->columns as $column) {
548:             // check if column is Foreign Key
549:             if (isset($this->foreignKeys[$column])) {
550:                 $foreign_table = strtolower(
551:                     $this->foreignKeys[$column]['table']);
552:                 /* self referencing foreign keys are treated
553:                  * as standard columns
554:                  */
555:                 if ($foreign_table == $this->tableName) {
556:                     $foreign_id = isset($this->foreignKeys[$column]['column']) ?
557:                         $this->foreignKeys[$column]['column'] : 'id';
558:                     $display = isset($this->foreignKeys[$column]['display']) ?
559:                         $this->foreignKeys[$column]['display'] : $column;
560:                     $query .= $foreign_table.'_parent.'
561:                         .$display.' AS '.$column.', ';
562:                     $from .= $foreign_table.' '.$foreign_table.'_parent, ';
563:                     $where .= $this->tableName.'.'.$column.'='
564:                         .$foreign_table.'_parent.'.$foreign_id.' AND ';
565:                 } else {
566:                     $foreign_id = isset($this->foreignKeys[$column]['column']) ?
567:                         $this->foreignKeys[$column]['column'] : 'id';
568:                     $display = isset($this->foreignKeys[$column]['display']) ?
569:                         $this->foreignKeys[$column]['display'] : $foreign_id;
570:                     $query .= $foreign_table.'.'.$display.' AS '.$column.', ';
571:                     $from .= $foreign_table.', ';
572:                     $where .= $this->tableName.'.'.$column.'='
573:                         .$foreign_table.'.'.$foreign_id.' AND ';
574:                 }
575:             } else {
576:                 // standard column */
577:                 $query .= $this->tableName.'.'.$column.' AS '.$column.', ';
578:             }
579:         }
580:         // remove last coma from $query
581:         $query2 = substr($query, 0, -2);
582:         // remove last coma from $from
583:         $from = substr($from, 0, -2);
584:         // remove last AND from $where
585:         $where = substr($where, 0, -5);
586: 
587:         // final query
588:         $query2 .= ' FROM '.$from.' WHERE '.$where.' LIMIT 1';
589:         $result = $this->query($query2, array('id_parameter' => $id));
590: 
591:         // check for one result
592:         if ($this->nRows == 1) {
593:             // fill class properties
594:             foreach ($result[0] as $key => $value) {
595:                 $this->$key = $value;
596:             }
597:         } else {
598:             $this->nRows = 0;
599:             $result = false;
600:         }
601: 
602:         unset($query, $query2);
603: 
604:         return $result[0];
605:     }
606: 
607: 
608:     /**
609:      * Gets all records from table
610:      *
611:      * @param string $order represents in which order result should be returned
612:      * @param integer $limit limits number of results
613:      * @param integer $offset starting record for return
614:      * @return array $results if records exists or false
615:      *
616:      */
617:     public function getAll($order='', $limit=0, $offset=0)
618:     {
619:         // construct query string
620:         $query = 'SELECT ';
621:         $from = $this->tableName.', ';
622:         $where = '';
623:         foreach ($this->columns as $column) {
624:             // check if column is Foreign Key
625:             if (isset($this->foreignKeys[$column])) {
626:                 $foreign_table = strtolower(
627:                     $this->foreignKeys[$column]['table']);
628:                 /* self referencing foreign keys are
629:                  * treated as standard columns
630:                  */
631:                 if ($foreign_table == $this->tableName) {
632:                     $foreign_id = isset($this->foreignKeys[$column]['column']) ?
633:                         $this->foreignKeys[$column]['column'] : 'id';
634:                     $display = isset($this->foreignKeys[$column]['display']) ?
635:                         $this->foreignKeys[$column]['display'] : $column;
636:                     $query .= $foreign_table.'_parent.'
637:                         .$display.' AS '.$column.', ';
638:                     $from .= $foreign_table.' '.$foreign_table.'_parent, ';
639:                     $where .= $this->tableName.'.'.$column.'='
640:                         .$foreign_table.'_parent.'.$foreign_id.' AND ';
641:                 } else {
642:                     $foreign_id = isset($this->foreignKeys[$column]['column']) ?
643:                         $this->foreignKeys[$column]['column'] : 'id';
644:                     $display = isset($this->foreignKeys[$column]['display']) ?
645:                         $this->foreignKeys[$column]['display'] : $foreign_id;
646:                     $query .= $foreign_table.'.'.$display.' AS '.$column.', ';
647:                     $from .= $foreign_table.', ';
648:                     $where .= $this->tableName.'.'.$column.'='
649:                         .$foreign_table.'.'.$foreign_id.' AND ';
650:                 }
651:             } else {
652:                 // standard column
653:                 $query .= $this->tableName.'.'.$column.' AS '.$column.', ';
654:             }
655:         }
656:         // remove last coma from $query
657:         $query2 = substr($query, 0, -2);
658:         // remove last coma from $from
659:         $from = substr($from, 0, -2);
660: 
661:         // final query
662:         $query2 .= ' FROM '.$from;
663: 
664:         if ($where != '') {
665:             // remove last AND from $where
666:             $where = substr($where, 0, -5);
667:             $query2 .= ' WHERE '.$where;
668:         }
669: 
670:         // ORDER BY clausule
671:         if ($order != '') {
672:             $query2 .= ' ORDER BY '.$order;
673:         }
674: 
675:         // LIMIT clausule
676:         if ($limit > 0) {
677:             $query2 .= ' LIMIT '.$limit;
678:         }
679: 
680:         // offset clausule
681:         if ($offset > 0) {
682:             $query2 .= ' OFFSET '.$offset;
683:         }
684: 
685:         $result = $this->query($query2);
686: 
687:         unset($query, $query2);
688: 
689:         // check for any result
690:         if ($this->nRows >= 1) {
691:             return $result;
692:         } else {
693:             return false;
694:         }
695:     }
696: 
697: 
698:     /**
699:      * Gets all records from model that satisfaid specific condition.
700:      *
701:      * @param string $condition SQL condition
702:      * @param string $order represents in which order result should be returned
703:      * @param integer $limit limits number of results
704:      * @param integer $offset starting record for return
705:      * @return array $results if records exists or false
706:      *
707:      */
708:     public function search($condition='', $order='', $limit=0, $offset=0)
709:     {
710:         $query = 'SELECT ';
711:         $from = $this->tableName.', ';
712:         $where = '';
713:         foreach ($this->columns as $column) {
714:             // check if column is Foreign Key
715:             if (isset($this->foreignKeys[$column])) {
716:                 $foreign_table = strtolower(
717:                     $this->foreignKeys[$column]['table']);
718:                 /* self referencing foreign keys are
719:                  * treated as standard columns
720:                  */
721:                 if ($foreign_table == $this->tableName) {
722:                     $foreign_id = isset($this->foreignKeys[$column]['column']) ?
723:                         $this->foreignKeys[$column]['column'] : 'id';
724:                     $display = isset($this->foreignKeys[$column]['display']) ?
725:                         $this->foreignKeys[$column]['display'] : $column;
726:                     $query .= $foreign_table.'_parent.'
727:                         .$display.' AS '.$column.', ';
728:                     $from .= $foreign_table.' '.$foreign_table.'_parent, ';
729:                     $where .= $this->tableName.'.'.$column.'='
730:                         .$foreign_table.'_parent.'.$foreign_id.' AND ';
731:                 } else {
732:                     $foreign_id = isset($this->foreignKeys[$column]['column']) ?
733:                         $this->foreignKeys[$column]['column'] : 'id';
734:                     $display = isset($this->foreignKeys[$column]['display']) ?
735:                         $this->foreignKeys[$column]['display'] : $foreign_id;
736:                     $query .= $foreign_table.'.'.$display.' AS '.$column.', ';
737:                     $from .= $foreign_table.', ';
738:                     $where .= $this->tableName.'.'.$column.'='
739:                         .$foreign_table.'.'.$foreign_id.' AND ';
740:                 }
741:             } else {
742:                 // standard column
743:                 $query .= $this->tableName.'.'.$column.' AS '.$column.', ';
744:             }
745:         }
746:         // remove last coma from $query
747:         $query2 = substr($query, 0, -2);
748:         // remove last coma from $from
749:         $from = substr($from, 0, -2);
750: 
751:         // final query
752:         $query2 .= ' FROM '.$from;
753: 
754:         // WHERE
755:         if ($where != '') {
756:             // remove last AND from $where
757:             $where = substr($where, 0, -5);
758:             $query2 .= ' WHERE '.$where;
759:             if ($condition != '') {
760:                 $query2 .= ' AND '.$condition;
761:             }
762:         } else {
763:             if ($condition != '') {
764:                 $query2 .= ' WHERE '.$condition;
765:             }
766:         }
767: 
768:         // ORDER BY clausule
769:         if ($order != '') {
770:             $query2 .= ' ORDER BY '.$order;
771:         }
772: 
773:         // LIMIT clausule
774:         if ($limit > 0) {
775:             $query2 .= ' LIMIT '.$limit;
776:         }
777: 
778:         // offset clausule
779:         if ($offset > 0) {
780:             $query2 .= ' OFFSET '.$offset;
781:         }
782: 
783:         $result = $this->query($query2);
784: 
785:         unset($query, $query2);
786: 
787:         // check for any result
788:         if ($this->nRows >= 1) {
789:         // check for one result
790:         // fill class properties with first result row
791:             foreach ($result[0] as $key => $value) {
792:                 $this->$key = $value;
793:             }
794:             return $result;
795:         } else {
796:             return false;
797:         }
798:     }
799: 
800: 
801:     /**
802:      * Gets next ID value from model
803:      *
804:      * @return integer $next_id
805:      *
806:      */
807:     public function getNextId()
808:     {
809: 
810:         $query = 'SELECT COALESCE(MAX('.$this->primary_key
811:             .'), 0) + 1 AS next_id FROM '.$this->tableName;
812:         $result = $this->query($query);
813: 
814:         $next_id = intval($result[0]['next_id']);
815: 
816:         unset ($query, $result);
817: 
818:         return $next_id;
819:     }
820: }
821: 
API documentation generated by ApiGen