Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ORM often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ORM, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class ORM implements \ArrayAccess |
||
43 | { |
||
44 | |||
45 | // ----------------------- // |
||
46 | // --- CLASS CONSTANTS --- // |
||
47 | // ----------------------- // |
||
48 | |||
49 | // WHERE and HAVING condition array keys |
||
50 | const CONDITION_FRAGMENT = 0; |
||
51 | const CONDITION_VALUES = 1; |
||
52 | const DEFAULT_CONNECTION = 'default'; |
||
53 | |||
54 | // Limit clause style |
||
55 | const LIMIT_STYLE_TOP_N = 'top'; |
||
56 | const LIMIT_STYLE_LIMIT = 'limit'; |
||
57 | |||
58 | // ------------------------ // |
||
59 | // --- CLASS PROPERTIES --- // |
||
60 | // ------------------------ // |
||
61 | |||
62 | /** |
||
63 | * Class configuration |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected static $_default_config = array( |
||
68 | 'connection_string' => 'sqlite::memory:', |
||
69 | 'id_column' => 'id', |
||
70 | 'id_column_overrides' => array(), |
||
71 | 'error_mode' => \PDO::ERRMODE_EXCEPTION, |
||
72 | 'username' => null, |
||
73 | 'password' => null, |
||
74 | 'driver_options' => null, |
||
75 | 'identifier_quote_character' => null, // if this is null, will be autodetected |
||
76 | 'limit_clause_style' => null, // if this is null, will be autodetected |
||
77 | 'logging' => false, |
||
78 | 'logger' => null, |
||
79 | 'caching' => false, |
||
80 | 'caching_auto_clear' => false, |
||
81 | 'return_result_sets' => false, |
||
82 | ); |
||
83 | |||
84 | /** |
||
85 | * Map of configuration settings |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | protected static $_config = array(); |
||
90 | |||
91 | /** |
||
92 | * Map of database connections, instances of the PDO class |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | protected static $_db = array(); |
||
97 | |||
98 | /** |
||
99 | * Last query run, only populated if logging is enabled |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | protected static $_last_query; |
||
104 | |||
105 | /** |
||
106 | * Log of all queries run, mapped by connection key, only populated if logging is enabled |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | protected static $_query_log = array(); |
||
111 | |||
112 | /** |
||
113 | * Query cache, only used if query caching is enabled |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected static $_query_cache = array(); |
||
118 | |||
119 | /** |
||
120 | * Reference to previously used PDOStatement object to enable low-level access, if needed |
||
121 | * |
||
122 | * @var null|\PDOStatement |
||
123 | */ |
||
124 | protected static $_last_statement = null; |
||
125 | |||
126 | // --------------------------- // |
||
127 | // --- INSTANCE PROPERTIES --- // |
||
128 | // --------------------------- // |
||
129 | |||
130 | /** |
||
131 | * Key name of the connections in static::$_db used by this instance |
||
132 | * |
||
133 | * @var string |
||
134 | */ |
||
135 | protected $_connection_name; |
||
136 | |||
137 | /** |
||
138 | * The name of the table the current ORM instance is associated with |
||
139 | * |
||
140 | * @var string |
||
141 | */ |
||
142 | protected $_table_name; |
||
143 | |||
144 | /** |
||
145 | * Alias for the table to be used in SELECT queries |
||
146 | * |
||
147 | * @var null|string |
||
148 | */ |
||
149 | protected $_table_alias = null; |
||
150 | |||
151 | /** |
||
152 | * Values to be bound to the query |
||
153 | * |
||
154 | * @var array |
||
155 | */ |
||
156 | protected $_values = array(); |
||
157 | |||
158 | /** |
||
159 | * Columns to select in the result |
||
160 | * |
||
161 | * @var array |
||
162 | */ |
||
163 | protected $_result_columns = array('*'); |
||
164 | |||
165 | /** |
||
166 | * Are we using the default result column or have these been manually changed? |
||
167 | * |
||
168 | * @var bool |
||
169 | */ |
||
170 | protected $_using_default_result_columns = true; |
||
171 | |||
172 | /** |
||
173 | * Join sources |
||
174 | * |
||
175 | * @var array |
||
176 | */ |
||
177 | protected $_join_sources = array(); |
||
178 | |||
179 | /** |
||
180 | * Should the query include a DISTINCT keyword? |
||
181 | * |
||
182 | * @var bool |
||
183 | */ |
||
184 | protected $_distinct = false; |
||
185 | |||
186 | /** |
||
187 | * Is this a raw query? |
||
188 | * |
||
189 | * @var bool |
||
190 | */ |
||
191 | protected $_is_raw_query = false; |
||
192 | |||
193 | /** |
||
194 | * The raw query |
||
195 | * |
||
196 | * @var string |
||
197 | */ |
||
198 | protected $_raw_query = ''; |
||
199 | |||
200 | /** |
||
201 | * The raw query parameters |
||
202 | * |
||
203 | * @var array |
||
204 | */ |
||
205 | protected $_raw_parameters = array(); |
||
206 | |||
207 | /** |
||
208 | * Array of WHERE clauses |
||
209 | * |
||
210 | * @var array |
||
211 | */ |
||
212 | protected $_where_conditions = array(); |
||
213 | |||
214 | /** |
||
215 | * LIMIT |
||
216 | * |
||
217 | * @var null|int |
||
218 | */ |
||
219 | protected $_limit = null; |
||
220 | |||
221 | /** |
||
222 | * OFFSET |
||
223 | * |
||
224 | * @var null|int |
||
225 | */ |
||
226 | protected $_offset = null; |
||
227 | |||
228 | /** |
||
229 | * ORDER BY |
||
230 | * |
||
231 | * @var array |
||
232 | */ |
||
233 | protected $_order_by = array(); |
||
234 | |||
235 | /** |
||
236 | * GROUP BY |
||
237 | * |
||
238 | * @var array |
||
239 | */ |
||
240 | protected $_group_by = array(); |
||
241 | |||
242 | /** |
||
243 | * HAVING |
||
244 | * |
||
245 | * @var array |
||
246 | */ |
||
247 | protected $_having_conditions = array(); |
||
248 | |||
249 | /** |
||
250 | * The data for a hydrated instance of the class |
||
251 | * |
||
252 | * @var array |
||
253 | */ |
||
254 | protected $_data = array(); |
||
255 | |||
256 | /** |
||
257 | * Fields that have been modified during the lifetime of the object |
||
258 | * |
||
259 | * @var array |
||
260 | */ |
||
261 | protected $_dirty_fields = array(); |
||
262 | |||
263 | /** |
||
264 | * Fields that are to be inserted in the DB raw |
||
265 | * |
||
266 | * @var array |
||
267 | */ |
||
268 | protected $_expr_fields = array(); |
||
269 | |||
270 | /** |
||
271 | * Is this a new object (has create() been called)? |
||
272 | * |
||
273 | * @var bool |
||
274 | */ |
||
275 | protected $_is_new = false; |
||
276 | |||
277 | /** |
||
278 | * Name of the column to use as the primary key for |
||
279 | * this instance only. Overrides the config settings. |
||
280 | * |
||
281 | * @var null|string |
||
282 | */ |
||
283 | protected $_instance_id_column = null; |
||
284 | |||
285 | /** |
||
286 | * Refresh cache for current query? |
||
287 | * |
||
288 | * @var bool |
||
289 | */ |
||
290 | protected $_refresh_cache = false; |
||
291 | |||
292 | /** |
||
293 | * Disable caching for current query? |
||
294 | * |
||
295 | * @var bool |
||
296 | */ |
||
297 | protected $_no_caching = false; |
||
298 | |||
299 | // ---------------------- // |
||
300 | // --- STATIC METHODS --- // |
||
301 | // ---------------------- // |
||
302 | |||
303 | /** |
||
304 | * Pass configuration settings to the class in the form of |
||
305 | * key/value pairs. As a shortcut, if the second argument |
||
306 | * is omitted and the key is a string, the setting is |
||
307 | * assumed to be the DSN string used by PDO to connect |
||
308 | * to the database (often, this will be the only configuration |
||
309 | * required to use Idiorm). If you have more than one setting |
||
310 | * you wish to configure, another shortcut is to pass an array |
||
311 | * of settings (and omit the second argument). |
||
312 | * |
||
313 | * @param string|array $key |
||
314 | * @param mixed $value |
||
315 | * @param string $connection_name Which connection to use |
||
316 | */ |
||
317 | 232 | public static function configure($key, $value = null, $connection_name = self::DEFAULT_CONNECTION) |
|
318 | { |
||
319 | 232 | static::_setup_db_config($connection_name); //ensures at least default config is set |
|
320 | |||
321 | 232 | if (is_array($key)) { |
|
322 | // Shortcut: If only one array argument is passed, |
||
323 | // assume it's an array of configuration settings |
||
324 | foreach ($key as $conf_key => $conf_value) { |
||
325 | static::configure($conf_key, $conf_value, $connection_name); |
||
326 | } |
||
327 | } else { |
||
328 | 232 | if (null === $value) { |
|
329 | // Shortcut: If only one string argument is passed, |
||
330 | // assume it's a connection string |
||
331 | 5 | $value = $key; |
|
332 | 5 | $key = 'connection_string'; |
|
333 | 5 | } |
|
334 | 232 | static::$_config[$connection_name][$key] = $value; |
|
335 | } |
||
336 | 232 | } |
|
337 | |||
338 | /** |
||
339 | * Retrieve configuration options by key, or as whole array. |
||
340 | * |
||
341 | * @param string|null $key |
||
342 | * @param string $connection_name Which connection to use |
||
343 | * |
||
344 | * @return string|array |
||
345 | */ |
||
346 | 3 | public static function get_config($key = null, $connection_name = self::DEFAULT_CONNECTION) |
|
354 | |||
355 | /** |
||
356 | * Delete all configs in _config array. |
||
357 | */ |
||
358 | 145 | public static function reset_config() |
|
362 | |||
363 | /** |
||
364 | * Despite its slightly odd name, this is actually the factory |
||
365 | * method used to acquire instances of the class. It is named |
||
366 | * this way for the sake of a readable interface, ie |
||
367 | * ORM::for_table('table_name')->find_one()-> etc. As such, |
||
368 | * this will normally be the first method called in a chain. |
||
369 | * |
||
370 | * @param string $table_name |
||
371 | * @param string $connection_name Which connection to use |
||
372 | * |
||
373 | * @return static |
||
374 | */ |
||
375 | 220 | public static function for_table($table_name, $connection_name = self::DEFAULT_CONNECTION) |
|
381 | |||
382 | /** |
||
383 | * Set up the database connection used by the class |
||
384 | * |
||
385 | * @param string $connection_name Which connection to use |
||
386 | */ |
||
387 | 231 | protected static function _setup_db($connection_name = self::DEFAULT_CONNECTION) |
|
404 | |||
405 | /** |
||
406 | * Ensures configuration (multiple connections) is at least set to default. |
||
407 | * |
||
408 | * @param string $connection_name Which connection to use |
||
409 | */ |
||
410 | 232 | protected static function _setup_db_config($connection_name) |
|
416 | |||
417 | /** |
||
418 | * Set the PDO object used by Idiorm to communicate with the database. |
||
419 | * This is public in case the ORM should use a ready-instantiated |
||
420 | * PDO object as its database connection. Accepts an optional string key |
||
421 | * to identify the connection if multiple connections are used. |
||
422 | * |
||
423 | * @param \PDO $db |
||
424 | * @param string $connection_name Which connection to use |
||
425 | */ |
||
426 | 232 | public static function set_db($db, $connection_name = self::DEFAULT_CONNECTION) |
|
436 | |||
437 | /** |
||
438 | * Delete all registered PDO objects in _db array. |
||
439 | */ |
||
440 | 145 | public static function reset_db() |
|
444 | |||
445 | /** |
||
446 | * Detect and initialise the character used to quote identifiers |
||
447 | * (table names, column names etc). If this has been specified |
||
448 | * manually using ORM::configure('identifier_quote_character', 'some-char'), |
||
449 | * this will do nothing. |
||
450 | * |
||
451 | * @param string $connection_name Which connection to use |
||
452 | */ |
||
453 | 232 | protected static function _setup_identifier_quote_character($connection_name) |
|
459 | |||
460 | /** |
||
461 | * Detect and initialise the limit clause style ("SELECT TOP 5" / |
||
462 | * "... LIMIT 5"). If this has been specified manually using |
||
463 | * ORM::configure('limit_clause_style', 'top'), this will do nothing. |
||
464 | * |
||
465 | * @param string $connection_name Which connection to use |
||
466 | */ |
||
467 | 232 | public static function _setup_limit_clause_style($connection_name) |
|
473 | |||
474 | /** |
||
475 | * Return the correct character used to quote identifiers (table |
||
476 | * names, column names etc) by looking at the driver being used by PDO. |
||
477 | * |
||
478 | * @param string $connection_name Which connection to use |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | 145 | protected static function _detect_identifier_quote_character($connection_name) |
|
499 | |||
500 | /** |
||
501 | * Returns a constant after determining the appropriate limit clause |
||
502 | * style |
||
503 | * |
||
504 | * @param string $connection_name Which connection to use |
||
505 | * |
||
506 | * @return string Limit clause style keyword/constant |
||
507 | */ |
||
508 | 145 | protected static function _detect_limit_clause_style($connection_name) |
|
519 | |||
520 | /** |
||
521 | * Returns the PDO instance used by the the ORM to communicate with |
||
522 | * the database. This can be called if any low-level DB access is |
||
523 | * required outside the class. If multiple connections are used, |
||
524 | * accepts an optional key name for the connection. |
||
525 | * |
||
526 | * @param string $connection_name Which connection to use |
||
527 | * |
||
528 | * @return \PDO |
||
529 | */ |
||
530 | 231 | public static function get_db($connection_name = self::DEFAULT_CONNECTION) |
|
535 | |||
536 | /** |
||
537 | * Executes a raw query as a wrapper for PDOStatement::execute. |
||
538 | * Useful for queries that can't be accomplished through Idiorm, |
||
539 | * particularly those using engine-specific features. |
||
540 | * |
||
541 | * @example raw_execute('SELECT `name`, AVG(`order`) FROM `customer` GROUP BY `name` HAVING AVG(`order`) > 10') |
||
542 | * @example raw_execute('INSERT OR REPLACE INTO `widget` (`id`, `name`) SELECT `id`, `name` FROM `other_table`') |
||
543 | * |
||
544 | * @param string $query The raw SQL query |
||
545 | * @param array $parameters Optional bound parameters |
||
546 | * @param string $connection_name Which connection to use |
||
547 | * |
||
548 | * @return bool Success |
||
549 | */ |
||
550 | 1 | public static function raw_execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) |
|
556 | |||
557 | /** |
||
558 | * Returns the PDOStatement instance last used by any connection wrapped by the ORM. |
||
559 | * Useful for access to PDOStatement::rowCount() or error information |
||
560 | * |
||
561 | * @return \PDOStatement |
||
562 | */ |
||
563 | 199 | public static function get_last_statement() |
|
567 | |||
568 | /** |
||
569 | * Internal helper method for executing statements. Logs queries, and |
||
570 | * stores statement object in ::_last_statement, accessible publicly |
||
571 | * through ::get_last_statement() |
||
572 | * |
||
573 | * @param string $query |
||
574 | * @param array $parameters An array of parameters to be bound in to the query |
||
575 | * @param string $connection_name Which connection to use |
||
576 | * |
||
577 | * @return bool Response of PDOStatement::execute() |
||
578 | * |
||
579 | * @throws \Exception |
||
580 | */ |
||
581 | 211 | protected static function _execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) |
|
614 | |||
615 | /** |
||
616 | * Add a query to the internal query log. Only works if the |
||
617 | * 'logging' config option is set to true. |
||
618 | * |
||
619 | * This works by manually binding the parameters to the query - the |
||
620 | * query isn't executed like this (PDO normally passes the query and |
||
621 | * parameters to the database which takes care of the binding) but |
||
622 | * doing it this way makes the logged queries more readable. |
||
623 | * |
||
624 | * @param string $query |
||
625 | * @param array $parameters An array of parameters to be bound in to the query |
||
626 | * @param string $connection_name Which connection to use |
||
627 | * @param float $query_time Query time |
||
628 | * |
||
629 | * @return bool |
||
630 | */ |
||
631 | 211 | protected static function _log_query($query, $parameters, $connection_name, $query_time) |
|
680 | |||
681 | /** |
||
682 | * Get the last query executed. Only works if the |
||
683 | * 'logging' config option is set to true. Otherwise |
||
684 | * this will return null. Returns last query from all connections if |
||
685 | * no connection_name is specified |
||
686 | * |
||
687 | * @param null|string $connection_name Which connection to use |
||
688 | * |
||
689 | * @return string |
||
690 | */ |
||
691 | 200 | public static function get_last_query($connection_name = null) |
|
702 | |||
703 | /** |
||
704 | * Get an array containing all the queries run on a |
||
705 | * specified connection up to now. |
||
706 | * Only works if the 'logging' config option is |
||
707 | * set to true. Otherwise, returned array will be empty. |
||
708 | * |
||
709 | * @param string $connection_name Which connection to use |
||
710 | * |
||
711 | * @return array |
||
712 | */ |
||
713 | public static function get_query_log($connection_name = self::DEFAULT_CONNECTION) |
||
721 | |||
722 | /** |
||
723 | * Get a list of the available connection names |
||
724 | * |
||
725 | * @return array |
||
726 | */ |
||
727 | public static function get_connection_names() |
||
731 | |||
732 | // ------------------------ // |
||
733 | // --- INSTANCE METHODS --- // |
||
734 | // ------------------------ // |
||
735 | |||
736 | /** |
||
737 | * "Private" constructor; shouldn't be called directly. |
||
738 | * Use the ORM::for_table factory method instead. |
||
739 | * |
||
740 | * @param string $table_name |
||
741 | * @param array $data |
||
742 | * @param string $connection_name |
||
743 | */ |
||
744 | 220 | protected function __construct($table_name, $data = array(), $connection_name = self::DEFAULT_CONNECTION) |
|
752 | |||
753 | /** |
||
754 | * Create a new, empty instance of the class. Used |
||
755 | * to add a new row to your database. May optionally |
||
756 | * be passed an associative array of data to populate |
||
757 | * the instance. If so, all fields will be flagged as |
||
758 | * dirty so all will be saved to the database when |
||
759 | * save() is called. |
||
760 | * |
||
761 | * @param null|array $data |
||
762 | * |
||
763 | * @return $this |
||
764 | */ |
||
765 | 19 | public function create($data = null) |
|
774 | |||
775 | /** |
||
776 | * Specify the ID column to use for this instance or array of instances only. |
||
777 | * This overrides the id_column and id_column_overrides settings. |
||
778 | * |
||
779 | * This is mostly useful for libraries built on top of Idiorm, and will |
||
780 | * not normally be used in manually built queries. If you don't know why |
||
781 | * you would want to use this, you should probably just ignore it. |
||
782 | * |
||
783 | * @param string $id_column |
||
784 | * |
||
785 | * @return $this |
||
786 | */ |
||
787 | 200 | public function use_id_column($id_column) |
|
793 | |||
794 | /** |
||
795 | * Create an ORM instance from the given row (an associative |
||
796 | * array of data fetched from the database) |
||
797 | * |
||
798 | * @param array $row |
||
799 | * |
||
800 | * @return ORM |
||
801 | */ |
||
802 | 196 | protected function _create_instance_from_row($row) |
|
810 | |||
811 | /** |
||
812 | * Tell the ORM that you are expecting a single result |
||
813 | * back from your query, and execute it. Will return |
||
814 | * a single instance of the ORM class, or false if no |
||
815 | * rows were returned. |
||
816 | * As a shortcut, you may supply an ID as a parameter |
||
817 | * to this method. This will perform a primary key |
||
818 | * lookup on the table. |
||
819 | * |
||
820 | * @param mixed $id |
||
821 | * |
||
822 | * @return false|ORM false on error |
||
823 | */ |
||
824 | 86 | public function find_one($id = null) |
|
825 | { |
||
826 | 86 | if (null !== $id) { |
|
827 | 31 | $this->where_id_is($id); |
|
828 | 31 | } |
|
829 | 86 | $this->limit(1); |
|
830 | 86 | $rows = $this->_run(); |
|
831 | |||
832 | 86 | if (empty($rows)) { |
|
833 | 2 | return false; |
|
834 | } |
||
835 | |||
836 | 85 | return $this->_create_instance_from_row($rows[0]); |
|
837 | } |
||
838 | |||
839 | /** |
||
840 | * Tell the ORM that you are expecting multiple results |
||
841 | * from your query, and execute it. Will return an array |
||
842 | * of instances of the ORM class, or an empty array if |
||
843 | * no rows were returned. |
||
844 | * |
||
845 | * @return array|IdiormResultSet |
||
846 | */ |
||
847 | 110 | public function find_many() |
|
855 | |||
856 | /** |
||
857 | * Tell the ORM that you are expecting multiple results |
||
858 | * from your query, and execute it. Will return an array |
||
859 | * of instances of the ORM class, or an empty array if |
||
860 | * no rows were returned. |
||
861 | * |
||
862 | * @return array |
||
863 | */ |
||
864 | 112 | protected function _find_many() |
|
870 | |||
871 | /** |
||
872 | * Tell the ORM that you are expecting multiple results |
||
873 | * from your query, and execute it. Will return a result set object |
||
874 | * containing instances of the ORM class. |
||
875 | * |
||
876 | * @return IdiormResultSet |
||
877 | */ |
||
878 | 3 | public function find_result_set() |
|
882 | |||
883 | /** |
||
884 | * Tell the ORM that you are expecting multiple results |
||
885 | * from your query, and execute it. Will return an array, |
||
886 | * or an empty array if no rows were returned. |
||
887 | * |
||
888 | * @return array |
||
889 | */ |
||
890 | 1 | public function find_array() |
|
894 | |||
895 | /** |
||
896 | * Tell the ORM that you wish to execute a COUNT query. |
||
897 | * Will return an integer representing the number of |
||
898 | * rows returned. |
||
899 | * |
||
900 | * @param string $column |
||
901 | * |
||
902 | * @return int |
||
903 | */ |
||
904 | 4 | public function count($column = '*') |
|
908 | |||
909 | /** |
||
910 | * Tell the ORM that you wish to execute a MAX query. |
||
911 | * Will return the max value of the choosen column. |
||
912 | * |
||
913 | * @param string $column |
||
914 | * |
||
915 | * @return int |
||
916 | */ |
||
917 | 2 | public function max($column) |
|
921 | |||
922 | /** |
||
923 | * Tell the ORM that you wish to execute a MIN query. |
||
924 | * Will return the min value of the choosen column. |
||
925 | * |
||
926 | * @param string $column |
||
927 | * |
||
928 | * @return int|float |
||
929 | */ |
||
930 | 2 | public function min($column) |
|
934 | |||
935 | /** |
||
936 | * Tell the ORM that you wish to execute a AVG query. |
||
937 | * Will return the average value of the choosen column. |
||
938 | * |
||
939 | * @param string $column |
||
940 | * |
||
941 | * @return float |
||
942 | */ |
||
943 | 2 | public function avg($column) |
|
947 | |||
948 | /** |
||
949 | * Tell the ORM that you wish to execute a SUM query. |
||
950 | * Will return the sum of the choosen column. |
||
951 | * |
||
952 | * @param string $column |
||
953 | * |
||
954 | * @return int|float |
||
955 | */ |
||
956 | 2 | public function sum($column) |
|
960 | |||
961 | /** |
||
962 | * Execute an aggregate query on the current connection. |
||
963 | * |
||
964 | * @param string $sql_function The aggregate function to call eg. MIN, COUNT, etc |
||
965 | * @param string $column The column to execute the aggregate query against |
||
966 | * |
||
967 | * @return float|int|mixed |
||
968 | */ |
||
969 | 12 | protected function _call_aggregate_db_function($sql_function, $column) |
|
995 | |||
996 | /** |
||
997 | * This method can be called to hydrate (populate) this |
||
998 | * instance of the class from an associative array of data. |
||
999 | * This will usually be called only from inside the class, |
||
1000 | * but it's public in case you need to call it directly. |
||
1001 | * |
||
1002 | * @param array $data |
||
1003 | * |
||
1004 | * @return $this |
||
1005 | */ |
||
1006 | 200 | public function hydrate($data = array()) |
|
1012 | |||
1013 | /** |
||
1014 | * Force the ORM to flag all the fields in the $data array |
||
1015 | * as "dirty" and therefore update them when save() is called. |
||
1016 | * |
||
1017 | 6 | * @return $this |
|
1018 | */ |
||
1019 | 6 | public function force_all_dirty() |
|
1025 | |||
1026 | /** |
||
1027 | * Perform a raw query. The query can contain placeholders in |
||
1028 | * either named or question mark style. If placeholders are |
||
1029 | * used, the parameters should be an array of values which will |
||
1030 | * be bound to the placeholders in the query. If this method |
||
1031 | * is called, all other query building methods will be ignored. |
||
1032 | * |
||
1033 | * @param string $query |
||
1034 | * @param array $parameters |
||
1035 | * |
||
1036 | 4 | * @return $this |
|
1037 | */ |
||
1038 | 4 | public function raw_query($query, $parameters = array()) |
|
1046 | |||
1047 | /** |
||
1048 | * Add an alias for the main table to be used in SELECT queries |
||
1049 | * |
||
1050 | * @param string $alias |
||
1051 | * |
||
1052 | 4 | * @return $this |
|
1053 | */ |
||
1054 | 4 | public function table_alias($alias) |
|
1060 | |||
1061 | /** |
||
1062 | * Internal method to add an unquoted expression to the set |
||
1063 | * of columns returned by the SELECT query. The second optional |
||
1064 | * argument is the alias to return the expression as. |
||
1065 | * |
||
1066 | * @param string $expr |
||
1067 | * @param mixed $alias |
||
1068 | * |
||
1069 | 34 | * @return $this |
|
1070 | */ |
||
1071 | 34 | protected function _add_result_column($expr, $alias = null) |
|
1086 | |||
1087 | /** |
||
1088 | * Counts the number of columns that belong to the primary |
||
1089 | 12 | * key and their value is null. |
|
1090 | * |
||
1091 | 12 | * @return int |
|
1092 | 3 | */ |
|
1093 | public function count_null_id_columns() |
||
1101 | |||
1102 | /** |
||
1103 | * Add a column to the list of columns returned by the SELECT |
||
1104 | * query. This defaults to '*'. The second optional argument is |
||
1105 | * the alias to return the column as. |
||
1106 | * |
||
1107 | * @param string $column |
||
1108 | 20 | * @param mixed $alias |
|
1109 | * |
||
1110 | 20 | * @return ORM |
|
1111 | */ |
||
1112 | 20 | public function select($column, $alias = null) |
|
1118 | |||
1119 | /** |
||
1120 | * Add an unquoted expression to the list of columns returned |
||
1121 | * by the SELECT query. The second optional argument is |
||
1122 | * the alias to return the column as. |
||
1123 | * |
||
1124 | * @param string $expr |
||
1125 | 16 | * @param mixed $alias |
|
1126 | * |
||
1127 | 16 | * @return ORM |
|
1128 | */ |
||
1129 | public function select_expr($expr, $alias = null) |
||
1133 | |||
1134 | /** |
||
1135 | * Add columns to the list of columns returned by the SELECT |
||
1136 | * query. This defaults to '*'. Many columns can be supplied |
||
1137 | * as either an array or as a list of parameters to the method. |
||
1138 | * |
||
1139 | * Note that the alias must not be numeric - if you want a |
||
1140 | * numeric alias then prepend it with some alpha chars. eg. a1 |
||
1141 | * |
||
1142 | * @example select_many(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5'); |
||
1143 | * @example select_many('column', 'column2', 'column3'); |
||
1144 | 2 | * @example select_many(array('column', 'column2', 'column3'), 'column4', 'column5'); |
|
1145 | * |
||
1146 | 2 | * @return ORM |
|
1147 | 2 | */ |
|
1148 | 2 | View Code Duplication | public function select_many() |
1163 | |||
1164 | /** |
||
1165 | * Add an unquoted expression to the list of columns returned |
||
1166 | * by the SELECT query. Many columns can be supplied as either |
||
1167 | * an array or as a list of parameters to the method. |
||
1168 | * |
||
1169 | * Note that the alias must not be numeric - if you want a |
||
1170 | * numeric alias then prepend it with some alpha chars. eg. a1 |
||
1171 | * |
||
1172 | * @example select_many_expr(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5') |
||
1173 | * @example select_many_expr('column', 'column2', 'column3') |
||
1174 | 2 | * @example select_many_expr(array('column', 'column2', 'column3'), 'column4', 'column5') |
|
1175 | * |
||
1176 | 2 | * @return ORM |
|
1177 | 2 | */ |
|
1178 | 2 | View Code Duplication | public function select_many_expr() |
1193 | |||
1194 | /** |
||
1195 | * Take a column specification for the select many methods and convert it |
||
1196 | * into a normalised array of columns and aliases. |
||
1197 | * |
||
1198 | * It is designed to turn the following styles into a normalised array: |
||
1199 | * |
||
1200 | * array(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5')) |
||
1201 | * |
||
1202 | 4 | * @param array $columns |
|
1203 | * |
||
1204 | 4 | * @return array |
|
1205 | 4 | */ |
|
1206 | 4 | protected function _normalise_select_many_columns($columns) |
|
1225 | 2 | ||
1226 | /** |
||
1227 | 2 | * Add a DISTINCT keyword before the list of columns in the SELECT query |
|
1228 | * |
||
1229 | 2 | * @return $this |
|
1230 | */ |
||
1231 | public function distinct() |
||
1237 | |||
1238 | /** |
||
1239 | * Internal method to add a JOIN source to the query. |
||
1240 | * |
||
1241 | * The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this |
||
1242 | * will be prepended to JOIN. |
||
1243 | * |
||
1244 | * The table should be the name of the table to join to. |
||
1245 | * |
||
1246 | * The constraint may be either a string or an array with three elements. If it |
||
1247 | * is a string, it will be compiled into the query as-is, with no escaping. The |
||
1248 | * recommended way to supply the constraint is as an array with three elements: |
||
1249 | * |
||
1250 | * first_column, operator, second_column |
||
1251 | * |
||
1252 | * Example: array('user.id', '=', 'profile.user_id') |
||
1253 | * |
||
1254 | * will compile to |
||
1255 | * |
||
1256 | * ON `user`.`id` = `profile`.`user_id` |
||
1257 | * |
||
1258 | * The final (optional) argument specifies an alias for the joined table. |
||
1259 | * |
||
1260 | * @param string $join_operator |
||
1261 | 20 | * @param string $table |
|
1262 | * @param string $constraint |
||
1263 | 20 | * @param string|null $table_alias |
|
1264 | * |
||
1265 | 20 | * @return $this |
|
1266 | */ |
||
1267 | protected function _add_join_source($join_operator, $table, $constraint, $table_alias = null) |
||
1291 | |||
1292 | /** |
||
1293 | * Add a RAW JOIN source to the query |
||
1294 | * |
||
1295 | * @param string $table |
||
1296 | 6 | * @param string $constraint |
|
1297 | * @param string $table_alias |
||
1298 | * @param array $parameters |
||
1299 | 6 | * |
|
1300 | 6 | * @return $this |
|
1301 | 6 | */ |
|
1302 | 6 | public function raw_join($table, $constraint, $table_alias, $parameters = array()) |
|
1324 | |||
1325 | /** |
||
1326 | * Add a simple JOIN source to the query |
||
1327 | * |
||
1328 | 12 | * @param string $table |
|
1329 | * @param string $constraint |
||
1330 | 12 | * @param string|null $table_alias |
|
1331 | * |
||
1332 | * @return ORM |
||
1333 | */ |
||
1334 | public function join($table, $constraint, $table_alias = null) |
||
1338 | |||
1339 | /** |
||
1340 | * Add an INNER JOIN souce to the query |
||
1341 | */ |
||
1342 | /** |
||
1343 | 2 | * @param string $table |
|
1344 | * @param string $constraint |
||
1345 | 2 | * @param null|string $table_alias |
|
1346 | * |
||
1347 | * @return ORM |
||
1348 | */ |
||
1349 | public function inner_join($table, $constraint, $table_alias = null) |
||
1353 | |||
1354 | /** |
||
1355 | * Add an LEFT JOIN source to the query |
||
1356 | * |
||
1357 | * @param string $table |
||
1358 | * @param $constraint |
||
1359 | * @param null $table_alias |
||
1360 | * |
||
1361 | * @return ORM |
||
1362 | */ |
||
1363 | public function left_join($table, $constraint, $table_alias = null) |
||
1367 | |||
1368 | /** |
||
1369 | * Add an RIGHT JOIN source to the query |
||
1370 | * |
||
1371 | 2 | * @param string $table |
|
1372 | * @param $constraint |
||
1373 | 2 | * @param null $table_alias |
|
1374 | * |
||
1375 | * @return ORM |
||
1376 | */ |
||
1377 | public function right_join($table, $constraint, $table_alias = null) |
||
1381 | |||
1382 | /** |
||
1383 | * Add a LEFT OUTER JOIN souce to the query |
||
1384 | * |
||
1385 | 2 | * @param string $table |
|
1386 | * @param string $constraint |
||
1387 | 2 | * @param null|string $table_alias |
|
1388 | * |
||
1389 | * @return ORM |
||
1390 | */ |
||
1391 | public function left_outer_join($table, $constraint, $table_alias = null) |
||
1395 | |||
1396 | /** |
||
1397 | * Add an RIGHT OUTER JOIN souce to the query |
||
1398 | * |
||
1399 | * @param string $table |
||
1400 | 2 | * @param string $constraint |
|
1401 | * @param null|string $table_alias |
||
1402 | 2 | * |
|
1403 | * @return ORM |
||
1404 | */ |
||
1405 | public function right_outer_join($table, $constraint, $table_alias = null) |
||
1409 | |||
1410 | /** |
||
1411 | * Add an FULL OUTER JOIN souce to the query |
||
1412 | */ |
||
1413 | /** |
||
1414 | 10 | * @param string $table |
|
1415 | * @param string $constraint |
||
1416 | 10 | * @param null|string $table_alias |
|
1417 | * |
||
1418 | * @return ORM |
||
1419 | */ |
||
1420 | public function full_outer_join($table, $constraint, $table_alias = null) |
||
1424 | |||
1425 | /** |
||
1426 | * Internal method to add a HAVING condition to the query |
||
1427 | */ |
||
1428 | /** |
||
1429 | 14 | * @param string $fragment |
|
1430 | * @param array $values |
||
1431 | 14 | * |
|
1432 | * @return ORM |
||
1433 | */ |
||
1434 | protected function _add_having($fragment, $values = array()) |
||
1438 | |||
1439 | /** |
||
1440 | * Internal method to add a HAVING condition to the query |
||
1441 | */ |
||
1442 | /** |
||
1443 | * @param string|array $column_name |
||
1444 | 4 | * @param string $separator |
|
1445 | * @param mixed $value |
||
1446 | 4 | * |
|
1447 | 4 | * @return ORM |
|
1448 | 4 | */ |
|
1449 | protected function _add_simple_having($column_name, $separator, $value) |
||
1453 | 4 | ||
1454 | 4 | /** |
|
1455 | 4 | * Internal method to add a HAVING clause with multiple values (like IN and NOT IN) |
|
1456 | 4 | */ |
|
1457 | 4 | /** |
|
1458 | * @param string|array $column_name |
||
1459 | 4 | * @param string $separator |
|
1460 | * @param mixed $values |
||
1461 | * |
||
1462 | * @return ORM |
||
1463 | */ |
||
1464 | View Code Duplication | public function _add_having_placeholder($column_name, $separator, $values) |
|
1481 | 4 | ||
1482 | 4 | /** |
|
1483 | * Internal method to add a HAVING clause with no parameters(like IS NULL and IS NOT NULL) |
||
1484 | 4 | * |
|
1485 | * @param string|array $column_name |
||
1486 | * @param string $operator |
||
1487 | * |
||
1488 | * @return ORM |
||
1489 | */ |
||
1490 | View Code Duplication | public function _add_having_no_value($column_name, $operator) |
|
1506 | |||
1507 | /** |
||
1508 | * Internal method to add a WHERE condition to the query |
||
1509 | 63 | * |
|
1510 | * @param string $fragment |
||
1511 | 63 | * @param array $values |
|
1512 | * |
||
1513 | * @return ORM |
||
1514 | */ |
||
1515 | protected function _add_where($fragment, $values = array()) |
||
1519 | |||
1520 | /** |
||
1521 | * Internal method to add a WHERE condition to the query |
||
1522 | * |
||
1523 | 5 | * @param string|array $column_name |
|
1524 | * @param string $separator |
||
1525 | 5 | * @param mixed $value |
|
1526 | 5 | * |
|
1527 | 5 | * @return ORM |
|
1528 | */ |
||
1529 | protected function _add_simple_where($column_name, $separator, $value) |
||
1533 | 5 | ||
1534 | 5 | /** |
|
1535 | 5 | * Add a WHERE clause with multiple values (like IN and NOT IN) |
|
1536 | 5 | * |
|
1537 | * @param string|array $column_name |
||
1538 | 5 | * @param string $separator |
|
1539 | * @param mixed $values |
||
1540 | * |
||
1541 | * @return ORM |
||
1542 | */ |
||
1543 | View Code Duplication | public function _add_where_placeholder($column_name, $separator, $values) |
|
1560 | 4 | ||
1561 | 4 | /** |
|
1562 | * Add a WHERE clause with no parameters(like IS NULL and IS NOT NULL) |
||
1563 | 4 | * |
|
1564 | * @param string|array $column_name |
||
1565 | * @param string $operator |
||
1566 | * |
||
1567 | * @return ORM |
||
1568 | */ |
||
1569 | View Code Duplication | public function _add_where_no_value($column_name, $operator) |
|
1585 | |||
1586 | 112 | /** |
|
1587 | 112 | * Internal method to add a HAVING or WHERE condition to the query |
|
1588 | * |
||
1589 | 112 | * @param string $type |
|
1590 | * @param string $fragment |
||
1591 | 112 | * @param mixed $values |
|
1592 | * |
||
1593 | * @return $this |
||
1594 | */ |
||
1595 | protected function _add_condition($type, $fragment, $values = array()) |
||
1613 | 1 | ||
1614 | 76 | /** |
|
1615 | * Helper method to compile a simple COLUMN SEPARATOR VALUE |
||
1616 | * style HAVING or WHERE condition into a string and value ready to |
||
1617 | 77 | * be passed to the _add_condition method. Avoids duplication |
|
1618 | * of the call to _quote_identifier |
||
1619 | 77 | * |
|
1620 | * If column_name is an associative array, it will add a condition for each column |
||
1621 | 77 | * |
|
1622 | * @param string $type |
||
1623 | 4 | * @param string|array $column_name |
|
1624 | 4 | * @param string $separator |
|
1625 | 2 | * @param string|int $value |
|
1626 | 2 | * |
|
1627 | * @return ORM |
||
1628 | 4 | */ |
|
1629 | 4 | protected function _add_simple_condition($type, $column_name, $separator, $value) |
|
1656 | 21 | ||
1657 | /** |
||
1658 | 21 | * Return a string containing the given number of question marks, |
|
1659 | * separated by commas. Eg "?, ?, ?" |
||
1660 | 1 | * |
|
1661 | * @param mixed $fields |
||
1662 | * |
||
1663 | * @return string |
||
1664 | */ |
||
1665 | protected function _create_placeholders($fields) |
||
1683 | |||
1684 | /** |
||
1685 | * Helper method that filters a column/value array returning only those |
||
1686 | * columns that belong to a compound primary key. |
||
1687 | * |
||
1688 | * If the key contains a column that does not exist in the given array, |
||
1689 | * a null value will be returned for it. |
||
1690 | * |
||
1691 | * @param mixed $value |
||
1692 | * |
||
1693 | 1 | * @return array |
|
1694 | */ |
||
1695 | 1 | protected function _get_compound_id_column_values($value) |
|
1704 | |||
1705 | /** |
||
1706 | * Helper method that filters an array containing compound column/value |
||
1707 | * arrays. |
||
1708 | * |
||
1709 | * @param array $values |
||
1710 | * |
||
1711 | * @return array |
||
1712 | */ |
||
1713 | protected function _get_compound_id_column_values_array($values) |
||
1722 | |||
1723 | /** |
||
1724 | * Add a WHERE column = value clause to your query. Each time |
||
1725 | * this is called in the chain, an additional WHERE will be |
||
1726 | * added, and these will be ANDed together when the final query |
||
1727 | * is built. |
||
1728 | * |
||
1729 | * If you use an array in $column_name, a new clause will be |
||
1730 | * added for each element. In this case, $value is ignored. |
||
1731 | 53 | * |
|
1732 | * @param string $column_name |
||
1733 | 53 | * @param mixed $value |
|
1734 | * |
||
1735 | * @return ORM |
||
1736 | */ |
||
1737 | public function where($column_name, $value = null) |
||
1741 | |||
1742 | /** |
||
1743 | * More explicitly named version of for the where() method. |
||
1744 | 2 | * Can be used if preferred. |
|
1745 | * |
||
1746 | 2 | * @param string $column_name |
|
1747 | * @param mixed $value |
||
1748 | * |
||
1749 | * @return ORM |
||
1750 | */ |
||
1751 | public function where_equal($column_name, $value = null) |
||
1755 | |||
1756 | /** |
||
1757 | * Add a WHERE column != value clause to your query. |
||
1758 | * |
||
1759 | 33 | * @param string $column_name |
|
1760 | * @param mixed $value |
||
1761 | 33 | * |
|
1762 | 1 | * @return ORM |
|
1763 | */ |
||
1764 | 32 | public function where_not_equal($column_name, $value = null) |
|
1768 | |||
1769 | /** |
||
1770 | * Special method to query the table by its primary key |
||
1771 | * |
||
1772 | * If primary key is compound, only the columns that |
||
1773 | * belong to they key will be used for the query |
||
1774 | * |
||
1775 | * @param mixed $id |
||
1776 | * |
||
1777 | * @return ORM |
||
1778 | */ |
||
1779 | View Code Duplication | public function where_id_is($id) |
|
1787 | 4 | ||
1788 | 4 | /** |
|
1789 | 4 | * Allows adding a WHERE clause that matches any of the conditions |
|
1790 | 4 | * specified in the array. Each element in the associative array will |
|
1791 | 4 | * be a different condition, where the key will be the column name. |
|
1792 | 4 | * |
|
1793 | * By default, an equal operator will be used against all columns, but |
||
1794 | 4 | * it can be overridden for any or every column using the second parameter. |
|
1795 | * |
||
1796 | 4 | * Each condition will be ORed together when added to the final query. |
|
1797 | * |
||
1798 | 4 | * @param array $values |
|
1799 | 3 | * @param string $operator |
|
1800 | 3 | * |
|
1801 | 1 | * @return ORM |
|
1802 | */ |
||
1803 | public function where_any_is($values, $operator = '=') |
||
1839 | |||
1840 | /** |
||
1841 | * Similar to where_id_is() but allowing multiple primary keys. |
||
1842 | * |
||
1843 | * If primary key is compound, only the columns that |
||
1844 | * belong to they key will be used for the query |
||
1845 | * |
||
1846 | * @param mixed $ids |
||
1847 | 2 | * |
|
1848 | * @return ORM |
||
1849 | 2 | */ |
|
1850 | public function where_id_in($ids) |
||
1858 | |||
1859 | /** |
||
1860 | 2 | * Add a WHERE ... LIKE clause to your query. |
|
1861 | * |
||
1862 | 2 | * @param string $column_name |
|
1863 | * @param mixed $value |
||
1864 | * |
||
1865 | * @return ORM |
||
1866 | */ |
||
1867 | public function where_like($column_name, $value = null) |
||
1871 | |||
1872 | /** |
||
1873 | 2 | * Add where WHERE ... NOT LIKE clause to your query. |
|
1874 | * |
||
1875 | 2 | * @param string $column_name |
|
1876 | * @param mixed $value |
||
1877 | * |
||
1878 | * @return ORM |
||
1879 | */ |
||
1880 | public function where_not_like($column_name, $value = null) |
||
1884 | |||
1885 | /** |
||
1886 | 2 | * Add a WHERE ... > clause to your query |
|
1887 | * |
||
1888 | 2 | * @param string $column_name |
|
1889 | * @param mixed $value |
||
1890 | * |
||
1891 | * @return ORM |
||
1892 | */ |
||
1893 | public function where_gt($column_name, $value = null) |
||
1897 | |||
1898 | /** |
||
1899 | 2 | * Add a WHERE ... < clause to your query |
|
1900 | * |
||
1901 | 2 | * @param string $column_name |
|
1902 | * @param mixed $value |
||
1903 | * |
||
1904 | * @return ORM |
||
1905 | */ |
||
1906 | public function where_lt($column_name, $value = null) |
||
1910 | |||
1911 | /** |
||
1912 | 2 | * Add a WHERE ... >= clause to your query |
|
1913 | * |
||
1914 | 2 | * @param string $column_name |
|
1915 | * @param mixed $value |
||
1916 | * |
||
1917 | * @return ORM |
||
1918 | */ |
||
1919 | public function where_gte($column_name, $value = null) |
||
1923 | |||
1924 | /** |
||
1925 | 3 | * Add a WHERE ... <= clause to your query |
|
1926 | * |
||
1927 | 3 | * @param string $column_name |
|
1928 | * @param mixed $value |
||
1929 | * |
||
1930 | * @return ORM |
||
1931 | */ |
||
1932 | public function where_lte($column_name, $value = null) |
||
1936 | |||
1937 | /** |
||
1938 | 2 | * Add a WHERE ... IN clause to your query |
|
1939 | * |
||
1940 | 2 | * @param string $column_name |
|
1941 | * @param mixed $values |
||
1942 | * |
||
1943 | * @return ORM |
||
1944 | */ |
||
1945 | public function where_in($column_name, $values) |
||
1949 | |||
1950 | 2 | /** |
|
1951 | * Add a WHERE ... NOT IN clause to your query |
||
1952 | 2 | * |
|
1953 | * @param string $column_name |
||
1954 | * @param mixed $values |
||
1955 | * |
||
1956 | * @return ORM |
||
1957 | */ |
||
1958 | public function where_not_in($column_name, $values) |
||
1962 | 2 | ||
1963 | /** |
||
1964 | 2 | * Add a WHERE column IS NULL clause to your query |
|
1965 | * |
||
1966 | * @param string $column_name |
||
1967 | * |
||
1968 | * @return ORM |
||
1969 | */ |
||
1970 | public function where_null($column_name) |
||
1974 | |||
1975 | /** |
||
1976 | * Add a WHERE column IS NOT NULL clause to your query |
||
1977 | 18 | * |
|
1978 | * @param string $column_name |
||
1979 | 18 | * |
|
1980 | * @return ORM |
||
1981 | */ |
||
1982 | public function where_not_null($column_name) |
||
1986 | |||
1987 | /** |
||
1988 | * Add a raw WHERE clause to the query. The clause should |
||
1989 | 95 | * contain question mark placeholders, which will be bound |
|
1990 | * to the parameters supplied in the second argument. |
||
1991 | 95 | * |
|
1992 | * @param string $clause |
||
1993 | 95 | * @param array $parameters |
|
1994 | * |
||
1995 | * @return ORM |
||
1996 | */ |
||
1997 | public function where_raw($clause, $parameters = array()) |
||
2001 | |||
2002 | /** |
||
2003 | 4 | * Add a LIMIT to the query |
|
2004 | * |
||
2005 | 4 | * @param int $limit |
|
2006 | * |
||
2007 | 4 | * @return $this |
|
2008 | */ |
||
2009 | public function limit($limit) |
||
2015 | |||
2016 | /** |
||
2017 | * Add an OFFSET to the query |
||
2018 | 12 | * |
|
2019 | * @param $offset |
||
2020 | 12 | * |
|
2021 | 12 | * @return $this |
|
2022 | */ |
||
2023 | 12 | public function offset($offset) |
|
2029 | |||
2030 | /** |
||
2031 | * Add an ORDER BY clause to the query |
||
2032 | * |
||
2033 | 8 | * @param string $column_name |
|
2034 | * @param string $ordering |
||
2035 | 8 | * |
|
2036 | * @return $this |
||
2037 | */ |
||
2038 | protected function _add_order_by($column_name, $ordering) |
||
2045 | 6 | ||
2046 | /** |
||
2047 | 6 | * Add an ORDER BY column DESC clause |
|
2048 | * |
||
2049 | * @param string $column_name |
||
2050 | * |
||
2051 | * @return ORM |
||
2052 | */ |
||
2053 | public function order_by_desc($column_name) |
||
2057 | 2 | ||
2058 | /** |
||
2059 | 2 | * Add an ORDER BY column ASC clause |
|
2060 | * |
||
2061 | 2 | * @param string $column_name |
|
2062 | * |
||
2063 | * @return ORM |
||
2064 | */ |
||
2065 | public function order_by_asc($column_name) |
||
2069 | 1 | ||
2070 | /** |
||
2071 | 1 | * Add an unquoted expression as an ORDER BY clause |
|
2072 | * |
||
2073 | * @param $clause |
||
2074 | * |
||
2075 | * @return $this |
||
2076 | */ |
||
2077 | public function order_by_expr($clause) |
||
2083 | 28 | ||
2084 | 28 | /** |
|
2085 | * Reset the ORDER BY clause |
||
2086 | 28 | */ |
|
2087 | public function reset_order_by() |
||
2093 | |||
2094 | /** |
||
2095 | * Add a column to the list of columns to GROUP BY |
||
2096 | 2 | * |
|
2097 | * @param string $column_name |
||
2098 | 2 | * |
|
2099 | * @return $this |
||
2100 | 2 | */ |
|
2101 | public function group_by($column_name) |
||
2108 | |||
2109 | /** |
||
2110 | * Add an unquoted expression to the list of columns to GROUP BY |
||
2111 | * |
||
2112 | * @param string $expr |
||
2113 | * |
||
2114 | * @return $this |
||
2115 | */ |
||
2116 | public function group_by_expr($expr) |
||
2122 | |||
2123 | /** |
||
2124 | * Add a HAVING column = value clause to your query. Each time |
||
2125 | * this is called in the chain, an additional HAVING will be |
||
2126 | * added, and these will be ANDed together when the final query |
||
2127 | * is built. |
||
2128 | * |
||
2129 | * If you use an array in $column_name, a new clause will be |
||
2130 | * added for each element. In this case, $value is ignored. |
||
2131 | 4 | * |
|
2132 | * @param string $column_name |
||
2133 | 4 | * @param null $value |
|
2134 | * |
||
2135 | * @return ORM |
||
2136 | */ |
||
2137 | public function having($column_name, $value = null) |
||
2141 | |||
2142 | /** |
||
2143 | * More explicitly named version of for the having() method. |
||
2144 | 2 | * Can be used if preferred. |
|
2145 | * |
||
2146 | 2 | * @param string $column_name |
|
2147 | * @param null $value |
||
2148 | * |
||
2149 | * @return ORM |
||
2150 | */ |
||
2151 | public function having_equal($column_name, $value = null) |
||
2155 | |||
2156 | /** |
||
2157 | * Add a HAVING column != value clause to your query. |
||
2158 | * |
||
2159 | * @param string $column_name |
||
2160 | * @param null $value |
||
2161 | * |
||
2162 | * @return ORM |
||
2163 | */ |
||
2164 | public function having_not_equal($column_name, $value = null) |
||
2168 | |||
2169 | /** |
||
2170 | * Special method to query the table by its primary key. |
||
2171 | * |
||
2172 | * If primary key is compound, only the columns that |
||
2173 | * belong to they key will be used for the query |
||
2174 | * |
||
2175 | * @param $id |
||
2176 | 2 | * |
|
2177 | * @return ORM |
||
2178 | 2 | */ |
|
2179 | View Code Duplication | public function having_id_is($id) |
|
2187 | |||
2188 | /** |
||
2189 | 2 | * Add a HAVING ... LIKE clause to your query. |
|
2190 | * |
||
2191 | 2 | * @param string $column_name |
|
2192 | * @param mixed $value |
||
2193 | * |
||
2194 | * @return ORM |
||
2195 | */ |
||
2196 | public function having_like($column_name, $value = null) |
||
2200 | |||
2201 | /** |
||
2202 | 2 | * Add where HAVING ... NOT LIKE clause to your query. |
|
2203 | * |
||
2204 | 2 | * @param string $column_name |
|
2205 | * @param mixed $value |
||
2206 | * |
||
2207 | * @return ORM |
||
2208 | */ |
||
2209 | public function having_not_like($column_name, $value = null) |
||
2213 | |||
2214 | /** |
||
2215 | 2 | * Add a HAVING ... > clause to your query |
|
2216 | * |
||
2217 | 2 | * @param string $column_name |
|
2218 | * @param mixed $value |
||
2219 | * |
||
2220 | * @return ORM |
||
2221 | */ |
||
2222 | public function having_gt($column_name, $value = null) |
||
2226 | |||
2227 | /** |
||
2228 | 2 | * Add a HAVING ... < clause to your query |
|
2229 | * |
||
2230 | 2 | * @param string $column_name |
|
2231 | * @param mixed $value |
||
2232 | * |
||
2233 | * @return ORM |
||
2234 | */ |
||
2235 | public function having_lt($column_name, $value = null) |
||
2239 | |||
2240 | /** |
||
2241 | 2 | * Add a HAVING ... >= clause to your query |
|
2242 | * |
||
2243 | 2 | * @param string $column_name |
|
2244 | * @param mixed $value |
||
2245 | * |
||
2246 | * @return ORM |
||
2247 | */ |
||
2248 | public function having_gte($column_name, $value = null) |
||
2252 | |||
2253 | /** |
||
2254 | 2 | * Add a HAVING ... <= clause to your query |
|
2255 | * |
||
2256 | 2 | * @param string $column_name |
|
2257 | * @param mixed $value |
||
2258 | * |
||
2259 | * @return ORM |
||
2260 | */ |
||
2261 | public function having_lte($column_name, $value = null) |
||
2265 | |||
2266 | /** |
||
2267 | 2 | * Add a HAVING ... IN clause to your query |
|
2268 | * |
||
2269 | 2 | * @param string $column_name |
|
2270 | * @param mixed $values |
||
2271 | * |
||
2272 | * @return ORM |
||
2273 | */ |
||
2274 | public function having_in($column_name, $values = null) |
||
2278 | |||
2279 | 2 | /** |
|
2280 | * Add a HAVING ... NOT IN clause to your query |
||
2281 | 2 | * |
|
2282 | * @param string $column_name |
||
2283 | * @param mixed $values |
||
2284 | * |
||
2285 | * @return ORM |
||
2286 | */ |
||
2287 | public function having_not_in($column_name, $values = null) |
||
2291 | 2 | ||
2292 | /** |
||
2293 | 2 | * Add a HAVING column IS NULL clause to your query |
|
2294 | * |
||
2295 | * @param string $column_name |
||
2296 | * |
||
2297 | * @return ORM |
||
2298 | */ |
||
2299 | public function having_null($column_name) |
||
2303 | |||
2304 | /** |
||
2305 | * Add a HAVING column IS NOT NULL clause to your query |
||
2306 | 2 | * |
|
2307 | * @param string $column_name |
||
2308 | 2 | * |
|
2309 | * @return ORM |
||
2310 | */ |
||
2311 | public function having_not_null($column_name) |
||
2315 | |||
2316 | /** |
||
2317 | * Add a raw HAVING clause to the query. The clause should |
||
2318 | * contain question mark placeholders, which will be bound |
||
2319 | * to the parameters supplied in the second argument. |
||
2320 | * |
||
2321 | * @param string $clause |
||
2322 | * @param array $parameters |
||
2323 | * |
||
2324 | * @return ORM |
||
2325 | */ |
||
2326 | public function having_raw($clause, $parameters = array()) |
||
2330 | |||
2331 | /** |
||
2332 | * Activate cache refreshing for current query |
||
2333 | * |
||
2334 | * @return ORM |
||
2335 | */ |
||
2336 | public function refreshCache() |
||
2342 | |||
2343 | 199 | /** |
|
2344 | 4 | * Disable caching for current query |
|
2345 | * |
||
2346 | 4 | * @return ORM |
|
2347 | */ |
||
2348 | public function noCaching() |
||
2354 | 195 | ||
2355 | 195 | /** |
|
2356 | 195 | * Build a SELECT statement based on the clauses that have |
|
2357 | 195 | * been passed to this instance by chaining method calls. |
|
2358 | 195 | */ |
|
2359 | 195 | protected function _build_select() |
|
2385 | |||
2386 | 195 | /** |
|
2387 | * Build the start of the SELECT statement |
||
2388 | 195 | * |
|
2389 | 4 | * @return string |
|
2390 | 4 | */ |
|
2391 | protected function _build_select_start() |
||
2416 | |||
2417 | /** |
||
2418 | 195 | * Build the JOIN sources |
|
2419 | * |
||
2420 | 195 | * @return string |
|
2421 | */ |
||
2422 | protected function _build_join() |
||
2430 | |||
2431 | /** |
||
2432 | 30 | * Build the WHERE clause(s) |
|
2433 | * |
||
2434 | * @return string |
||
2435 | */ |
||
2436 | protected function _build_where() |
||
2440 | |||
2441 | /** |
||
2442 | 197 | * Build the HAVING clause(s) |
|
2443 | * |
||
2444 | 197 | * @return string |
|
2445 | */ |
||
2446 | 197 | protected function _build_having() |
|
2450 | 112 | ||
2451 | 112 | /** |
|
2452 | 112 | * Build GROUP BY |
|
2453 | * |
||
2454 | 112 | * @return string |
|
2455 | 112 | */ |
|
2456 | protected function _build_group_by() |
||
2464 | |||
2465 | 195 | /** |
|
2466 | 184 | * Build a WHERE or HAVING clause |
|
2467 | * |
||
2468 | * @param string $type |
||
2469 | 13 | * |
|
2470 | * @return string |
||
2471 | 13 | */ |
|
2472 | protected function _build_conditions($type) |
||
2489 | |||
2490 | 93 | /** |
|
2491 | * Build ORDER BY |
||
2492 | * |
||
2493 | 93 | * @return string |
|
2494 | */ |
||
2495 | 93 | protected function _build_order_by() |
|
2505 | |||
2506 | 195 | /** |
|
2507 | 4 | * Build LIMIT |
|
2508 | 4 | * |
|
2509 | * @return string |
||
2510 | */ |
||
2511 | protected function _build_limit() |
||
2534 | 197 | ||
2535 | 197 | /** |
|
2536 | 197 | * Build OFFSET |
|
2537 | 197 | * |
|
2538 | 197 | * @return string |
|
2539 | 197 | */ |
|
2540 | protected function _build_offset() |
||
2555 | 207 | ||
2556 | 207 | /** |
|
2557 | * Wrapper around PHP's join function which |
||
2558 | 207 | * only adds the pieces if they are not empty. |
|
2559 | * |
||
2560 | * @param string $glue |
||
2561 | * @param array $pieces |
||
2562 | * |
||
2563 | * @return string |
||
2564 | */ |
||
2565 | protected function _join_if_not_empty($glue, $pieces) |
||
2579 | |||
2580 | /** |
||
2581 | * Quote a string that is used as an identifier |
||
2582 | * (table names, column names etc). This method can |
||
2583 | * also deal with dot-separated identifiers eg table.column |
||
2584 | * |
||
2585 | * @param string $identifier |
||
2586 | * |
||
2587 | * @return string |
||
2588 | */ |
||
2589 | protected function _quote_one_identifier($identifier) |
||
2596 | |||
2597 | 207 | /** |
|
2598 | * Quote a string that is used as an identifier |
||
2599 | * (table names, column names etc) or an array containing |
||
2600 | 207 | * multiple identifiers. This method can also deal with |
|
2601 | * dot-separated identifiers eg table.column |
||
2602 | * |
||
2603 | * @param array|string $identifier |
||
2604 | * |
||
2605 | * @return string |
||
2606 | */ |
||
2607 | protected function _quote_identifier($identifier) |
||
2617 | 3 | ||
2618 | 1 | /** |
|
2619 | 3 | * This method performs the actual quoting of a single |
|
2620 | 1 | * part of an identifier, using the identifier quote |
|
2621 | 1 | * character specified in the config (or autodetected). |
|
2622 | * |
||
2623 | 1 | * @param string $part |
|
2624 | 1 | * |
|
2625 | 1 | * @return string |
|
2626 | 1 | */ |
|
2627 | protected function _quote_identifier_part($part) |
||
2638 | |||
2639 | /** |
||
2640 | * Create a cache key for the given query and parameters. |
||
2641 | * |
||
2642 | * @param string $query |
||
2643 | * @param array $parameters |
||
2644 | * @param null|string $table_name |
||
2645 | * @param string $connection_name |
||
2646 | 3 | * |
|
2647 | * @return mixed|string |
||
2648 | */ |
||
2649 | 3 | protected static function _create_cache_key($query, $parameters, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2671 | |||
2672 | /** |
||
2673 | * Check the query cache for the given cache key. If a value |
||
2674 | * is cached for the key, return the value. Otherwise, return false. |
||
2675 | * |
||
2676 | 1 | * @param string $cache_key |
|
2677 | * @param null|string $table_name |
||
2678 | * @param string $connection_name |
||
2679 | 1 | * |
|
2680 | * @return bool|mixed |
||
2681 | */ |
||
2682 | 1 | protected static function _check_query_cache($cache_key, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2703 | |||
2704 | /** |
||
2705 | * Clear the query cache |
||
2706 | * |
||
2707 | * @param null|string $table_name |
||
2708 | 3 | * @param string $connection_name |
|
2709 | * |
||
2710 | * @return bool|mixed |
||
2711 | 3 | */ |
|
2712 | 3 | public static function clear_cache($table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2733 | |||
2734 | /** |
||
2735 | * Add the given value to the query cache. |
||
2736 | * |
||
2737 | * @param string $cache_key |
||
2738 | * @param string $value |
||
2739 | 199 | * @param null|string $table_name |
|
2740 | * @param string $connection_name |
||
2741 | * |
||
2742 | 199 | * @return bool|mixed |
|
2743 | */ |
||
2744 | 199 | protected static function _cache_query_result($cache_key, $value, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2770 | |||
2771 | /** |
||
2772 | 199 | * Execute the SELECT query that has been built up by chaining methods |
|
2773 | 199 | * on this class. Return an array of rows as associative arrays. |
|
2774 | 199 | */ |
|
2775 | protected function _run() |
||
2814 | 4 | ||
2815 | /** |
||
2816 | 31 | * Return the raw data wrapped by this ORM |
|
2817 | * instance as an associative array. Column |
||
2818 | * names may optionally be supplied as arguments, |
||
2819 | * if so, only those keys will be returned. |
||
2820 | */ |
||
2821 | public function as_array() |
||
2830 | 37 | ||
2831 | 2 | /** |
|
2832 | * Return the raw data wrapped by this ORM |
||
2833 | * instance as an json. |
||
2834 | 35 | * |
|
2835 | * @param int $options |
||
2836 | * |
||
2837 | * @return string |
||
2838 | */ |
||
2839 | public function as_json($options = 0) |
||
2843 | |||
2844 | /** |
||
2845 | * Return the value of a property of this object (database row) |
||
2846 | 33 | * or null if not present. |
|
2847 | * |
||
2848 | 33 | * If a column-names array is passed, it will return a associative array |
|
2849 | * with the value of each column or null if it is not present. |
||
2850 | 33 | * |
|
2851 | 23 | * @param mixed $key |
|
2852 | 3 | * |
|
2853 | 3 | * @return mixed |
|
2854 | 1 | */ |
|
2855 | public function get($key) |
||
2868 | |||
2869 | /** |
||
2870 | * Return the name of the column in the database table which contains |
||
2871 | * the primary key ID of the row. |
||
2872 | */ |
||
2873 | protected function _get_id_column_name() |
||
2885 | |||
2886 | /** |
||
2887 | * Get the primary key ID of this object. |
||
2888 | * |
||
2889 | * @param bool $disallow_null |
||
2890 | * |
||
2891 | * @return mixed |
||
2892 | * |
||
2893 | * @throws \Exception |
||
2894 | 10 | */ |
|
2895 | public function id($disallow_null = false) |
||
2913 | |||
2914 | /** |
||
2915 | 29 | * Set a property to a particular value on this object. |
|
2916 | 29 | * To set multiple properties at once, pass an associative array |
|
2917 | 29 | * as the first parameter and leave out the second parameter. |
|
2918 | 29 | * Flags the properties as 'dirty' so they will be saved to the |
|
2919 | 2 | * database when save() is called. |
|
2920 | 29 | * |
|
2921 | 10 | * @param mixed $key |
|
2922 | 10 | * @param mixed $value |
|
2923 | 29 | * |
|
2924 | * @return ORM |
||
2925 | 29 | */ |
|
2926 | public function set($key, $value = null) |
||
2930 | |||
2931 | /** |
||
2932 | * Set a property to a particular value on this object. |
||
2933 | * To set multiple properties at once, pass an associative array |
||
2934 | * as the first parameter and leave out the second parameter. |
||
2935 | * Flags the properties as 'dirty' so they will be saved to the |
||
2936 | 1 | * database when save() is called. |
|
2937 | * |
||
2938 | 1 | * @param string|array $key |
|
2939 | * @param string|null $value |
||
2940 | * |
||
2941 | * @return ORM |
||
2942 | */ |
||
2943 | public function set_expr($key, $value = null) |
||
2947 | |||
2948 | 2 | /** |
|
2949 | * Set a property on the ORM object. |
||
2950 | * |
||
2951 | * @param string|array $key |
||
2952 | * @param string|null $value |
||
2953 | * @param bool $expr |
||
2954 | * |
||
2955 | * @return $this |
||
2956 | */ |
||
2957 | protected function _set_orm_property($key, $value = null, $expr = false) |
||
2976 | |||
2977 | 17 | /** |
|
2978 | 17 | * Check whether the given field has been changed since this |
|
2979 | * object was saved. |
||
2980 | 16 | * |
|
2981 | 1 | * @param string $key |
|
2982 | 1 | * |
|
2983 | 15 | * @return bool |
|
2984 | */ |
||
2985 | public function is_dirty($key) |
||
2989 | 12 | ||
2990 | /** |
||
2991 | * Check whether the model was the result of a call to create() or not |
||
2992 | 27 | * |
|
2993 | 27 | * @return bool |
|
2994 | */ |
||
2995 | 27 | public function is_new() |
|
2999 | |||
3000 | 27 | /** |
|
3001 | * Save any fields which have been modified on this object |
||
3002 | 12 | * to the database. |
|
3003 | 12 | * |
|
3004 | 9 | * @return bool |
|
3005 | * |
||
3006 | 9 | * @throws \Exception |
|
3007 | */ |
||
3008 | public function save() |
||
3080 | 17 | ||
3081 | /** |
||
3082 | 17 | * Add a WHERE clause for every column that belongs to the primary key |
|
3083 | * |
||
3084 | * @param array $query warning: this is a reference |
||
3085 | */ |
||
3086 | public function _add_id_column_conditions(&$query) |
||
3109 | 4 | ||
3110 | /** |
||
3111 | * Build an UPDATE query |
||
3112 | 4 | * |
|
3113 | 4 | * @return string |
|
3114 | 4 | */ |
|
3115 | 4 | protected function _build_update() |
|
3135 | 2 | ||
3136 | 2 | /** |
|
3137 | * Build an INSERT query |
||
3138 | 2 | * |
|
3139 | * @return string |
||
3140 | 2 | */ |
|
3141 | protected function _build_insert() |
||
3158 | |||
3159 | 16 | /** |
|
3160 | * Delete this record from the database |
||
3161 | * |
||
3162 | 16 | * @return bool |
|
3163 | 16 | */ |
|
3164 | public function delete() |
||
3178 | |||
3179 | 13 | /** |
|
3180 | * Delete many records from the database |
||
3181 | 13 | * |
|
3182 | 13 | * @return bool |
|
3183 | */ |
||
3184 | public function delete_many() |
||
3199 | |||
3200 | // --------------------- // |
||
3201 | // --- ArrayAccess --- // |
||
3202 | // --------------------- // |
||
3203 | |||
3204 | /** |
||
3205 | * @param mixed $key |
||
3206 | * |
||
3207 | * @return bool |
||
3208 | */ |
||
3209 | 81 | public function offsetExists($key) |
|
3213 | 81 | ||
3214 | 80 | /** |
|
3215 | * @param mixed $key |
||
3216 | 1 | * |
|
3217 | * @return mixed |
||
3218 | */ |
||
3219 | public function offsetGet($key) |
||
3223 | |||
3224 | /** |
||
3225 | * @param mixed $key |
||
3226 | * @param mixed $value |
||
3227 | */ |
||
3228 | public function offsetSet($key, $value) |
||
3235 | 87 | ||
3236 | /** |
||
3237 | 87 | * @param mixed $key |
|
3238 | */ |
||
3239 | public function offsetUnset($key) |
||
3244 | |||
3245 | // --------------------- // |
||
3246 | // --- MAGIC METHODS --- // |
||
3247 | // --------------------- // |
||
3248 | |||
3249 | /** |
||
3250 | * @param $key |
||
3251 | * |
||
3252 | * @return mixed |
||
3253 | */ |
||
3254 | public function __get($key) |
||
3258 | |||
3259 | /** |
||
3260 | * @param $key |
||
3261 | * @param $value |
||
3262 | */ |
||
3263 | public function __set($key, $value) |
||
3267 | |||
3268 | /** |
||
3269 | * @param $key |
||
3270 | */ |
||
3271 | public function __unset($key) |
||
3275 | |||
3276 | /** |
||
3277 | * @param $key |
||
3278 | * |
||
3279 | * @return bool |
||
3280 | */ |
||
3281 | public function __isset($key) |
||
3285 | |||
3286 | /** |
||
3287 | * Magic method to capture calls to undefined class methods. |
||
3288 | * In this case we are attempting to convert camel case formatted |
||
3289 | * methods into underscore formatted methods. |
||
3290 | * |
||
3291 | * This allows us to call ORM methods using camel case and remain |
||
3292 | * backwards compatible. |
||
3293 | * |
||
3294 | * @param string $name |
||
3295 | * @param array $arguments |
||
3296 | * |
||
3297 | * @return ORM |
||
3298 | * |
||
3299 | * @throws IdiormMethodMissingException |
||
3300 | */ |
||
3301 | public function __call($name, $arguments) |
||
3311 | |||
3312 | /** |
||
3313 | * Magic method to capture calls to undefined static class methods. |
||
3314 | * In this case we are attempting to convert camel case formatted |
||
3315 | * methods into underscore formatted methods. |
||
3316 | * |
||
3317 | * This allows us to call ORM methods using camel case and remain |
||
3318 | * backwards compatible. |
||
3319 | * |
||
3320 | * @param string $name |
||
3321 | * @param array $arguments |
||
3322 | * |
||
3323 | * @return ORM |
||
3324 | */ |
||
3325 | public static function __callStatic($name, $arguments) |
||
3331 | |||
3332 | } |
||
3333 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: