Skip to content

Commit

Permalink
Added support for COUNT queries
Browse files Browse the repository at this point in the history
  • Loading branch information
j4mie committed Apr 12, 2010
1 parent 634861d commit 42c375b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.markdown
Expand Up @@ -87,6 +87,12 @@ To find all records where the `gender` is `female`:

$females = ORM::for_table('person')->where('gender', 'female')->find_many();

#### Counting results ####

To return a count of the number of rows that would be returned by a query, call the `count()` method.

$number_of_people = ORM::for_table('person')->count();

#### WHERE clauses ####

The `where` method on the ORM class adds a single `WHERE` clause to your query. The method may be called (chained) multiple times to add more than one `WHERE` clause. All the `WHERE` clauses will be `AND`ed together when the query is run. Support for `OR`ing `WHERE` clauses is not currently present; if a query requires an `OR` operator you should use the `where_raw` or `raw_select` methods (see below).
Expand Down
3 changes: 2 additions & 1 deletion demo.php
Expand Up @@ -48,6 +48,7 @@
}

// Get a list of all contacts from the database
$count = ORM::for_table('contact')->count();
$contact_list = ORM::for_table('contact')->find_many();
?>

Expand All @@ -60,7 +61,7 @@

<h1>Idiorm Demo</h1>

<h2>Contact List</h2>
<h2>Contact List (<?php echo $count; ?> contacts)</h2>
<ul>
<?php foreach ($contact_list as $contact): ?>
<li>
Expand Down
40 changes: 30 additions & 10 deletions idiorm.php
Expand Up @@ -51,6 +51,7 @@ class ORM {
// Find types
const FIND_ONE = 0;
const FIND_MANY = 1;
const COUNT = 2;

// Update or insert?
const UPDATE = 0;
Expand Down Expand Up @@ -266,6 +267,16 @@ public function find_many() {
return $this->run();
}

/**
* Tell the ORM that you wish to execute a COUNT query.
* Will return an integer representing the number of
* rows returned.
*/
public function count() {
$this->find_type = self::COUNT;
return $this->run();
}

/**
* This method can be called hydrate (populate) this
* instance of the class from an associative array of data.
Expand Down Expand Up @@ -395,7 +406,12 @@ private function build_select() {
}

$query = array();
$query[] = 'SELECT * FROM ' . $this->table_name;

if ($this->find_type === self::COUNT) {
$query[] = 'SELECT COUNT(*) AS count FROM ' . $this->table_name;
} else {
$query[] = 'SELECT * FROM ' . $this->table_name;
}

if ($this->where_is_raw) { // Raw WHERE clause
$query[] = "WHERE";
Expand Down Expand Up @@ -458,15 +474,19 @@ private function run() {
$statement = self::$db->prepare($this->build_select());
$statement->execute($this->values);

if ($this->find_type == self::FIND_ONE) {
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result ? self::for_table($this->table_name)->hydrate($result) : $result;
} else {
$instances = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$instances[] = self::for_table($this->table_name)->hydrate($row);
}
return $instances;
switch ($this->find_type) {
case self::FIND_ONE:
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result ? self::for_table($this->table_name)->hydrate($result) : $result;
case self::FIND_MANY:
$instances = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$instances[] = self::for_table($this->table_name)->hydrate($row);
}
return $instances;
case self::COUNT:
$result = $statement->fetch(PDO::FETCH_ASSOC);
return isset($result['count']) ? (int) $result['count'] : 0;
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/test_queries.php
Expand Up @@ -22,6 +22,10 @@
$expected = 'SELECT * FROM widget WHERE id = "5"';
Tester::check_equal("Filtering on ID", $expected);

ORM::for_table('widget')->count();
$expected = 'SELECT COUNT(*) AS count FROM widget';
Tester::check_equal("COUNT query", $expected);

ORM::for_table('widget')->where('name', 'Fred')->find_one();
$expected = 'SELECT * FROM widget WHERE name = "Fred"';
Tester::check_equal("Single where clause", $expected);
Expand Down

0 comments on commit 42c375b

Please sign in to comment.