query.php 19.6 KB
Newer Older
1 2 3 4 5 6
<?php namespace Laravel\Database;

use Closure;
use Laravel\Database;
use Laravel\Paginator;
use Laravel\Database\Query\Grammars\SQLServer;
7 8 9 10

class Query {

	/**
Taylor Otwell committed
11
	 * The database connection.
12
	 *
13
	 * @var Connection
14
	 */
Taylor Otwell committed
15
	public $connection;
16 17

	/**
18
	 * The query grammar instance.
19
	 *
20
	 * @var Query\Grammars\Grammar
21
	 */
22
	public $grammar;
23 24

	/**
25 26
	 * The SELECT clause.
	 *
Taylor Otwell committed
27
	 * @var array
28
	 */
Taylor Otwell committed
29
	public $selects;
30 31

	/**
32
	 * The aggregating column and function.
33
	 *
Taylor Otwell committed
34
	 * @var array
35
	 */
Taylor Otwell committed
36
	public $aggregate;
37 38

	/**
Taylor Otwell committed
39
	 * Indicates if the query should return distinct results.
40
	 *
Taylor Otwell committed
41
	 * @var bool
42
	 */
Taylor Otwell committed
43
	public $distinct = false;
44 45 46 47 48 49

	/**
	 * The table name.
	 *
	 * @var string
	 */
Taylor Otwell committed
50
	public $from;
51 52

	/**
Taylor Otwell committed
53
	 * The table joins.
54
	 *
Taylor Otwell committed
55
	 * @var array
56
	 */
Taylor Otwell committed
57
	public $joins;
58 59

	/**
Taylor Otwell committed
60
	 * The WHERE clauses.
61
	 *
Taylor Otwell committed
62
	 * @var array
63
	 */
Taylor Otwell committed
64
	public $wheres;
65 66

	/**
67 68 69 70 71 72 73
	 * The GROUP BY clauses.
	 *
	 * @var array
	 */
	public $groupings;

	/**
Taylor Otwell committed
74
	 * The ORDER BY clauses.
75 76 77
	 *
	 * @var array
	 */
Taylor Otwell committed
78
	public $orderings;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

	/**
	 * The LIMIT value.
	 *
	 * @var int
	 */
	public $limit;

	/**
	 * The OFFSET value.
	 *
	 * @var int
	 */
	public $offset;

	/**
	 * The query value bindings.
	 *
	 * @var array
	 */
	public $bindings = array();

	/**
	 * Create a new query instance.
	 *
104 105 106
	 * @param  Connection  $connection
	 * @param  Grammar     $grammar
	 * @param  string      $table
107 108
	 * @return void
	 */
109
	public function __construct(Connection $connection, Query\Grammars\Grammar $grammar, $table)
110
	{
Taylor Otwell committed
111
		$this->from = $table;
112
		$this->grammar = $grammar;
113
		$this->connection = $connection;
114 115 116 117 118 119 120 121 122 123 124 125 126 127
	}

	/**
	 * Force the query to return distinct results.
	 *
	 * @return Query
	 */
	public function distinct()
	{
		$this->distinct = true;
		return $this;
	}

	/**
Taylor Otwell committed
128 129
	 * Add an array of columns to the SELECT clause.
	 *
130
	 * @param  array  $columns
131 132
	 * @return Query
	 */
133
	public function select($columns = array('*'))
134
	{
Taylor Otwell committed
135
		$this->selects = (array) $columns;
136 137 138 139
		return $this;
	}

	/**
Taylor Otwell committed
140
	 * Add a join clause to the query.
141
	 *
142 143 144 145 146 147 148
	 * @param  string  $table
	 * @param  string  $column1
	 * @param  string  $operator
	 * @param  string  $column2
	 * @param  string  $type
	 * @return Query
	 */
149
	public function join($table, $column1, $operator = null, $column2 = null, $type = 'INNER')
150
	{
151 152
		// If the "column" is really an instance of a Closure, the developer is
		// trying to create a join with a complex "ON" clause. So, we will add
Taylor Otwell committed
153
		// the join, and then call the Closure with the join/
154 155 156 157 158 159
		if ($column1 instanceof Closure)
		{
			$this->joins[] = new Query\Join($type, $table);

			call_user_func($column1, end($this->joins));
		}
160

161 162 163 164 165 166 167 168 169 170 171
		// If the column is just a string, we can assume that the join just
		// has a simple on clause, and we'll create the join instance and
		// add the clause automatically for the develoepr.
		else
		{
			$join = new Query\Join($type, $table);

			$join->on($column1, $operator, $column2);

			$this->joins[] = $join;
		}
Taylor Otwell committed
172

173 174 175 176 177 178 179 180 181 182 183 184
		return $this;
	}

	/**
	 * Add a left join to the query.
	 *
	 * @param  string  $table
	 * @param  string  $column1
	 * @param  string  $operator
	 * @param  string  $column2
	 * @return Query
	 */
185
	public function left_join($table, $column1, $operator = null, $column2 = null)
186 187 188 189 190
	{
		return $this->join($table, $column1, $operator, $column2, 'LEFT');
	}

	/**
191
	 * Reset the where clause to its initial state.
Taylor Otwell committed
192 193 194 195 196
	 *
	 * @return void
	 */
	public function reset_where()
	{
197
		list($this->wheres, $this->bindings) = array(array(), array());
Taylor Otwell committed
198 199 200
	}

	/**
201 202 203 204 205 206 207 208 209
	 * Add a raw where condition to the query.
	 *
	 * @param  string  $where
	 * @param  array   $bindings
	 * @param  string  $connector
	 * @return Query
	 */
	public function raw_where($where, $bindings = array(), $connector = 'AND')
	{
210
		$this->wheres[] = array('type' => 'where_raw', 'connector' => $connector, 'sql' => $where);
Taylor Otwell committed
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		$this->bindings = array_merge($this->bindings, $bindings);

		return $this;
	}

	/**
	 * Add a raw or where condition to the query.
	 *
	 * @param  string  $where
	 * @param  array   $bindings
	 * @return Query
	 */
	public function raw_or_where($where, $bindings = array())
	{
		return $this->raw_where($where, $bindings, 'OR');
	}

	/**
	 * Add a where condition to the query.
	 *
	 * @param  string  $column
	 * @param  string  $operator
	 * @param  mixed   $value
	 * @param  string  $connector
	 * @return Query
	 */
238
	public function where($column, $operator = null, $value = null, $connector = 'AND')
239
	{
240
		// If a Closure is passed into the method, it means a nested where
241 242 243 244 245 246 247
		// clause is being initiated, so we will take a different course
		// of action than when the statement is just a simple where.
		if ($column instanceof Closure)
		{
			return $this->where_nested($column, $connector);
		}

248 249 250
		$type = 'where';

		$this->wheres[] = compact('type', 'column', 'operator', 'value', 'connector');
Taylor Otwell committed
251

252 253 254 255 256 257 258 259 260 261 262 263 264
		$this->bindings[] = $value;

		return $this;
	}

	/**
	 * Add an or where condition to the query.
	 *
	 * @param  string  $column
	 * @param  string  $operator
	 * @param  mixed   $value
	 * @return Query
	 */
265
	public function or_where($column, $operator = null, $value = null)
266 267 268 269 270
	{
		return $this->where($column, $operator, $value, 'OR');
	}

	/**
271
	 * Add an or where condition for the primary key to the query.
Taylor Otwell committed
272
	 *
273 274 275 276 277 278 279 280 281
	 * @param  mixed  $value
	 * @return Query
	 */
	public function or_where_id($value)
	{
		return $this->or_where('id', '=', $value);		
	}

	/**
282 283 284 285 286
	 * Add a where in condition to the query.
	 *
	 * @param  string  $column
	 * @param  array   $values
	 * @param  string  $connector
Taylor Otwell committed
287
	 * @param  bool    $not
288 289
	 * @return Query
	 */
Taylor Otwell committed
290
	public function where_in($column, $values, $connector = 'AND', $not = false)
291
	{
292 293 294
		$type = ($not) ? 'where_not_in' : 'where_in';

		$this->wheres[] = compact('type', 'column', 'values', 'connector');
Taylor Otwell committed
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
		$this->bindings = array_merge($this->bindings, $values);

		return $this;
	}

	/**
	 * Add an or where in condition to the query.
	 *
	 * @param  string  $column
	 * @param  array   $values
	 * @return Query
	 */
	public function or_where_in($column, $values)
	{
		return $this->where_in($column, $values, 'OR');
	}

	/**
	 * Add a where not in condition to the query.
	 *
	 * @param  string  $column
	 * @param  array   $values
	 * @param  string  $connector
	 * @return Query
	 */
	public function where_not_in($column, $values, $connector = 'AND')
	{
Taylor Otwell committed
323
		return $this->where_in($column, $values, $connector, true);
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	}

	/**
	 * Add an or where not in condition to the query.
	 *
	 * @param  string  $column
	 * @param  array   $values
	 * @return Query
	 */
	public function or_where_not_in($column, $values)
	{
		return $this->where_not_in($column, $values, 'OR');
	}

	/**
	 * Add a where null condition to the query.
	 *
	 * @param  string  $column
	 * @param  string  $connector
Taylor Otwell committed
343
	 * @param  bool    $not
344 345
	 * @return Query
	 */
Taylor Otwell committed
346
	public function where_null($column, $connector = 'AND', $not = false)
347
	{
348 349 350
		$type = ($not) ? 'where_not_null' : 'where_null';

		$this->wheres[] = compact('type', 'column', 'connector');
Taylor Otwell committed
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
		return $this;
	}

	/**
	 * Add an or where null condition to the query.
	 *
	 * @param  string  $column
	 * @return Query
	 */
	public function or_where_null($column)
	{
		return $this->where_null($column, 'OR');
	}

	/**
	 * Add a where not null condition to the query.
	 *
	 * @param  string  $column
	 * @param  string  $connector
	 * @return Query
	 */
	public function where_not_null($column, $connector = 'AND')
	{
Taylor Otwell committed
375
		return $this->where_null($column, $connector, true);
376 377 378 379 380 381 382 383 384 385 386 387 388 389
	}

	/**
	 * Add an or where not null condition to the query.
	 *
	 * @param  string  $column
	 * @return Query
	 */
	public function or_where_not_null($column)
	{
		return $this->where_not_null($column, 'OR');
	}

	/**
390
	 * Add a nested where condition to the query.
Taylor Otwell committed
391
	 *
392 393 394 395
	 * @param  Closure  $callback
	 * @param  string   $connector
	 * @return Query
	 */
396
	public function where_nested($callback, $connector = 'AND')
397 398 399
	{
		$type = 'where_nested';

400 401 402
		// To handle a nested where statement, we will actually instantiate a new
		// Query instance and run the callback over that instance, which will
		// allow the developer to have a fresh query instance
403 404 405 406
		$query = new Query($this->connection, $this->grammar, $this->from);

		call_user_func($callback, $query);

407 408 409
		// Once the callback has been run on the query, we will store the nested
		// query instance on the where clause array so that it's passed to the
		// query's query grammar instance when building.
410 411 412 413 414 415 416 417 418
		$this->wheres[] = compact('type', 'query', 'connector');

		$this->bindings = array_merge($this->bindings, $query->bindings);

		return $this;
	}

	/**
	 * Add dynamic where conditions to the query.
Taylor Otwell committed
419
	 *
Taylor Otwell committed
420 421 422 423 424 425 426 427
	 * @param  string  $method
	 * @param  array   $parameters
	 * @return Query
	 */
	private function dynamic_where($method, $parameters)
	{
		$finder = substr($method, 6);

428 429 430
		$flags = PREG_SPLIT_DELIM_CAPTURE;

		$segments = preg_split('/(_and_|_or_)/i', $finder, -1, $flags);
Taylor Otwell committed
431

432 433
		// The connector variable will determine which connector will be used
		// for the condition. We'll change it as we come across new boolean
434
		// connectors in the dynamic method string.
Taylor Otwell committed
435
		//
436 437 438
		// The index variable helps us get the correct parameter value for
		// the where condition. We increment it each time we add another
		// condition to the query's where clause.
Taylor Otwell committed
439 440 441 442 443 444
		$connector = 'AND';

		$index = 0;

		foreach ($segments as $segment)
		{
445 446 447
			// If the segment is not a boolean connector, we can assume it it is
			// a column name, and we'll add it to the query as a new constraint
			// of the query's where clause and keep iterating the segments.
Taylor Otwell committed
448 449 450 451 452 453
			if ($segment != '_and_' and $segment != '_or_')
			{
				$this->where($segment, '=', $parameters[$index], $connector);

				$index++;
			}
454 455 456
			// Otherwise, we will store the connector so we know how the next
			// where clause we find in the query should be connected to the
			// previous one and will add it when we find the next one.
Taylor Otwell committed
457 458 459 460 461 462 463 464 465 466
			else
			{
				$connector = trim(strtoupper($segment), '_');
			}
		}

		return $this;
	}

