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 |
||
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 |
||
218 | */ |
||
219 | protected $_limit = null; |
||
220 | |||
221 | /** |
||
222 | * OFFSET |
||
223 | * |
||
224 | * @var null |
||
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 $key |
||
314 | * @param mixed $value |
||
315 | * @param string $connection_name Which connection to use |
||
316 | */ |
||
317 | 138 | public static function configure($key, $value = null, $connection_name = self::DEFAULT_CONNECTION) |
|
318 | { |
||
319 | 138 | static::_setup_db_config($connection_name); //ensures at least default config is set |
|
320 | |||
321 | 138 | 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 | 138 | if (is_null($value)) { |
|
329 | // Shortcut: If only one string argument is passed, |
||
330 | // assume it's a connection string |
||
331 | $value = $key; |
||
332 | $key = 'connection_string'; |
||
333 | } |
||
334 | 138 | static::$_config[$connection_name][$key] = $value; |
|
335 | } |
||
336 | 138 | } |
|
337 | |||
338 | /** |
||
339 | * Retrieve configuration options by key, or as whole array. |
||
340 | * |
||
341 | * @param string $key |
||
342 | * @param string $connection_name Which connection to use |
||
343 | */ |
||
344 | 2 | public static function get_config($key = null, $connection_name = self::DEFAULT_CONNECTION) |
|
345 | { |
||
346 | 2 | if ($key) { |
|
|
|||
347 | 1 | return static::$_config[$connection_name][$key]; |
|
348 | } else { |
||
349 | 1 | return static::$_config[$connection_name]; |
|
350 | } |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Delete all configs in _config array. |
||
355 | */ |
||
356 | 138 | public static function reset_config() |
|
360 | |||
361 | /** |
||
362 | * Despite its slightly odd name, this is actually the factory |
||
363 | * method used to acquire instances of the class. It is named |
||
364 | * this way for the sake of a readable interface, ie |
||
365 | * ORM::for_table('table_name')->find_one()-> etc. As such, |
||
366 | * this will normally be the first method called in a chain. |
||
367 | * |
||
368 | * @param string $table_name |
||
369 | * @param string $connection_name Which connection to use |
||
370 | * |
||
371 | * @return ORM |
||
372 | */ |
||
373 | 126 | public static function for_table($table_name, $connection_name = self::DEFAULT_CONNECTION) |
|
374 | { |
||
375 | 126 | static::_setup_db($connection_name); |
|
376 | |||
377 | 126 | return new static($table_name, array(), $connection_name); |
|
378 | } |
||
379 | |||
380 | /** |
||
381 | * Set up the database connection used by the class |
||
382 | * |
||
383 | * @param string $connection_name Which connection to use |
||
384 | */ |
||
385 | 138 | protected static function _setup_db($connection_name = self::DEFAULT_CONNECTION) |
|
386 | { |
||
387 | 138 | if (!array_key_exists($connection_name, static::$_db) || |
|
388 | 138 | !is_object(static::$_db[$connection_name]) |
|
389 | 138 | ) { |
|
390 | 1 | static::_setup_db_config($connection_name); |
|
391 | |||
392 | 1 | $db = new \PDO( |
|
393 | 1 | static::$_config[$connection_name]['connection_string'], static::$_config[$connection_name]['username'], static::$_config[$connection_name]['password'], static::$_config[$connection_name]['driver_options'] |
|
394 | 1 | ); |
|
395 | |||
396 | 1 | $db->setAttribute(\PDO::ATTR_ERRMODE, static::$_config[$connection_name]['error_mode']); |
|
397 | 1 | static::set_db($db, $connection_name); |
|
398 | 1 | } |
|
399 | 138 | } |
|
400 | |||
401 | /** |
||
402 | * Ensures configuration (multiple connections) is at least set to default. |
||
403 | * |
||
404 | * @param string $connection_name Which connection to use |
||
405 | */ |
||
406 | 138 | protected static function _setup_db_config($connection_name) |
|
407 | { |
||
408 | 138 | if (!array_key_exists($connection_name, static::$_config)) { |
|
409 | 138 | static::$_config[$connection_name] = static::$_default_config; |
|
410 | 138 | } |
|
411 | 138 | } |
|
412 | |||
413 | /** |
||
414 | * Set the PDO object used by Idiorm to communicate with the database. |
||
415 | * This is public in case the ORM should use a ready-instantiated |
||
416 | * PDO object as its database connection. Accepts an optional string key |
||
417 | * to identify the connection if multiple connections are used. |
||
418 | * |
||
419 | * @param \PDO $db |
||
420 | * @param string $connection_name Which connection to use |
||
421 | */ |
||
422 | 138 | public static function set_db($db, $connection_name = self::DEFAULT_CONNECTION) |
|
423 | { |
||
424 | 138 | static::_setup_db_config($connection_name); |
|
425 | 138 | static::$_db[$connection_name] = $db; |
|
426 | 138 | if (!is_null(static::$_db[$connection_name])) { |
|
427 | 138 | static::_setup_identifier_quote_character($connection_name); |
|
428 | 138 | static::_setup_limit_clause_style($connection_name); |
|
429 | 138 | } |
|
430 | 138 | } |
|
431 | |||
432 | /** |
||
433 | * Delete all registered PDO objects in _db array. |
||
434 | */ |
||
435 | 138 | public static function reset_db() |
|
439 | |||
440 | /** |
||
441 | * Detect and initialise the character used to quote identifiers |
||
442 | * (table names, column names etc). If this has been specified |
||
443 | * manually using ORM::configure('identifier_quote_character', 'some-char'), |
||
444 | * this will do nothing. |
||
445 | * |
||
446 | * @param string $connection_name Which connection to use |
||
447 | */ |
||
448 | 138 | protected static function _setup_identifier_quote_character($connection_name) |
|
449 | { |
||
450 | 138 | if (is_null(static::$_config[$connection_name]['identifier_quote_character'])) { |
|
451 | 138 | static::$_config[$connection_name]['identifier_quote_character'] = static::_detect_identifier_quote_character($connection_name); |
|
452 | 138 | } |
|
453 | 138 | } |
|
454 | |||
455 | /** |
||
456 | * Detect and initialise the limit clause style ("SELECT TOP 5" / |
||
457 | * "... LIMIT 5"). If this has been specified manually using |
||
458 | * ORM::configure('limit_clause_style', 'top'), this will do nothing. |
||
459 | * |
||
460 | * @param string $connection_name Which connection to use |
||
461 | */ |
||
462 | 138 | public static function _setup_limit_clause_style($connection_name) |
|
463 | { |
||
464 | 138 | if (is_null(static::$_config[$connection_name]['limit_clause_style'])) { |
|
465 | 138 | static::$_config[$connection_name]['limit_clause_style'] = static::_detect_limit_clause_style($connection_name); |
|
466 | 138 | } |
|
467 | 138 | } |
|
468 | |||
469 | /** |
||
470 | * Return the correct character used to quote identifiers (table |
||
471 | * names, column names etc) by looking at the driver being used by PDO. |
||
472 | * |
||
473 | * @param string $connection_name Which connection to use |
||
474 | * |
||
475 | * @return string |
||
476 | */ |
||
477 | 138 | protected static function _detect_identifier_quote_character($connection_name) |
|
494 | |||
495 | /** |
||
496 | * Returns a constant after determining the appropriate limit clause |
||
497 | * style |
||
498 | * |
||
499 | * @param string $connection_name Which connection to use |
||
500 | * |
||
501 | * @return string Limit clause style keyword/constant |
||
502 | */ |
||
503 | 138 | protected static function _detect_limit_clause_style($connection_name) |
|
514 | |||
515 | /** |
||
516 | * Returns the PDO instance used by the the ORM to communicate with |
||
517 | * the database. This can be called if any low-level DB access is |
||
518 | * required outside the class. If multiple connections are used, |
||
519 | * accepts an optional key name for the connection. |
||
520 | * |
||
521 | * @param string $connection_name Which connection to use |
||
522 | * |
||
523 | * @return \PDO |
||
524 | */ |
||
525 | 138 | public static function get_db($connection_name = self::DEFAULT_CONNECTION) |
|
526 | { |
||
527 | 138 | static::_setup_db($connection_name); // required in case this is called before Idiorm is instantiated |
|
528 | 138 | return static::$_db[$connection_name]; |
|
529 | } |
||
530 | |||
531 | /** |
||
532 | * Executes a raw query as a wrapper for PDOStatement::execute. |
||
533 | * Useful for queries that can't be accomplished through Idiorm, |
||
534 | * particularly those using engine-specific features. |
||
535 | * |
||
536 | * @example raw_execute('SELECT `name`, AVG(`order`) FROM `customer` GROUP BY `name` HAVING AVG(`order`) > 10') |
||
537 | * @example raw_execute('INSERT OR REPLACE INTO `widget` (`id`, `name`) SELECT `id`, `name` FROM `other_table`') |
||
538 | * |
||
539 | * @param string $query The raw SQL query |
||
540 | * @param array $parameters Optional bound parameters |
||
541 | * @param string $connection_name Which connection to use |
||
542 | * |
||
543 | * @return bool Success |
||
544 | */ |
||
545 | 1 | public static function raw_execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) |
|
546 | { |
||
547 | 1 | static::_setup_db($connection_name); |
|
548 | |||
549 | 1 | return static::_execute($query, $parameters, $connection_name); |
|
550 | } |
||
551 | |||
552 | /** |
||
553 | * Returns the PDOStatement instance last used by any connection wrapped by the ORM. |
||
554 | * Useful for access to PDOStatement::rowCount() or error information |
||
555 | * |
||
556 | * @return \PDOStatement |
||
557 | */ |
||
558 | 111 | public static function get_last_statement() |
|
562 | |||
563 | /** |
||
564 | * Internal helper method for executing statements. Logs queries, and |
||
565 | * stores statement object in ::_last_statement, accessible publicly |
||
566 | * through ::get_last_statement() |
||
567 | * |
||
568 | * @param string $query |
||
569 | * @param array $parameters An array of parameters to be bound in to the query |
||
570 | * @param string $connection_name Which connection to use |
||
571 | * |
||
572 | * @return bool Response of PDOStatement::execute() |
||
573 | * |
||
574 | * @throws \Exception |
||
575 | */ |
||
576 | 119 | protected static function _execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) |
|
609 | |||
610 | /** |
||
611 | * Add a query to the internal query log. Only works if the |
||
612 | * 'logging' config option is set to true. |
||
613 | * |
||
614 | * This works by manually binding the parameters to the query - the |
||
615 | * query isn't executed like this (PDO normally passes the query and |
||
616 | * parameters to the database which takes care of the binding) but |
||
617 | * doing it this way makes the logged queries more readable. |
||
618 | * |
||
619 | * @param string $query |
||
620 | * @param array $parameters An array of parameters to be bound in to the query |
||
621 | * @param string $connection_name Which connection to use |
||
622 | * @param float $query_time Query time |
||
623 | * |
||
624 | * @return bool |
||
625 | */ |
||
626 | 119 | protected static function _log_query($query, $parameters, $connection_name, $query_time) |
|
675 | |||
676 | /** |
||
677 | * Get the last query executed. Only works if the |
||
678 | * 'logging' config option is set to true. Otherwise |
||
679 | * this will return null. Returns last query from all connections if |
||
680 | * no connection_name is specified |
||
681 | * |
||
682 | * @param null|string $connection_name Which connection to use |
||
683 | * |
||
684 | * @return string |
||
685 | */ |
||
686 | 111 | public static function get_last_query($connection_name = null) |
|
697 | |||
698 | /** |
||
699 | * Get an array containing all the queries run on a |
||
700 | * specified connection up to now. |
||
701 | * Only works if the 'logging' config option is |
||
702 | * set to true. Otherwise, returned array will be empty. |
||
703 | * |
||
704 | * @param string $connection_name Which connection to use |
||
705 | * |
||
706 | * @return array |
||
707 | */ |
||
708 | public static function get_query_log($connection_name = self::DEFAULT_CONNECTION) |
||
716 | |||
717 | /** |
||
718 | * Get a list of the available connection names |
||
719 | * |
||
720 | * @return array |
||
721 | */ |
||
722 | public static function get_connection_names() |
||
726 | |||
727 | // ------------------------ // |
||
728 | // --- INSTANCE METHODS --- // |
||
729 | // ------------------------ // |
||
730 | |||
731 | /** |
||
732 | * "Private" constructor; shouldn't be called directly. |
||
733 | * Use the ORM::for_table factory method instead. |
||
734 | * |
||
735 | * @param string $table_name |
||
736 | * @param array $data |
||
737 | * @param string $connection_name |
||
738 | */ |
||
739 | 126 | protected function __construct($table_name, $data = array(), $connection_name = self::DEFAULT_CONNECTION) |
|
747 | |||
748 | /** |
||
749 | * Create a new, empty instance of the class. Used |
||
750 | * to add a new row to your database. May optionally |
||
751 | * be passed an associative array of data to populate |
||
752 | * the instance. If so, all fields will be flagged as |
||
753 | * dirty so all will be saved to the database when |
||
754 | * save() is called. |
||
755 | * |
||
756 | * @param mixed $data |
||
757 | * |
||
758 | * @return $this |
||
759 | */ |
||
760 | 11 | public function create($data = null) |
|
769 | |||
770 | /** |
||
771 | * Specify the ID column to use for this instance or array of instances only. |
||
772 | * This overrides the id_column and id_column_overrides settings. |
||
773 | * |
||
774 | * This is mostly useful for libraries built on top of Idiorm, and will |
||
775 | * not normally be used in manually built queries. If you don't know why |
||
776 | * you would want to use this, you should probably just ignore it. |
||
777 | * |
||
778 | * @param string $id_column |
||
779 | * |
||
780 | * @return $this |
||
781 | */ |
||
782 | 114 | public function use_id_column($id_column) |
|
788 | |||
789 | /** |
||
790 | * Create an ORM instance from the given row (an associative |
||
791 | * array of data fetched from the database) |
||
792 | * |
||
793 | * @param array $row |
||
794 | * |
||
795 | * @return ORM |
||
796 | */ |
||
797 | 110 | protected function _create_instance_from_row($row) |
|
805 | |||
806 | /** |
||
807 | * Tell the ORM that you are expecting a single result |
||
808 | * back from your query, and execute it. Will return |
||
809 | * a single instance of the ORM class, or false if no |
||
810 | * rows were returned. |
||
811 | * As a shortcut, you may supply an ID as a parameter |
||
812 | * to this method. This will perform a primary key |
||
813 | * lookup on the table. |
||
814 | * |
||
815 | * @param mixed $id |
||
816 | * |
||
817 | * @return false|ORM false on error |
||
818 | */ |
||
819 | 50 | public function find_one($id = null) |
|
833 | |||
834 | /** |
||
835 | * Tell the ORM that you are expecting multiple results |
||
836 | * from your query, and execute it. Will return an array |
||
837 | * of instances of the ORM class, or an empty array if |
||
838 | * no rows were returned. |
||
839 | * |
||
840 | * @return array|IdiormResultSet |
||
841 | */ |
||
842 | 58 | public function find_many() |
|
850 | |||
851 | /** |
||
852 | * Tell the ORM that you are expecting multiple results |
||
853 | * from your query, and execute it. Will return an array |
||
854 | * of instances of the ORM class, or an empty array if |
||
855 | * no rows were returned. |
||
856 | * |
||
857 | * @return array |
||
858 | */ |
||
859 | 60 | protected function _find_many() |
|
865 | |||
866 | /** |
||
867 | * Tell the ORM that you are expecting multiple results |
||
868 | * from your query, and execute it. Will return a result set object |
||
869 | * containing instances of the ORM class. |
||
870 | * |
||
871 | * @return IdiormResultSet |
||
872 | */ |
||
873 | 3 | public function find_result_set() |
|
877 | |||
878 | /** |
||
879 | * Tell the ORM that you are expecting multiple results |
||
880 | * from your query, and execute it. Will return an array, |
||
881 | * or an empty array if no rows were returned. |
||
882 | * |
||
883 | * @return array |
||
884 | */ |
||
885 | 1 | public function find_array() |
|
889 | |||
890 | /** |
||
891 | * Tell the ORM that you wish to execute a COUNT query. |
||
892 | * Will return an integer representing the number of |
||
893 | * rows returned. |
||
894 | * |
||
895 | * @param string $column |
||
896 | * |
||
897 | * @return int |
||
898 | */ |
||
899 | 2 | public function count($column = '*') |
|
903 | |||
904 | /** |
||
905 | * Tell the ORM that you wish to execute a MAX query. |
||
906 | * Will return the max value of the choosen column. |
||
907 | * |
||
908 | * @param string $column |
||
909 | * |
||
910 | * @return int |
||
911 | */ |
||
912 | 1 | public function max($column) |
|
916 | |||
917 | /** |
||
918 | * Tell the ORM that you wish to execute a MIN query. |
||
919 | * Will return the min value of the choosen column. |
||
920 | * |
||
921 | * @param string $column |
||
922 | * |
||
923 | * @return int |
||
924 | */ |
||
925 | 1 | public function min($column) |
|
929 | |||
930 | /** |
||
931 | * Tell the ORM that you wish to execute a AVG query. |
||
932 | * Will return the average value of the choosen column. |
||
933 | * |
||
934 | * @param string $column |
||
935 | * |
||
936 | * @return int |
||
937 | */ |
||
938 | 1 | public function avg($column) |
|
942 | |||
943 | /** |
||
944 | * Tell the ORM that you wish to execute a SUM query. |
||
945 | * Will return the sum of the choosen column. |
||
946 | * |
||
947 | * @param string $column |
||
948 | * |
||
949 | * @return int |
||
950 | */ |
||
951 | 1 | public function sum($column) |
|
955 | |||
956 | /** |
||
957 | * Execute an aggregate query on the current connection. |
||
958 | * |
||
959 | * @param string $sql_function The aggregate function to call eg. MIN, COUNT, etc |
||
960 | * @param string $column The column to execute the aggregate query against |
||
961 | * |
||
962 | * @return int |
||
963 | */ |
||
964 | 6 | protected function _call_aggregate_db_function($sql_function, $column) |
|
990 | |||
991 | /** |
||
992 | * This method can be called to hydrate (populate) this |
||
993 | * instance of the class from an associative array of data. |
||
994 | * This will usually be called only from inside the class, |
||
995 | * but it's public in case you need to call it directly. |
||
996 | * |
||
997 | * @param array $data |
||
998 | * |
||
999 | * @return $this |
||
1000 | */ |
||
1001 | 112 | public function hydrate($data = array()) |
|
1007 | |||
1008 | /** |
||
1009 | * Force the ORM to flag all the fields in the $data array |
||
1010 | * as "dirty" and therefore update them when save() is called. |
||
1011 | */ |
||
1012 | 2 | public function force_all_dirty() |
|
1018 | |||
1019 | /** |
||
1020 | * Perform a raw query. The query can contain placeholders in |
||
1021 | * either named or question mark style. If placeholders are |
||
1022 | * used, the parameters should be an array of values which will |
||
1023 | * be bound to the placeholders in the query. If this method |
||
1024 | * is called, all other query building methods will be ignored. |
||
1025 | * |
||
1026 | * @param string $query |
||
1027 | * @param array $parameters |
||
1028 | * |
||
1029 | * @return $this |
||
1030 | */ |
||
1031 | 2 | public function raw_query($query, $parameters = array()) |
|
1039 | |||
1040 | /** |
||
1041 | * Add an alias for the main table to be used in SELECT queries |
||
1042 | * |
||
1043 | * @param string $alias |
||
1044 | * |
||
1045 | * @return $this |
||
1046 | */ |
||
1047 | 2 | public function table_alias($alias) |
|
1053 | |||
1054 | /** |
||
1055 | * Internal method to add an unquoted expression to the set |
||
1056 | * of columns returned by the SELECT query. The second optional |
||
1057 | * argument is the alias to return the expression as. |
||
1058 | * |
||
1059 | * @param string $expr |
||
1060 | * @param mixed $alias |
||
1061 | * |
||
1062 | * @return $this |
||
1063 | */ |
||
1064 | 19 | protected function _add_result_column($expr, $alias = null) |
|
1079 | |||
1080 | /** |
||
1081 | * Counts the number of columns that belong to the primary |
||
1082 | * key and their value is null. |
||
1083 | */ |
||
1084 | 6 | public function count_null_id_columns() |
|
1092 | |||
1093 | /** |
||
1094 | * Add a column to the list of columns returned by the SELECT |
||
1095 | * query. This defaults to '*'. The second optional argument is |
||
1096 | * the alias to return the column as. |
||
1097 | * |
||
1098 | * @param string $column |
||
1099 | * @param mixed $alias |
||
1100 | * |
||
1101 | * @return ORM |
||
1102 | */ |
||
1103 | 12 | public function select($column, $alias = null) |
|
1109 | |||
1110 | /** |
||
1111 | * Add an unquoted expression to the list of columns returned |
||
1112 | * by the SELECT query. The second optional argument is |
||
1113 | * the alias to return the column as. |
||
1114 | * |
||
1115 | * @param string $expr |
||
1116 | * @param mixed $alias |
||
1117 | * |
||
1118 | * @return ORM |
||
1119 | */ |
||
1120 | 8 | public function select_expr($expr, $alias = null) |
|
1124 | |||
1125 | /** |
||
1126 | * Add columns to the list of columns returned by the SELECT |
||
1127 | * query. This defaults to '*'. Many columns can be supplied |
||
1128 | * as either an array or as a list of parameters to the method. |
||
1129 | * |
||
1130 | * Note that the alias must not be numeric - if you want a |
||
1131 | * numeric alias then prepend it with some alpha chars. eg. a1 |
||
1132 | * |
||
1133 | * @example select_many(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5'); |
||
1134 | * @example select_many('column', 'column2', 'column3'); |
||
1135 | * @example select_many(array('column', 'column2', 'column3'), 'column4', 'column5'); |
||
1136 | * |
||
1137 | * @return ORM |
||
1138 | */ |
||
1139 | 1 | View Code Duplication | public function select_many() |
1154 | |||
1155 | /** |
||
1156 | * Add an unquoted expression to the list of columns returned |
||
1157 | * by the SELECT query. Many columns can be supplied as either |
||
1158 | * an array or as a list of parameters to the method. |
||
1159 | * |
||
1160 | * Note that the alias must not be numeric - if you want a |
||
1161 | * numeric alias then prepend it with some alpha chars. eg. a1 |
||
1162 | * |
||
1163 | * @example select_many_expr(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5') |
||
1164 | * @example select_many_expr('column', 'column2', 'column3') |
||
1165 | * @example select_many_expr(array('column', 'column2', 'column3'), 'column4', 'column5') |
||
1166 | * |
||
1167 | * @return ORM |
||
1168 | */ |
||
1169 | 1 | View Code Duplication | public function select_many_expr() |
1184 | |||
1185 | /** |
||
1186 | * Take a column specification for the select many methods and convert it |
||
1187 | * into a normalised array of columns and aliases. |
||
1188 | * |
||
1189 | * It is designed to turn the following styles into a normalised array: |
||
1190 | * |
||
1191 | * array(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5')) |
||
1192 | * |
||
1193 | * @param array $columns |
||
1194 | * |
||
1195 | * @return array |
||
1196 | */ |
||
1197 | 2 | protected function _normalise_select_many_columns($columns) |
|
1216 | |||
1217 | /** |
||
1218 | * Add a DISTINCT keyword before the list of columns in the SELECT query |
||
1219 | */ |
||
1220 | 1 | public function distinct() |
|
1226 | |||
1227 | /** |
||
1228 | * Internal method to add a JOIN source to the query. |
||
1229 | * |
||
1230 | * The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this |
||
1231 | * will be prepended to JOIN. |
||
1232 | * |
||
1233 | * The table should be the name of the table to join to. |
||
1234 | * |
||
1235 | * The constraint may be either a string or an array with three elements. If it |
||
1236 | * is a string, it will be compiled into the query as-is, with no escaping. The |
||
1237 | * recommended way to supply the constraint is as an array with three elements: |
||
1238 | * |
||
1239 | * first_column, operator, second_column |
||
1240 | * |
||
1241 | * Example: array('user.id', '=', 'profile.user_id') |
||
1242 | * |
||
1243 | * will compile to |
||
1244 | * |
||
1245 | * ON `user`.`id` = `profile`.`user_id` |
||
1246 | * |
||
1247 | * The final (optional) argument specifies an alias for the joined table. |
||
1248 | * |
||
1249 | * @param string $join_operator |
||
1250 | * @param string $table |
||
1251 | * @param string $constraint |
||
1252 | * @param string|null $table_alias |
||
1253 | * |
||
1254 | * @return $this |
||
1255 | */ |
||
1256 | 10 | protected function _add_join_source($join_operator, $table, $constraint, $table_alias = null) |
|
1281 | |||
1282 | /** |
||
1283 | * Add a RAW JOIN source to the query |
||
1284 | * |
||
1285 | * @param string $table |
||
1286 | * @param string $constraint |
||
1287 | * @param string $table_alias |
||
1288 | * @param array $parameters |
||
1289 | * |
||
1290 | * @return $this |
||
1291 | */ |
||
1292 | 3 | public function raw_join($table, $constraint, $table_alias, $parameters = array()) |
|
1314 | |||
1315 | /** |
||
1316 | * Add a simple JOIN source to the query |
||
1317 | * |
||
1318 | * @param string $table |
||
1319 | * @param string $constraint |
||
1320 | * @param string|null $table_alias |
||
1321 | * |
||
1322 | * @return ORM |
||
1323 | */ |
||
1324 | 6 | public function join($table, $constraint, $table_alias = null) |
|
1328 | |||
1329 | /** |
||
1330 | * Add an INNER JOIN souce to the query |
||
1331 | */ |
||
1332 | /** |
||
1333 | * @param string $table |
||
1334 | * @param string $constraint |
||
1335 | * @param null|string $table_alias |
||
1336 | * |
||
1337 | * @return ORM |
||
1338 | */ |
||
1339 | 1 | public function inner_join($table, $constraint, $table_alias = null) |
|
1343 | |||
1344 | /** |
||
1345 | * Add a LEFT OUTER JOIN souce to the query |
||
1346 | * |
||
1347 | * @param string $table |
||
1348 | * @param string $constraint |
||
1349 | * @param null|string $table_alias |
||
1350 | * |
||
1351 | * @return $this|ORM |
||
1352 | */ |
||
1353 | 1 | public function left_outer_join($table, $constraint, $table_alias = null) |
|
1357 | |||
1358 | /** |
||
1359 | * Add an RIGHT OUTER JOIN souce to the query |
||
1360 | * |
||
1361 | * @param string $table |
||
1362 | * @param string $constraint |
||
1363 | * @param null|string $table_alias |
||
1364 | * |
||
1365 | * @return $this|ORM |
||
1366 | */ |
||
1367 | 1 | public function right_outer_join($table, $constraint, $table_alias = null) |
|
1371 | |||
1372 | /** |
||
1373 | * Add an FULL OUTER JOIN souce to the query |
||
1374 | */ |
||
1375 | /** |
||
1376 | * @param string $table |
||
1377 | * @param string $constraint |
||
1378 | * @param null|string $table_alias |
||
1379 | * |
||
1380 | * @return ORM |
||
1381 | */ |
||
1382 | 1 | public function full_outer_join($table, $constraint, $table_alias = null) |
|
1386 | |||
1387 | /** |
||
1388 | * Internal method to add a HAVING condition to the query |
||
1389 | */ |
||
1390 | /** |
||
1391 | * @param string $fragment |
||
1392 | * @param array $values |
||
1393 | * |
||
1394 | * @return ORM |
||
1395 | */ |
||
1396 | 5 | protected function _add_having($fragment, $values = array()) |
|
1400 | |||
1401 | /** |
||
1402 | * Internal method to add a HAVING condition to the query |
||
1403 | */ |
||
1404 | /** |
||
1405 | * @param string|array $column_name |
||
1406 | * @param string $separator |
||
1407 | * @param mixed $value |
||
1408 | * |
||
1409 | * @return $this|ORM |
||
1410 | */ |
||
1411 | 7 | protected function _add_simple_having($column_name, $separator, $value) |
|
1415 | |||
1416 | /** |
||
1417 | * Internal method to add a HAVING clause with multiple values (like IN and NOT IN) |
||
1418 | */ |
||
1419 | /** |
||
1420 | * @param string|array $column_name |
||
1421 | * @param string $separator |
||
1422 | * @param mixed $values |
||
1423 | * |
||
1424 | * @return ORM |
||
1425 | */ |
||
1426 | 2 | View Code Duplication | public function _add_having_placeholder($column_name, $separator, $values) |
1443 | |||
1444 | /** |
||
1445 | * Internal method to add a HAVING clause with no parameters(like IS NULL and IS NOT NULL) |
||
1446 | * |
||
1447 | * @param string|array $column_name |
||
1448 | * @param string $operator |
||
1449 | * |
||
1450 | * @return ORM |
||
1451 | */ |
||
1452 | 2 | View Code Duplication | public function _add_having_no_value($column_name, $operator) |
1468 | |||
1469 | /** |
||
1470 | * Internal method to add a WHERE condition to the query |
||
1471 | * |
||
1472 | * @param string $fragment |
||
1473 | * @param array $values |
||
1474 | * |
||
1475 | * @return ORM |
||
1476 | */ |
||
1477 | 16 | protected function _add_where($fragment, $values = array()) |
|
1481 | |||
1482 | /** |
||
1483 | * Internal method to add a WHERE condition to the query |
||
1484 | * |
||
1485 | * @param string|array $column_name |
||
1486 | * @param string $separator |
||
1487 | * @param mixed $value |
||
1488 | * |
||
1489 | * @return $this|ORM |
||
1490 | */ |
||
1491 | 38 | protected function _add_simple_where($column_name, $separator, $value) |
|
1495 | |||
1496 | /** |
||
1497 | * Add a WHERE clause with multiple values (like IN and NOT IN) |
||
1498 | * |
||
1499 | * @param string|array $column_name |
||
1500 | * @param string $separator |
||
1501 | * @param mixed $values |
||
1502 | * |
||
1503 | * @return ORM |
||
1504 | */ |
||
1505 | 3 | View Code Duplication | public function _add_where_placeholder($column_name, $separator, $values) |
1522 | |||
1523 | /** |
||
1524 | * Add a WHERE clause with no parameters(like IS NULL and IS NOT NULL) |
||
1525 | * |
||
1526 | * @param string $column_name |
||
1527 | * @param string $operator |
||
1528 | * |
||
1529 | * @return ORM |
||
1530 | */ |
||
1531 | 2 | View Code Duplication | public function _add_where_no_value($column_name, $operator) |
1547 | |||
1548 | /** |
||
1549 | * Internal method to add a HAVING or WHERE condition to the query |
||
1550 | * |
||
1551 | * @param string $type |
||
1552 | * @param string $fragment |
||
1553 | * @param array $values |
||
1554 | * |
||
1555 | * @return $this |
||
1556 | */ |
||
1557 | 65 | protected function _add_condition($type, $fragment, $values = array()) |
|
1575 | |||
1576 | /** |
||
1577 | * Helper method to compile a simple COLUMN SEPARATOR VALUE |
||
1578 | * style HAVING or WHERE condition into a string and value ready to |
||
1579 | * be passed to the _add_condition method. Avoids duplication |
||
1580 | * of the call to _quote_identifier |
||
1581 | * |
||
1582 | * If column_name is an associative array, it will add a condition for each column |
||
1583 | * |
||
1584 | * @param string $type |
||
1585 | * @param string|array $column_name |
||
1586 | * @param string $separator |
||
1587 | * @param string|int $value |
||
1588 | * |
||
1589 | * @return $this|ORM |
||
1590 | */ |
||
1591 | 45 | protected function _add_simple_condition($type, $column_name, $separator, $value) |
|
1617 | |||
1618 | /** |
||
1619 | * Return a string containing the given number of question marks, |
||
1620 | * separated by commas. Eg "?, ?, ?" |
||
1621 | * |
||
1622 | * @param mixed $fields |
||
1623 | * |
||
1624 | * @return string |
||
1625 | */ |
||
1626 | 11 | protected function _create_placeholders($fields) |
|
1644 | |||
1645 | /** |
||
1646 | * Helper method that filters a column/value array returning only those |
||
1647 | * columns that belong to a compound primary key. |
||
1648 | * |
||
1649 | * If the key contains a column that does not exist in the given array, |
||
1650 | * a null value will be returned for it. |
||
1651 | * |
||
1652 | * @param mixed $value |
||
1653 | * |
||
1654 | * @return array |
||
1655 | */ |
||
1656 | 2 | protected function _get_compound_id_column_values($value) |
|
1665 | |||
1666 | /** |
||
1667 | * Helper method that filters an array containing compound column/value |
||
1668 | * arrays. |
||
1669 | * |
||
1670 | * @param array $values |
||
1671 | * |
||
1672 | * @return array |
||
1673 | */ |
||
1674 | 1 | protected function _get_compound_id_column_values_array($values) |
|
1683 | |||
1684 | /** |
||
1685 | * Add a WHERE column = value clause to your query. Each time |
||
1686 | * this is called in the chain, an additional WHERE will be |
||
1687 | * added, and these will be ANDed together when the final query |
||
1688 | * is built. |
||
1689 | * |
||
1690 | * If you use an array in $column_name, a new clause will be |
||
1691 | * added for each element. In this case, $value is ignored. |
||
1692 | * |
||
1693 | * @param string $column_name |
||
1694 | * @param mixed $value |
||
1695 | * |
||
1696 | * @return $this|ORM |
||
1697 | */ |
||
1698 | 31 | public function where($column_name, $value = null) |
|
1702 | |||
1703 | /** |
||
1704 | * More explicitly named version of for the where() method. |
||
1705 | * Can be used if preferred. |
||
1706 | * |
||
1707 | * @param string $column_name |
||
1708 | * @param mixed $value |
||
1709 | * |
||
1710 | * @return $this|ORM |
||
1711 | */ |
||
1712 | 33 | public function where_equal($column_name, $value = null) |
|
1716 | |||
1717 | /** |
||
1718 | * Add a WHERE column != value clause to your query. |
||
1719 | * |
||
1720 | * @param string $column_name |
||
1721 | * @param mixed $value |
||
1722 | * |
||
1723 | * @return $this|ORM |
||
1724 | */ |
||
1725 | 1 | public function where_not_equal($column_name, $value = null) |
|
1729 | |||
1730 | /** |
||
1731 | * Special method to query the table by its primary key |
||
1732 | * |
||
1733 | * If primary key is compound, only the columns that |
||
1734 | * belong to they key will be used for the query |
||
1735 | * |
||
1736 | * @param mixed $id |
||
1737 | * |
||
1738 | * @return $this|ORM |
||
1739 | */ |
||
1740 | 20 | View Code Duplication | public function where_id_is($id) |
1748 | |||
1749 | /** |
||
1750 | * Allows adding a WHERE clause that matches any of the conditions |
||
1751 | * specified in the array. Each element in the associative array will |
||
1752 | * be a different condition, where the key will be the column name. |
||
1753 | * |
||
1754 | * By default, an equal operator will be used against all columns, but |
||
1755 | * it can be overridden for any or every column using the second parameter. |
||
1756 | * |
||
1757 | * Each condition will be ORed together when added to the final query. |
||
1758 | * |
||
1759 | * @param array $values |
||
1760 | * @param string $operator |
||
1761 | * |
||
1762 | * @return $this|ORM |
||
1763 | */ |
||
1764 | 4 | public function where_any_is($values, $operator = '=') |
|
1801 | |||
1802 | /** |
||
1803 | * Similar to where_id_is() but allowing multiple primary keys. |
||
1804 | * |
||
1805 | * If primary key is compound, only the columns that |
||
1806 | * belong to they key will be used for the query |
||
1807 | * |
||
1808 | * @param mixed $ids |
||
1809 | * |
||
1810 | * @return $this|ORM |
||
1811 | */ |
||
1812 | 2 | public function where_id_in($ids) |
|
1820 | |||
1821 | /** |
||
1822 | * Add a WHERE ... LIKE clause to your query. |
||
1823 | * |
||
1824 | * @param string $column_name |
||
1825 | * @param mixed $value |
||
1826 | * |
||
1827 | * @return $this|ORM |
||
1828 | */ |
||
1829 | 1 | public function where_like($column_name, $value = null) |
|
1833 | |||
1834 | /** |
||
1835 | * Add where WHERE ... NOT LIKE clause to your query. |
||
1836 | * |
||
1837 | * @param string $column_name |
||
1838 | * @param mixed $value |
||
1839 | * |
||
1840 | * @return $this|ORM |
||
1841 | */ |
||
1842 | 1 | public function where_not_like($column_name, $value = null) |
|
1846 | |||
1847 | /** |
||
1848 | * Add a WHERE ... > clause to your query |
||
1849 | * |
||
1850 | * @param string $column_name |
||
1851 | * @param mixed $value |
||
1852 | * |
||
1853 | * @return $this|ORM |
||
1854 | */ |
||
1855 | 1 | public function where_gt($column_name, $value = null) |
|
1859 | |||
1860 | /** |
||
1861 | * Add a WHERE ... < clause to your query |
||
1862 | * |
||
1863 | * @param string $column_name |
||
1864 | * @param mixed $value |
||
1865 | * |
||
1866 | * @return $this|ORM |
||
1867 | */ |
||
1868 | 1 | public function where_lt($column_name, $value = null) |
|
1872 | |||
1873 | /** |
||
1874 | * Add a WHERE ... >= clause to your query |
||
1875 | * |
||
1876 | * @param string $column_name |
||
1877 | * @param mixed $value |
||
1878 | * |
||
1879 | * @return $this|ORM |
||
1880 | */ |
||
1881 | 1 | public function where_gte($column_name, $value = null) |
|
1885 | |||
1886 | /** |
||
1887 | * Add a WHERE ... <= clause to your query |
||
1888 | * |
||
1889 | * @param string $column_name |
||
1890 | * @param mixed $value |
||
1891 | * |
||
1892 | * @return $this|ORM |
||
1893 | */ |
||
1894 | 1 | public function where_lte($column_name, $value = null) |
|
1898 | |||
1899 | /** |
||
1900 | * Add a WHERE ... IN clause to your query |
||
1901 | * |
||
1902 | * @param string $column_name |
||
1903 | * @param mixed $values |
||
1904 | * |
||
1905 | * @return $this|ORM |
||
1906 | */ |
||
1907 | 2 | public function where_in($column_name, $values) |
|
1911 | |||
1912 | /** |
||
1913 | * Add a WHERE ... NOT IN clause to your query |
||
1914 | * |
||
1915 | * @param string $column_name |
||
1916 | * @param mixed $values |
||
1917 | * |
||
1918 | * @return $this|ORM |
||
1919 | */ |
||
1920 | 1 | public function where_not_in($column_name, $values) |
|
1924 | |||
1925 | /** |
||
1926 | * Add a WHERE column IS NULL clause to your query |
||
1927 | * |
||
1928 | * @param string $column_name |
||
1929 | * |
||
1930 | * @return $this|ORM |
||
1931 | */ |
||
1932 | 1 | public function where_null($column_name) |
|
1936 | |||
1937 | /** |
||
1938 | * Add a WHERE column IS NOT NULL clause to your query |
||
1939 | * |
||
1940 | * @param string $column_name |
||
1941 | * |
||
1942 | * @return $this|ORM |
||
1943 | */ |
||
1944 | 1 | public function where_not_null($column_name) |
|
1948 | |||
1949 | /** |
||
1950 | * Add a raw WHERE clause to the query. The clause should |
||
1951 | * contain question mark placeholders, which will be bound |
||
1952 | * to the parameters supplied in the second argument. |
||
1953 | * |
||
1954 | * @param $clause |
||
1955 | * @param array $parameters |
||
1956 | * |
||
1957 | * @return $this|ORM |
||
1958 | */ |
||
1959 | 11 | public function where_raw($clause, $parameters = array()) |
|
1963 | |||
1964 | /** |
||
1965 | * Add a LIMIT to the query |
||
1966 | * |
||
1967 | * @param int $limit |
||
1968 | * |
||
1969 | * @return $this |
||
1970 | */ |
||
1971 | 55 | public function limit($limit) |
|
1977 | |||
1978 | /** |
||
1979 | * Add an OFFSET to the query |
||
1980 | * |
||
1981 | * @param $offset |
||
1982 | * |
||
1983 | * @return $this |
||
1984 | */ |
||
1985 | 2 | public function offset($offset) |
|
1991 | |||
1992 | /** |
||
1993 | * Add an ORDER BY clause to the query |
||
1994 | * |
||
1995 | * @param string $column_name |
||
1996 | * @param string $ordering |
||
1997 | * |
||
1998 | * @return $this |
||
1999 | */ |
||
2000 | 5 | protected function _add_order_by($column_name, $ordering) |
|
2007 | |||
2008 | /** |
||
2009 | * Add an ORDER BY column DESC clause |
||
2010 | * |
||
2011 | * @param string $column_name |
||
2012 | * |
||
2013 | * @return $this|ORM |
||
2014 | */ |
||
2015 | 3 | public function order_by_desc($column_name) |
|
2019 | |||
2020 | /** |
||
2021 | * Add an ORDER BY column ASC clause |
||
2022 | * |
||
2023 | * @param string $column_name |
||
2024 | * |
||
2025 | * @return $this|ORM |
||
2026 | */ |
||
2027 | 3 | public function order_by_asc($column_name) |
|
2031 | |||
2032 | /** |
||
2033 | * Add an unquoted expression as an ORDER BY clause |
||
2034 | * |
||
2035 | * @param $clause |
||
2036 | * |
||
2037 | * @return $this |
||
2038 | */ |
||
2039 | 1 | public function order_by_expr($clause) |
|
2045 | |||
2046 | /** |
||
2047 | * Reset the ORDER BY clause |
||
2048 | */ |
||
2049 | 1 | public function reset_order_by() |
|
2055 | |||
2056 | /** |
||
2057 | * Add a column to the list of columns to GROUP BY |
||
2058 | * |
||
2059 | * @param string $column_name |
||
2060 | * |
||
2061 | * @return $this |
||
2062 | */ |
||
2063 | 14 | public function group_by($column_name) |
|
2070 | |||
2071 | /** |
||
2072 | * Add an unquoted expression to the list of columns to GROUP BY |
||
2073 | * |
||
2074 | * @param string $expr |
||
2075 | * |
||
2076 | * @return $this |
||
2077 | */ |
||
2078 | 1 | public function group_by_expr($expr) |
|
2084 | |||
2085 | /** |
||
2086 | * Add a HAVING column = value clause to your query. Each time |
||
2087 | * this is called in the chain, an additional HAVING will be |
||
2088 | * added, and these will be ANDed together when the final query |
||
2089 | * is built. |
||
2090 | * |
||
2091 | * If you use an array in $column_name, a new clause will be |
||
2092 | * added for each element. In this case, $value is ignored. |
||
2093 | * |
||
2094 | * @param string $column_name |
||
2095 | * @param null $value |
||
2096 | * |
||
2097 | * @return $this|ORM |
||
2098 | */ |
||
2099 | 2 | public function having($column_name, $value = null) |
|
2103 | |||
2104 | /** |
||
2105 | * More explicitly named version of for the having() method. |
||
2106 | * Can be used if preferred. |
||
2107 | * |
||
2108 | * @param string $column_name |
||
2109 | * @param null $value |
||
2110 | * |
||
2111 | * @return $this|ORM |
||
2112 | */ |
||
2113 | 2 | public function having_equal($column_name, $value = null) |
|
2117 | |||
2118 | /** |
||
2119 | * Add a HAVING column != value clause to your query. |
||
2120 | * |
||
2121 | * @param string $column_name |
||
2122 | * @param null $value |
||
2123 | * |
||
2124 | * @return $this|ORM |
||
2125 | */ |
||
2126 | 1 | public function having_not_equal($column_name, $value = null) |
|
2130 | |||
2131 | /** |
||
2132 | * Special method to query the table by its primary key. |
||
2133 | * |
||
2134 | * If primary key is compound, only the columns that |
||
2135 | * belong to they key will be used for the query |
||
2136 | * |
||
2137 | * @param $id |
||
2138 | * |
||
2139 | * @return $this|ORM |
||
2140 | */ |
||
2141 | View Code Duplication | public function having_id_is($id) |
|
2149 | |||
2150 | /** |
||
2151 | * Add a HAVING ... LIKE clause to your query. |
||
2152 | * |
||
2153 | * @param string $column_name |
||
2154 | * @param mixed $value |
||
2155 | * |
||
2156 | * @return $this|ORM |
||
2157 | */ |
||
2158 | 1 | public function having_like($column_name, $value = null) |
|
2162 | |||
2163 | /** |
||
2164 | * Add where HAVING ... NOT LIKE clause to your query. |
||
2165 | * |
||
2166 | * @param string $column_name |
||
2167 | * @param mixed $value |
||
2168 | * |
||
2169 | * @return $this|ORM |
||
2170 | */ |
||
2171 | 1 | public function having_not_like($column_name, $value = null) |
|
2175 | |||
2176 | /** |
||
2177 | * Add a HAVING ... > clause to your query |
||
2178 | * |
||
2179 | * @param string $column_name |
||
2180 | * @param mixed $value |
||
2181 | * |
||
2182 | * @return $this|ORM |
||
2183 | */ |
||
2184 | 1 | public function having_gt($column_name, $value = null) |
|
2188 | |||
2189 | /** |
||
2190 | * Add a HAVING ... < clause to your query |
||
2191 | * |
||
2192 | * @param string $column_name |
||
2193 | * @param mixed $value |
||
2194 | * |
||
2195 | * @return $this|ORM |
||
2196 | */ |
||
2197 | 1 | public function having_lt($column_name, $value = null) |
|
2201 | |||
2202 | /** |
||
2203 | * Add a HAVING ... >= clause to your query |
||
2204 | * |
||
2205 | * @param string $column_name |
||
2206 | * @param mixed $value |
||
2207 | * |
||
2208 | * @return $this|ORM |
||
2209 | */ |
||
2210 | 1 | public function having_gte($column_name, $value = null) |
|
2214 | |||
2215 | /** |
||
2216 | * Add a HAVING ... <= clause to your query |
||
2217 | * |
||
2218 | * @param string $column_name |
||
2219 | * @param mixed $value |
||
2220 | * |
||
2221 | * @return $this|ORM |
||
2222 | */ |
||
2223 | 1 | public function having_lte($column_name, $value = null) |
|
2227 | |||
2228 | /** |
||
2229 | * Add a HAVING ... IN clause to your query |
||
2230 | * |
||
2231 | * @param string $column_name |
||
2232 | * @param mixed $values |
||
2233 | * |
||
2234 | * @return $this|ORM |
||
2235 | */ |
||
2236 | 1 | public function having_in($column_name, $values = null) |
|
2240 | |||
2241 | /** |
||
2242 | * Add a HAVING ... NOT IN clause to your query |
||
2243 | * |
||
2244 | * @param string $column_name |
||
2245 | * @param mixed $values |
||
2246 | * |
||
2247 | * @return $this|ORM |
||
2248 | */ |
||
2249 | 1 | public function having_not_in($column_name, $values = null) |
|
2253 | |||
2254 | /** |
||
2255 | * Add a HAVING column IS NULL clause to your query |
||
2256 | * |
||
2257 | * @param string $column_name |
||
2258 | * |
||
2259 | * @return $this|ORM |
||
2260 | */ |
||
2261 | 1 | public function having_null($column_name) |
|
2265 | |||
2266 | /** |
||
2267 | * Add a HAVING column IS NOT NULL clause to your query |
||
2268 | * |
||
2269 | * @param string $column_name |
||
2270 | * |
||
2271 | * @return $this|ORM |
||
2272 | */ |
||
2273 | 1 | public function having_not_null($column_name) |
|
2277 | |||
2278 | /** |
||
2279 | * Add a raw HAVING clause to the query. The clause should |
||
2280 | * contain question mark placeholders, which will be bound |
||
2281 | * to the parameters supplied in the second argument. |
||
2282 | * |
||
2283 | * @param $clause |
||
2284 | * @param array $parameters |
||
2285 | * |
||
2286 | * @return $this|ORM |
||
2287 | */ |
||
2288 | 1 | public function having_raw($clause, $parameters = array()) |
|
2292 | |||
2293 | /** |
||
2294 | * Activate cache refreshing for current query |
||
2295 | * |
||
2296 | * @return ORM |
||
2297 | */ |
||
2298 | public function refreshCache() |
||
2304 | |||
2305 | /** |
||
2306 | * Disable caching for current query |
||
2307 | * |
||
2308 | * @return ORM |
||
2309 | */ |
||
2310 | public function noCaching() |
||
2316 | |||
2317 | /** |
||
2318 | * Build a SELECT statement based on the clauses that have |
||
2319 | * been passed to this instance by chaining method calls. |
||
2320 | */ |
||
2321 | 111 | protected function _build_select() |
|
2346 | |||
2347 | /** |
||
2348 | * Build the start of the SELECT statement |
||
2349 | */ |
||
2350 | 109 | protected function _build_select_start() |
|
2373 | |||
2374 | /** |
||
2375 | * Build the JOIN sources |
||
2376 | */ |
||
2377 | 109 | protected function _build_join() |
|
2385 | |||
2386 | /** |
||
2387 | * Build the WHERE clause(s) |
||
2388 | */ |
||
2389 | 110 | protected function _build_where() |
|
2393 | |||
2394 | /** |
||
2395 | * Build the HAVING clause(s) |
||
2396 | */ |
||
2397 | 109 | protected function _build_having() |
|
2401 | |||
2402 | /** |
||
2403 | * Build GROUP BY |
||
2404 | */ |
||
2405 | 109 | protected function _build_group_by() |
|
2413 | |||
2414 | /** |
||
2415 | * Build a WHERE or HAVING clause |
||
2416 | * |
||
2417 | * @param string $type |
||
2418 | * |
||
2419 | * @return string |
||
2420 | */ |
||
2421 | 110 | protected function _build_conditions($type) |
|
2437 | |||
2438 | /** |
||
2439 | * Build ORDER BY |
||
2440 | */ |
||
2441 | 109 | protected function _build_order_by() |
|
2449 | |||
2450 | /** |
||
2451 | * Build LIMIT |
||
2452 | */ |
||
2453 | 109 | protected function _build_limit() |
|
2469 | |||
2470 | /** |
||
2471 | * Build OFFSET |
||
2472 | */ |
||
2473 | 109 | protected function _build_offset() |
|
2486 | |||
2487 | /** |
||
2488 | * Wrapper around PHP's join function which |
||
2489 | * only adds the pieces if they are not empty. |
||
2490 | * |
||
2491 | * @param string $glue |
||
2492 | * @param array $pieces |
||
2493 | * |
||
2494 | * @return string |
||
2495 | */ |
||
2496 | 110 | protected function _join_if_not_empty($glue, $pieces) |
|
2510 | |||
2511 | /** |
||
2512 | * Quote a string that is used as an identifier |
||
2513 | * (table names, column names etc). This method can |
||
2514 | * also deal with dot-separated identifiers eg table.column |
||
2515 | * |
||
2516 | * @param string $identifier |
||
2517 | * |
||
2518 | * @return string |
||
2519 | */ |
||
2520 | 117 | protected function _quote_one_identifier($identifier) |
|
2527 | |||
2528 | /** |
||
2529 | * Quote a string that is used as an identifier |
||
2530 | * (table names, column names etc) or an array containing |
||
2531 | * multiple identifiers. This method can also deal with |
||
2532 | * dot-separated identifiers eg table.column |
||
2533 | * |
||
2534 | * @param array|string $identifier |
||
2535 | * |
||
2536 | * @return string |
||
2537 | */ |
||
2538 | 117 | protected function _quote_identifier($identifier) |
|
2548 | |||
2549 | /** |
||
2550 | * This method performs the actual quoting of a single |
||
2551 | * part of an identifier, using the identifier quote |
||
2552 | * character specified in the config (or autodetected). |
||
2553 | * |
||
2554 | * @param string $part |
||
2555 | * |
||
2556 | * @return string |
||
2557 | */ |
||
2558 | 117 | protected function _quote_identifier_part($part) |
|
2572 | |||
2573 | /** |
||
2574 | * Create a cache key for the given query and parameters. |
||
2575 | * |
||
2576 | * @param string $query |
||
2577 | * @param array $parameters |
||
2578 | * @param null|string $table_name |
||
2579 | * @param string $connection_name |
||
2580 | * |
||
2581 | * @return mixed|string |
||
2582 | */ |
||
2583 | 2 | protected static function _create_cache_key($query, $parameters, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2601 | |||
2602 | /** |
||
2603 | * Check the query cache for the given cache key. If a value |
||
2604 | * is cached for the key, return the value. Otherwise, return false. |
||
2605 | * |
||
2606 | * @param string $cache_key |
||
2607 | * @param null|string $table_name |
||
2608 | * @param string $connection_name |
||
2609 | * |
||
2610 | * @return bool|mixed |
||
2611 | */ |
||
2612 | 2 | protected static function _check_query_cache($cache_key, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2633 | |||
2634 | /** |
||
2635 | * Clear the query cache |
||
2636 | * |
||
2637 | * @param null|string $table_name |
||
2638 | * @param string $connection_name |
||
2639 | * |
||
2640 | * @return bool|mixed |
||
2641 | */ |
||
2642 | public static function clear_cache($table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
||
2663 | |||
2664 | /** |
||
2665 | * Add the given value to the query cache. |
||
2666 | * |
||
2667 | * @param string $cache_key |
||
2668 | * @param string $value |
||
2669 | * @param null|string $table_name |
||
2670 | * @param string $connection_name |
||
2671 | * |
||
2672 | * @return bool|mixed |
||
2673 | */ |
||
2674 | 2 | protected static function _cache_query_result($cache_key, $value, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) |
|
2700 | |||
2701 | /** |
||
2702 | * Execute the SELECT query that has been built up by chaining methods |
||
2703 | * on this class. Return an array of rows as associative arrays. |
||
2704 | */ |
||
2705 | 111 | protected function _run() |
|
2744 | |||
2745 | /** |
||
2746 | * Return the raw data wrapped by this ORM |
||
2747 | * instance as an associative array. Column |
||
2748 | * names may optionally be supplied as arguments, |
||
2749 | * if so, only those keys will be returned. |
||
2750 | */ |
||
2751 | public function as_array() |
||
2760 | |||
2761 | /** |
||
2762 | * Return the value of a property of this object (database row) |
||
2763 | * or null if not present. |
||
2764 | * |
||
2765 | * If a column-names array is passed, it will return a associative array |
||
2766 | * with the value of each column or null if it is not present. |
||
2767 | * |
||
2768 | * @param mixed $key |
||
2769 | * |
||
2770 | * @return mixed |
||
2771 | */ |
||
2772 | 21 | public function get($key) |
|
2785 | |||
2786 | /** |
||
2787 | * Return the name of the column in the database table which contains |
||
2788 | * the primary key ID of the row. |
||
2789 | */ |
||
2790 | 33 | protected function _get_id_column_name() |
|
2801 | |||
2802 | /** |
||
2803 | * Get the primary key ID of this object. |
||
2804 | * |
||
2805 | * @param bool $disallow_null |
||
2806 | * |
||
2807 | * @return mixed |
||
2808 | * |
||
2809 | * @throws \Exception |
||
2810 | */ |
||
2811 | 19 | public function id($disallow_null = false) |
|
2829 | |||
2830 | /** |
||
2831 | * Set a property to a particular value on this object. |
||
2832 | * To set multiple properties at once, pass an associative array |
||
2833 | * as the first parameter and leave out the second parameter. |
||
2834 | * Flags the properties as 'dirty' so they will be saved to the |
||
2835 | * database when save() is called. |
||
2836 | * |
||
2837 | * @param mixed $key |
||
2838 | * @param mixed $value |
||
2839 | * |
||
2840 | * @return $this|ORM |
||
2841 | */ |
||
2842 | 17 | public function set($key, $value = null) |
|
2846 | |||
2847 | /** |
||
2848 | * Set a property to a particular value on this object. |
||
2849 | * To set multiple properties at once, pass an associative array |
||
2850 | * as the first parameter and leave out the second parameter. |
||
2851 | * Flags the properties as 'dirty' so they will be saved to the |
||
2852 | * database when save() is called. |
||
2853 | * |
||
2854 | * @param string|array $key |
||
2855 | * @param string|null $value |
||
2856 | * |
||
2857 | * @return ORM |
||
2858 | */ |
||
2859 | 5 | public function set_expr($key, $value = null) |
|
2863 | |||
2864 | /** |
||
2865 | * Set a property on the ORM object. |
||
2866 | * |
||
2867 | * @param string|array $key |
||
2868 | * @param string|null $value |
||
2869 | * @param bool $expr |
||
2870 | * |
||
2871 | * @return $this |
||
2872 | */ |
||
2873 | 18 | protected function _set_orm_property($key, $value = null, $expr = false) |
|
2890 | |||
2891 | /** |
||
2892 | * Check whether the given field has been changed since this |
||
2893 | * object was saved. |
||
2894 | * |
||
2895 | * @param string $key |
||
2896 | * |
||
2897 | * @return bool |
||
2898 | */ |
||
2899 | 1 | public function is_dirty($key) |
|
2903 | |||
2904 | /** |
||
2905 | * Check whether the model was the result of a call to create() or not |
||
2906 | * |
||
2907 | * @return bool |
||
2908 | */ |
||
2909 | 2 | public function is_new() |
|
2913 | |||
2914 | /** |
||
2915 | * Save any fields which have been modified on this object |
||
2916 | * to the database. |
||
2917 | * |
||
2918 | * @return bool |
||
2919 | * |
||
2920 | * @throws \Exception |
||
2921 | */ |
||
2922 | 15 | public function save() |
|
2990 | |||
2991 | /** |
||
2992 | * Add a WHERE clause for every column that belongs to the primary key |
||
2993 | * |
||
2994 | * @param array $query warning: this is a reference |
||
2995 | */ |
||
2996 | 13 | public function _add_id_column_conditions(&$query) |
|
3019 | |||
3020 | /** |
||
3021 | * Build an UPDATE query |
||
3022 | */ |
||
3023 | 10 | protected function _build_update() |
|
3043 | |||
3044 | /** |
||
3045 | * Build an INSERT query |
||
3046 | */ |
||
3047 | 6 | protected function _build_insert() |
|
3064 | |||
3065 | /** |
||
3066 | * Delete this record from the database |
||
3067 | */ |
||
3068 | 3 | public function delete() |
|
3082 | |||
3083 | /** |
||
3084 | * Delete many records from the database |
||
3085 | */ |
||
3086 | 1 | public function delete_many() |
|
3101 | |||
3102 | // --------------------- // |
||
3103 | // --- ArrayAccess --- // |
||
3104 | // --------------------- // |
||
3105 | |||
3106 | 8 | public function offsetExists($key) |
|
3110 | |||
3111 | 2 | public function offsetGet($key) |
|
3115 | |||
3116 | 9 | public function offsetSet($key, $value) |
|
3123 | |||
3124 | 1 | public function offsetUnset($key) |
|
3129 | |||
3130 | // --------------------- // |
||
3131 | // --- MAGIC METHODS --- // |
||
3132 | // --------------------- // |
||
3133 | 1 | public function __get($key) |
|
3137 | |||
3138 | 7 | public function __set($key, $value) |
|
3142 | |||
3143 | public function __unset($key) |
||
3147 | |||
3148 | 7 | public function __isset($key) |
|
3152 | |||
3153 | /** |
||
3154 | * Magic method to capture calls to undefined class methods. |
||
3155 | * In this case we are attempting to convert camel case formatted |
||
3156 | * methods into underscore formatted methods. |
||
3157 | * |
||
3158 | * This allows us to call ORM methods using camel case and remain |
||
3159 | * backwards compatible. |
||
3160 | * |
||
3161 | * @param string $name |
||
3162 | * @param array $arguments |
||
3163 | * |
||
3164 | * @return ORM |
||
3165 | * |
||
3166 | * @throws IdiormMethodMissingException |
||
3167 | */ |
||
3168 | 2 | public function __call($name, $arguments) |
|
3178 | |||
3179 | /** |
||
3180 | * Magic method to capture calls to undefined static class methods. |
||
3181 | * In this case we are attempting to convert camel case formatted |
||
3182 | * methods into underscore formatted methods. |
||
3183 | * |
||
3184 | * This allows us to call ORM methods using camel case and remain |
||
3185 | * backwards compatible. |
||
3186 | * |
||
3187 | * @param string $name |
||
3188 | * @param array $arguments |
||
3189 | * |
||
3190 | * @return ORM |
||
3191 | */ |
||
3192 | public static function __callStatic($name, $arguments) |
||
3198 | |||
3199 | } |
||
3200 |
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: