blob: c3279e8c3ed39440e39e0417bc4124bdfe8f9524 [file] [log] [blame]
Derek Allard09de1852007-02-14 01:35:56 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allard09de1852007-02-14 01:35:56 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allard09de1852007-02-14 01:35:56 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Active Record Class
20 *
21 * This is the platform-independent base Active Record implementation class.
22 *
23 * @package CodeIgniter
24 * @subpackage Drivers
25 * @category Database
26 * @author Rick Ellis
27 * @link http://www.codeigniter.com/user_guide/database/
28 */
29class CI_DB_active_record extends CI_DB_driver {
30
31 var $ar_select = array();
32 var $ar_distinct = FALSE;
33 var $ar_from = array();
34 var $ar_join = array();
35 var $ar_where = array();
36 var $ar_like = array();
37 var $ar_groupby = array();
38 var $ar_having = array();
39 var $ar_limit = FALSE;
40 var $ar_offset = FALSE;
41 var $ar_order = FALSE;
42 var $ar_orderby = array();
43 var $ar_set = array();
Derek Allard80dd7022007-12-18 23:55:06 +000044 var $ar_wherein = array();
45
Derek Allard09de1852007-02-14 01:35:56 +000046
47
48 /**
49 * Select
50 *
51 * Generates the SELECT portion of the query
52 *
53 * @access public
54 * @param string
55 * @return object
56 */
57 function select($select = '*')
58 {
59 if (is_string($select))
60 {
61 $select = explode(',', $select);
62 }
63
64 foreach ($select as $val)
65 {
66 $val = trim($val);
67
68 if ($val != '')
69 $this->ar_select[] = $val;
70 }
71 return $this;
72 }
73
74 // --------------------------------------------------------------------
75
76 /**
77 * DISTINCT
78 *
79 * Sets a flag which tells the query string compiler to add DISTINCT
80 *
81 * @access public
82 * @param bool
83 * @return object
84 */
85 function distinct($val = TRUE)
86 {
87 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
88 return $this;
89 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * From
95 *
96 * Generates the FROM portion of the query
97 *
98 * @access public
99 * @param mixed can be a string or array
100 * @return object
101 */
102 function from($from)
103 {
104 foreach ((array)$from as $val)
105 {
106 $this->ar_from[] = $this->dbprefix.$val;
107 }
108 return $this;
109 }
110
111 // --------------------------------------------------------------------
112
113 /**
114 * Join
115 *
116 * Generates the JOIN portion of the query
117 *
118 * @access public
119 * @param string
120 * @param string the join condition
121 * @param string the type of join
122 * @return object
123 */
124 function join($table, $cond, $type = '')
125 {
126 if ($type != '')
127 {
128 $type = strtoupper(trim($type));
129
130 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
131 {
132 $type = '';
133 }
134 else
135 {
136 $type .= ' ';
137 }
138 }
139
140 if ($this->dbprefix)
141 {
142 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
143 }
144
145 // If a DB prefix is used we might need to add it to the column names
146 if ($this->dbprefix)
147 {
148 // First we remove any existing prefixes in the condition to avoid duplicates
149 $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
150
151 // Next we add the prefixes to the condition
152 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
153 }
154
155 $this->ar_join[] = $type.'JOIN '.$this->dbprefix.$table.' ON '.$cond;
156 return $this;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Where
163 *
164 * Generates the WHERE portion of the query. Separates
165 * multiple calls with AND
166 *
167 * @access public
168 * @param mixed
169 * @param mixed
170 * @return object
171 */
172 function where($key, $value = NULL)
173 {
174 return $this->_where($key, $value, 'AND ');
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * OR Where
181 *
182 * Generates the WHERE portion of the query. Separates
183 * multiple calls with OR
184 *
185 * @access public
186 * @param mixed
187 * @param mixed
188 * @return object
189 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000190 function or_where($key, $value = NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000191 {
192 return $this->_where($key, $value, 'OR ');
193 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000194
195 // --------------------------------------------------------------------
196
197 /**
198 * orwhere() is an alias of or_where()
199 * this function is here for backwards compatibility, as
200 * orwhere() has been deprecated
201 */
202 function orwhere($key, $value = NULL)
203 {
204 return $this->or_where($key, $value);
205 }
Derek Allard09de1852007-02-14 01:35:56 +0000206
207 // --------------------------------------------------------------------
208
209 /**
210 * Where
211 *
212 * Called by where() or orwhere()
213 *
214 * @access private
215 * @param mixed
216 * @param mixed
217 * @param string
218 * @return object
219 */
220 function _where($key, $value = NULL, $type = 'AND ')
221 {
222 if ( ! is_array($key))
223 {
224 $key = array($key => $value);
225 }
226
227 foreach ($key as $k => $v)
228 {
229 $prefix = (count($this->ar_where) == 0) ? '' : $type;
230
231 if ( ! is_null($v))
232 {
233 if ( ! $this->_has_operator($k))
234 {
235 $k .= ' =';
236 }
237
238 $v = ' '.$this->escape($v);
239 }
240
241 $this->ar_where[] = $prefix.$k.$v;
242 }
243 return $this;
244 }
Derek Allard80dd7022007-12-18 23:55:06 +0000245
246 // --------------------------------------------------------------------
247
248 /**
249 * Where_in
250 *
251 * Generates a WHERE field IN ('item', 'item') SQL query
252 *
253 * @access public
254 * @param string The field to search
255 * @param array The values searched on
256 * @param string
257 * @return object
258 */
259 function where_in($key = NULL, $values = NULL, $type = 'and')
260 {
261 if ($key === NULL || !is_array($values))
262 {
263 return;
264 }
265
266 $type = (strtolower($type) == 'or') ? ' OR ' : ' AND ';
267
268 foreach ($values as $value)
269 {
270 $this->ar_wherein[] = $this->escape($value);
271 }
272
273 $prefix = (count($this->ar_where) == 0) ? '' : $type;
274
275 $this->ar_where[] = $prefix.$key. " IN (" . implode(", ", $this->ar_wherein) . ") ";
276
277 return $this;
278 }
279
Derek Allard09de1852007-02-14 01:35:56 +0000280 // --------------------------------------------------------------------
281
282 /**
283 * Like
284 *
285 * Generates a %LIKE% portion of the query. Separates
286 * multiple calls with AND
287 *
288 * @access public
289 * @param mixed
290 * @param mixed
291 * @return object
292 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000293 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000294 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000295 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * OR Like
302 *
303 * Generates a %LIKE% portion of the query. Separates
304 * multiple calls with OR
305 *
306 * @access public
307 * @param mixed
308 * @param mixed
309 * @return object
310 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000311 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000312 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000313 return $this->_like($field, $match, 'OR ', $side);
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * orlike() is an alias of or_like()
320 * this function is here for backwards compatibility, as
321 * orlike() has been deprecated
322 */
323 function orlike($field, $match = '', $side = 'both')
324 {
325 return $this->orlike($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000326 }
327
328 // --------------------------------------------------------------------
329
330 /**
331 * Like
332 *
333 * Called by like() or orlike()
334 *
335 * @access private
336 * @param mixed
337 * @param mixed
338 * @param string
339 * @return object
340 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000341 function _like($field, $match = '', $type = 'AND ', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000342 {
343 if ( ! is_array($field))
344 {
345 $field = array($field => $match);
346 }
347
348 foreach ($field as $k => $v)
349 {
350 $prefix = (count($this->ar_like) == 0) ? '' : $type;
351
352 $v = $this->escape_str($v);
Derek Allard218e2bc2007-12-17 21:18:14 +0000353
354 if ($side == 'before')
355 {
356 $this->ar_like[] = $prefix." $k LIKE '%{$v}'";
357 }
358 elseif ($side == 'after')
359 {
360 $this->ar_like[] = $prefix." $k LIKE '{$v}%'";
361 }
362 else
363 {
364 $this->ar_like[] = $prefix." $k LIKE '%{$v}%'";
365 }
Derek Allard09de1852007-02-14 01:35:56 +0000366 }
367 return $this;
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * GROUP BY
374 *
375 * @access public
376 * @param string
377 * @return object
378 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000379 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000380 {
381 if (is_string($by))
382 {
383 $by = explode(',', $by);
384 }
385
386 foreach ($by as $val)
387 {
388 $val = trim($val);
389
390 if ($val != '')
391 $this->ar_groupby[] = $val;
392 }
393 return $this;
394 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000395
396 // --------------------------------------------------------------------
397
398 /**
399 * groupby() is an alias of group_by()
400 * this function is here for backwards compatibility, as
401 * groupby() has been deprecated
402 */
403 function groupby($by)
404 {
405 return $this->group_by($by);
406 }
407
Derek Allard09de1852007-02-14 01:35:56 +0000408 // --------------------------------------------------------------------
409
410 /**
411 * Sets the HAVING value
412 *
413 * Separates multiple calls with AND
414 *
415 * @access public
416 * @param string
417 * @param string
418 * @return object
419 */
420 function having($key, $value = '')
421 {
422 return $this->_having($key, $value, 'AND ');
423 }
424
425 // --------------------------------------------------------------------
426
427 /**
428 * Sets the OR HAVING value
429 *
430 * Separates multiple calls with OR
431 *
432 * @access public
433 * @param string
434 * @param string
435 * @return object
436 */
437 function orhaving($key, $value = '')
438 {
439 return $this->_having($key, $value, 'OR ');
440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * Sets the HAVING values
446 *
447 * Called by having() or orhaving()
448 *
449 * @access private
450 * @param string
451 * @param string
452 * @return object
453 */
454 function _having($key, $value = '', $type = 'AND ')
455 {
456 if ( ! is_array($key))
457 {
458 $key = array($key => $value);
459 }
460
461 foreach ($key as $k => $v)
462 {
463 $prefix = (count($this->ar_having) == 0) ? '' : $type;
464
465 if ($v != '')
466 {
467 $v = ' '.$this->escape($v);
468 }
469
470 $this->ar_having[] = $prefix.$k.$v;
471 }
472 return $this;
473 }
474
475 // --------------------------------------------------------------------
476
477 /**
478 * Sets the ORDER BY value
479 *
480 * @access public
481 * @param string
482 * @param string direction: asc or desc
483 * @return object
484 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000485 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000486 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000487 if (strtolower($direction) == 'random')
488 {
489 $orderby = ''; // Random results want or don't need a field name
490 $direction = $this->_random_keyword;
491 }
492 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000493 {
Derek Allard92782492007-08-10 11:26:01 +0000494 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000495 }
496
497 $this->ar_orderby[] = $orderby.$direction;
498 return $this;
499 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000500
Derek Allard218e2bc2007-12-17 21:18:14 +0000501 // --------------------------------------------------------------------
502
503 /**
504 * orderby() is an alias of order_by()
505 * this function is here for backwards compatibility, as
506 * orderby() has been deprecated
507 */
508 function orderby($orderby, $direction = '')
509 {
510 return $this->order_by($orderby, $direction);
511 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000512
Derek Allard09de1852007-02-14 01:35:56 +0000513 // --------------------------------------------------------------------
514
515 /**
516 * Sets the LIMIT value
517 *
518 * @access public
519 * @param integer the limit value
520 * @param integer the offset value
521 * @return object
522 */
523 function limit($value, $offset = '')
524 {
525 $this->ar_limit = $value;
526
527 if ($offset != '')
528 $this->ar_offset = $offset;
529
530 return $this;
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Sets the OFFSET value
537 *
538 * @access public
539 * @param integer the offset value
540 * @return object
541 */
542 function offset($value)
543 {
544 $this->ar_offset = $value;
545 return $this;
546 }
547
548 // --------------------------------------------------------------------
549
550 /**
551 * The "set" function. Allows key/value pairs to be set for inserting or updating
552 *
553 * @access public
554 * @param mixed
555 * @param string
556 * @return object
557 */
558 function set($key, $value = '')
559 {
560 $key = $this->_object_to_array($key);
561
562 if ( ! is_array($key))
563 {
564 $key = array($key => $value);
565 }
566
567 foreach ($key as $k => $v)
568 {
569 $this->ar_set[$k] = $this->escape($v);
570 }
571
572 return $this;
573 }
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Get
579 *
580 * Compiles the select statement based on the other functions called
581 * and runs the query
582 *
583 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +0000584 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +0000585 * @param string the limit clause
586 * @param string the offset clause
587 * @return object
588 */
589 function get($table = '', $limit = null, $offset = null)
590 {
591 if ($table != '')
592 {
593 $this->from($table);
594 }
595
596 if ( ! is_null($limit))
597 {
598 $this->limit($limit, $offset);
599 }
600
601 $sql = $this->_compile_select();
602
603 $result = $this->query($sql);
604 $this->_reset_select();
605 return $result;
606 }
607
608 // --------------------------------------------------------------------
609
610 /**
Derek Allard694b5b82007-12-18 15:58:03 +0000611 * "Count All Results" query
612 *
613 * Generates a platform-specific query string that counts all records
614 * returned by an Active Record query.
615 *
616 * @access public
617 * @param string
618 * @return string
619 */
620 function count_all_results($table = '')
621 {
622 if ($table != '')
623 {
624 $this->from($table);
625 }
626
Derek Allard6ddb5a12007-12-18 17:22:50 +0000627 $sql = $this->_compile_select($this->_count_string);
Derek Allard694b5b82007-12-18 15:58:03 +0000628
629 $query = $this->query($sql);
630 $this->_reset_select();
631
632 if ($query->num_rows() == 0)
633 {
634 return '0';
635 }
636
637 $row = $query->row();
638 return $row->numrows;
639 }
640
641 // --------------------------------------------------------------------
642
643 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000644 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +0000645 *
646 * Allows the where clause, limit and offset to be added directly
647 *
648 * @access public
649 * @param string the where clause
650 * @param string the limit clause
651 * @param string the offset clause
652 * @return object
653 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000654 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +0000655 {
656 if ($table != '')
657 {
658 $this->from($table);
659 }
660
661 if ( ! is_null($where))
662 {
663 $this->where($where);
664 }
665
666 if ( ! is_null($limit))
667 {
668 $this->limit($limit, $offset);
669 }
670
671 $sql = $this->_compile_select();
672
673 $result = $this->query($sql);
674 $this->_reset_select();
675 return $result;
676 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000677
678 // --------------------------------------------------------------------
679
680 /**
681 * getwhere() is an alias of get_where()
682 * this function is here for backwards compatibility, as
683 * getwhere() has been deprecated
684 */
685 function getwhere($table = '', $where = null, $limit = null, $offset = null)
686 {
687 return $this->get_where($table, $where, $limit, $offset);
688 }
Derek Allard09de1852007-02-14 01:35:56 +0000689
690 // --------------------------------------------------------------------
691
692 /**
693 * Insert
694 *
695 * Compiles an insert string and runs the query
696 *
697 * @access public
698 * @param string the table to retrieve the results from
699 * @param array an associative array of insert values
700 * @return object
701 */
702 function insert($table = '', $set = NULL)
703 {
704 if ( ! is_null($set))
705 {
706 $this->set($set);
707 }
708
709 if (count($this->ar_set) == 0)
710 {
711 if ($this->db_debug)
712 {
713 return $this->display_error('db_must_use_set');
714 }
715 return FALSE;
716 }
717
718 if ($table == '')
719 {
720 if ( ! isset($this->ar_from[0]))
721 {
722 if ($this->db_debug)
723 {
724 return $this->display_error('db_must_set_table');
725 }
726 return FALSE;
727 }
728
729 $table = $this->ar_from[0];
730 }
731
732 $sql = $this->_insert($this->dbprefix.$table, array_keys($this->ar_set), array_values($this->ar_set));
733
734 $this->_reset_write();
735 return $this->query($sql);
736 }
737
738 // --------------------------------------------------------------------
739
740 /**
741 * Update
742 *
743 * Compiles an update string and runs the query
744 *
745 * @access public
746 * @param string the table to retrieve the results from
747 * @param array an associative array of update values
748 * @param mixed the where clause
749 * @return object
750 */
751 function update($table = '', $set = NULL, $where = null)
752 {
753 if ( ! is_null($set))
754 {
755 $this->set($set);
756 }
757
758 if (count($this->ar_set) == 0)
759 {
760 if ($this->db_debug)
761 {
762 return $this->display_error('db_must_use_set');
763 }
764 return FALSE;
765 }
766
767 if ($table == '')
768 {
769 if ( ! isset($this->ar_from[0]))
770 {
771 if ($this->db_debug)
772 {
773 return $this->display_error('db_must_set_table');
774 }
775 return FALSE;
776 }
777
778 $table = $this->ar_from[0];
779 }
780
781 if ($where != null)
782 {
783 $this->where($where);
784 }
785
786 $sql = $this->_update($this->dbprefix.$table, $this->ar_set, $this->ar_where);
787
788 $this->_reset_write();
789 return $this->query($sql);
790 }
791
792 // --------------------------------------------------------------------
793
794 /**
795 * Delete
796 *
797 * Compiles a delete string and runs the query
798 *
799 * @access public
800 * @param string the table to retrieve the results from
801 * @param mixed the where clause
802 * @return object
803 */
804 function delete($table = '', $where = '')
805 {
806 if ($table == '')
807 {
808 if ( ! isset($this->ar_from[0]))
809 {
810 if ($this->db_debug)
811 {
812 return $this->display_error('db_must_set_table');
813 }
814 return FALSE;
815 }
816
817 $table = $this->ar_from[0];
818 }
819
820 if ($where != '')
821 {
822 $this->where($where);
823 }
824
825 if (count($this->ar_where) == 0)
826 {
827 if ($this->db_debug)
828 {
829 return $this->display_error('db_del_must_use_where');
830 }
831 return FALSE;
832 }
833
834 $sql = $this->_delete($this->dbprefix.$table, $this->ar_where);
835
836 $this->_reset_write();
837 return $this->query($sql);
838 }
839
840 // --------------------------------------------------------------------
841
842 /**
843 * Use Table - DEPRECATED
844 *
845 * @deprecated use $this->db->from instead
846 */
847 function use_table($table)
848 {
849 return $this->from($table);
850 return $this;
851 }
852
853 // --------------------------------------------------------------------
854
855 /**
Derek Allard09de1852007-02-14 01:35:56 +0000856 * Tests whether the string has an SQL operator
857 *
858 * @access private
859 * @param string
860 * @return bool
861 */
862 function _has_operator($str)
863 {
864 $str = trim($str);
865 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
866 {
867 return FALSE;
868 }
869
870 return TRUE;
871 }
872
873 // --------------------------------------------------------------------
874
875 /**
876 * Compile the SELECT statement
877 *
878 * Generates a query string based on which functions were used.
879 * Should not be called directly. The get() function calls it.
880 *
881 * @access private
882 * @return string
883 */
Derek Allard694b5b82007-12-18 15:58:03 +0000884 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +0000885 {
886 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
887
888 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
889
Derek Allard694b5b82007-12-18 15:58:03 +0000890 if ($select_override !== FALSE)
891 {
892 $sql = $select_override;
893 }
894
Derek Allard09de1852007-02-14 01:35:56 +0000895 if (count($this->ar_from) > 0)
896 {
897 $sql .= "\nFROM ";
898 $sql .= implode(', ', $this->ar_from);
899 }
900
901 if (count($this->ar_join) > 0)
902 {
903 $sql .= "\n";
904 $sql .= implode("\n", $this->ar_join);
905 }
906
907 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
908 {
909 $sql .= "\nWHERE ";
910 }
911
912 $sql .= implode("\n", $this->ar_where);
913
914 if (count($this->ar_like) > 0)
915 {
916 if (count($this->ar_where) > 0)
917 {
918 $sql .= " AND ";
919 }
920
921 $sql .= implode("\n", $this->ar_like);
922 }
923
924 if (count($this->ar_groupby) > 0)
925 {
926 $sql .= "\nGROUP BY ";
927 $sql .= implode(', ', $this->ar_groupby);
928 }
929
930 if (count($this->ar_having) > 0)
931 {
932 $sql .= "\nHAVING ";
933 $sql .= implode("\n", $this->ar_having);
934 }
935
936 if (count($this->ar_orderby) > 0)
937 {
938 $sql .= "\nORDER BY ";
939 $sql .= implode(', ', $this->ar_orderby);
940
941 if ($this->ar_order !== FALSE)
942 {
943 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
944 }
945 }
946
947 if (is_numeric($this->ar_limit))
948 {
949 $sql .= "\n";
950 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
951 }
952
953 return $sql;
954 }
955
956 // --------------------------------------------------------------------
957
958 /**
959 * Object to Array
960 *
961 * Takes an object as input and converts the class variables to array key/vals
962 *
963 * @access public
964 * @param object
965 * @return array
966 */
967 function _object_to_array($object)
968 {
969 if ( ! is_object($object))
970 {
971 return $object;
972 }
973
974 $array = array();
975 foreach (get_object_vars($object) as $key => $val)
976 {
977 if ( ! is_object($val) AND ! is_array($val))
978 {
979 $array[$key] = $val;
980 }
981 }
982
983 return $array;
984 }
985
986 // --------------------------------------------------------------------
987
988 /**
989 * Resets the active record values. Called by the get() function
990 *
991 * @access private
992 * @return void
993 */
994 function _reset_select()
995 {
996 $this->ar_select = array();
997 $this->ar_distinct = FALSE;
998 $this->ar_from = array();
999 $this->ar_join = array();
1000 $this->ar_where = array();
1001 $this->ar_like = array();
1002 $this->ar_groupby = array();
1003 $this->ar_having = array();
1004 $this->ar_limit = FALSE;
1005 $this->ar_offset = FALSE;
1006 $this->ar_order = FALSE;
1007 $this->ar_orderby = array();
1008 }
1009
1010 // --------------------------------------------------------------------
1011
1012 /**
1013 * Resets the active record "write" values.
1014 *
1015 * Called by the insert() or update() functions
1016 *
1017 * @access private
1018 * @return void
1019 */
1020 function _reset_write()
1021 {
1022 $this->ar_set = array();
1023 $this->ar_from = array();
1024 $this->ar_where = array();
1025 }
1026
1027}
1028
adminac94f382006-09-24 20:28:12 +00001029?>