	/**
467 468 469 470 471 472 473 474 475 476 477 478
	 * Add a grouping to the query.
	 *
	 * @param  string  $column
	 * @return Query
	 */
	public function group_by($column)
	{
		$this->groupings[] = $column;
		return $this;
	}

	/**
479 480 481 482 483 484
	 * Add an ordering to the query.
	 *
	 * @param  string  $column
	 * @param  string  $direction
	 * @return Query
	 */
485
	public function order_by($column, $direction = 'asc')
486
	{
Taylor Otwell committed
487
		$this->orderings[] = compact('column', 'direction');
488 489 490 491 492 493 494 495 496 497 498
		return $this;
	}

	/**
	 * Set the query offset.
	 *
	 * @param  int  $value
	 * @return Query
	 */
	public function skip($value)
	{
Taylor Otwell committed
499
		$this->offset = $value;
500 501 502 503 504 505 506 507 508 509 510
		return $this;
	}

	/**
	 * Set the query limit.
	 *
	 * @param  int  $value
	 * @return Query
	 */
	public function take($value)
	{
Taylor Otwell committed
511
		$this->limit = $value;
512 513 514 515
		return $this;
	}

	/**
516
	 * Set the query limit and offset for a given page.
517 518 519 520 521
	 *
	 * @param  int    $page
	 * @param  int    $per_page
	 * @return Query
	 */
Taylor Otwell committed
522
	public function for_page($page, $per_page)
523
	{
Taylor Otwell committed
524
		return $this->skip(($page - 1) * $per_page)->take($per_page);
525 526 527
	}

	/**
Taylor Otwell committed
528
	 * Find a record by the primary key.
529
	 *
Taylor Otwell committed
530
	 * @param  int     $id
531
	 * @param  array   $columns
532 533
	 * @return object
	 */
Taylor Otwell committed
534
	public function find($id, $columns = array('*'))
535
	{
Taylor Otwell committed
536
		return $this->where('id', '=', $id)->first($columns);
537 538 539
	}

	/**
540
	 * Execute the query as a SELECT statement and return a single column.
541 542 543 544
	 *
	 * @param  string  $column
	 * @return mixed
	 */
545
	public function only($column)
546
	{
547
		$sql = $this->grammar->select($this->select(array($column)));
Taylor Otwell committed
548

549
		return $this->connection->only($sql, $this->bindings);
550 551 552
	}

	/**
Taylor Otwell committed
553
	 * Execute the query as a SELECT statement and return the first result.
554
	 *
555 556
	 * @param  array  $columns
	 * @return mixed
557
	 */
Taylor Otwell committed
558
	public function first($columns = array('*'))
559
	{
560 561
		$columns = (array) $columns;

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
		// Since we only need the first result, we'll go ahead and set the
		// limit clause to 1, since this will be much faster than getting
		// all of the rows and then only returning the first.
		$results = $this->take(1)->get($columns);

		return (count($results) > 0) ? $results[0] : null;
	}

	/**
	 * Get an array with the values of a given column.
	 *
	 * @param  string  $column
	 * @param  string  $key
	 * @return array
	 */
	public function lists($column, $key = null)
	{
		$columns = (is_null($key)) ? array($column) : array($column, $key);

		$results = $this->get($columns);

		// First we will get the array of values for the requested column.
		// Of course, this array will simply have numeric keys. After we
		// have this array we will determine if we need to key the array
		// by another column from the result set.
		$values = array_map(function($row) use ($column)
		{
			return $row->$column;

		}, $results);

		// If a key was provided, we will extract an array of keys and
		// set the keys on the array of values using the array_combine
		// function provided by PHP, which should give us the proper
		// array form to return from the method.
		if ( ! is_null($key))
		{
			return array_combine(array_map(function($row) use ($key)
			{
				return $row->$key;

			}, $results), $values);
		}

		return $values;
Taylor Otwell committed
607 608 609 610 611 612 613 614 615 616
	}

	/**
	 * Execute the query as a SELECT statement.
	 *
	 * @param  array  $columns
	 * @return array
	 */
	public function get($columns = array('*'))
	{
Taylor Otwell committed
617
		if (is_null($this->selects)) $this->select($columns);
Taylor Otwell committed
618

619 620 621 622 623 624
		$sql = $this->grammar->select($this);

		$results = $this->connection->query($sql, $this->bindings);

		// If the query has an offset and we are using the SQL Server grammar,
		// we need to spin through the results and remove the "rownum" from
Taylor Otwell committed
625
		// each of the objects since there is no "offset".
626 627 628 629 630 631 632
		if ($this->offset > 0 and $this->grammar instanceof SQLServer)
		{
			array_walk($results, function($result)
			{
				unset($result->rownum);
			});
		}
633

634 635
		// Reset the SELECT clause so more queries can be performed using
		// the same instance. This is helpful for getting aggregates and
636
		// then getting actual results from the query.
Taylor Otwell committed
637
		$this->selects = null;
638 639 640 641 642

		return $results;
	}

	/**
643 644
	 * Get an aggregate value.
	 *
Phill Sparks committed
645
	 * @param  string  $aggregator
646
	 * @param  array   $columns
647 648
	 * @return mixed
	 */
649
	public function aggregate($aggregator, $columns)
650
	{
651 652 653
		// We'll set the aggregate value so the grammar does not try to compile
		// a SELECT clause on the query. If an aggregator is present, it's own
		// grammar function will be used to build the SQL syntax.
654
		$this->aggregate = compact('aggregator', 'columns');
655

656 657 658
		$sql = $this->grammar->select($this);

		$result = $this->connection->only($sql, $this->bindings);
659

660 661 662
		// Reset the aggregate so more queries can be performed using the same
		// instance. This is helpful for getting aggregates and then getting
		// actual results from the query such as during paging.
663 664 665 666 667 668
		$this->aggregate = null;

		return $result;
	}

	/**
669 670 671 672 673 674
	 * Get the paginated query results as a Paginator instance.
	 *
	 * @param  int        $per_page
	 * @param  array      $columns
	 * @return Paginator
	 */
675
	public function paginate($per_page = 20, $columns = array('*'))
676
	{
677
		// Because some database engines may throw errors if we leave orderings
Taylor Otwell committed
678
		// on the query when retrieving the total number of records, we'll drop
679
		// all of the ordreings and put them back on the query.
680 681
		list($orderings, $this->orderings) = array($this->orderings, null);

Taylor Otwell committed
682 683 684
		$total = $this->count(reset($columns));

		$page = Paginator::page($total, $per_page);
685

686 687
		$this->orderings = $orderings;

Taylor Otwell committed
688 689
		// Now we're ready to get the actual pagination results from the table
		// using the for_page and get methods. The "for_page" method provides
690
		// a convenient way to set the paging limit and offset.
691 692 693
		$results = $this->for_page($page, $per_page)->get($columns);

		return Paginator::make($results, $total, $per_page);
694 695 696
	}

	/**
Taylor Otwell committed
697 698
	 * Insert an array of values into the database table.
	 *
699 700 701 702 703
	 * @param  array  $values
	 * @return bool
	 */
	public function insert($values)
	{
704 705 706
		// Force every insert to be treated like a batch insert to make creating
		// the binding array simpler since we can just spin through the inserted
		// rows as if there/ was more than one every time.
707 708 709 710
		if ( ! is_array(reset($values))) $values = array($values);

		$bindings = array();

711 712 713
		// We need to merge the the insert values into the array of the query
		// bindings so that they will be bound to the PDO statement when it
		// is executed by the database connection.
714 715 716 717 718
		foreach ($values as $value)
		{
			$bindings = array_merge($bindings, array_values($value));
		}

719 720
		$sql = $this->grammar->insert($this, $values);

721
		return $this->connection->query($sql, $bindings);
722 723 724
	}

	/**
725
	 * Insert an array of values into the database table and return the ID.
Taylor Otwell committed
726
	 *
727 728
	 * @param  array   $values
	 * @param  string  $sequence
729 730
	 * @return int
	 */
731
	public function insert_get_id($values, $sequence = null)
732
	{
733 734
		$sql = $this->grammar->insert($this, $values);

735
		$this->connection->query($sql, array_values($values));
736

737 738 739
		// Some database systems (Postgres) require a sequence name to be
		// given when retrieving the auto-incrementing ID, so we'll pass
		// the given sequence into the method just in case.
740
		return (int) $this->connection->pdo->lastInsertId($sequence);
Taylor Otwell committed
741 742 743
	}

	/**
744 745 746 747 748 749 750 751
	 * Increment the value of a column by a given amount.
	 *
	 * @param  string  $column
	 * @param  int     $amount
	 * @return int
	 */
	public function increment($column, $amount = 1)
	{
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
		return $this->adjust($column, $amount, ' + ');
	}

	/**
	 * Decrement the value of a column by a given amount.
	 *
	 * @param  string  $column
	 * @param  int     $amount
	 * @return int
	 */
	public function decrement($column, $amount = 1)
	{
		return $this->adjust($column, $amount, ' - ');
	}

	/**
	 * Adjust the value of a column up or down by a given amount.
	 *
	 * @param  string  $column
	 * @param  int     $amount
	 * @param  string  $operator
	 * @return int
	 */
	protected function adjust($column, $amount, $operator)
	{
777 778
		$wrapped = $this->grammar->wrap($column);

779 780 781
		// To make the adjustment to the column, we'll wrap the expression in an
		// Expression instance, which forces the adjustment to be injected into
		// the query as a string instead of bound.
782
		$value = Database::raw($wrapped.$operator.$amount);
783 784

		return $this->update(array($column => $value));
785 786 787
	}

	/**
Taylor Otwell committed
788 789
	 * Update an array of values in the database table.
	 *
790
	 * @param  array  $values
Taylor Otwell committed
791
	 * @return int
792 793 794
	 */
	public function update($values)
	{
795 796 797
		// For update statements, we need to merge the bindings such that the update
		// values occur before the where bindings in the array since the sets will
		// precede any of the where clauses in the SQL syntax that is generated.
798 799
		$bindings =  array_merge(array_values($values), $this->bindings);

800 801
		$sql = $this->grammar->update($this, $values);

802
		return $this->connection->query($sql, $bindings);
803 804 805 806 807
	}

	/**
	 * Execute the query as a DELETE statement.
	 *
Taylor Otwell committed
808 809
	 * Optionally, an ID may be passed to the method do delete a specific row.
	 *
810
	 * @param  int   $id
Taylor Otwell committed
811
	 * @return int
812 813 814
	 */
	public function delete($id = null)
	{
815 816 817
		// If an ID is given to the method, we'll set the where clause to
		// match on the value of the ID. This allows the developer to
		// quickly delete a row by its primary key value.
818 819 820 821 822 823
		if ( ! is_null($id))
		{
			$this->where('id', '=', $id);
		}

		$sql = $this->grammar->delete($this);
824

825
		return $this->connection->query($sql, $this->bindings);		
826 827 828
	}

	/**
Taylor Otwell committed
829
	 * Magic Method for handling dynamic functions.
Taylor Otwell committed
830
	 *
831
	 * This method handles calls to aggregates as well as dynamic where clauses.
832 833 834
	 */
	public function __call($method, $parameters)
	{
835 836
		if (strpos($method, 'where_') === 0)
		{
Taylor Otwell committed
837
			return $this->dynamic_where($method, $parameters, $this);
838 839
		}

840 841 842
		// All of the aggregate methods are handled by a single method, so we'll
		// catch them all here and then pass them off to the agregate method
		// instead of creating methods for each one of them.
843
		if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))
844
		{
845 846 847
			if (count($parameters) == 0) $parameters[0] = '*';

			return $this->aggregate(strtoupper($method), (array) $parameters[0]);
848
		}
849

850
		throw new \Exception("Method [$method] is not defined on the Query class.");
851 852
	}

Phill Sparks committed
853
}