public function testRange() { $test_model = new TestModel(); $test_model->integer_field = 1; $test_model->text_field = "feh"; $test_model->date_field = date("Y-m-d H:i:s"); $test_model->save(); $select = new Select("test_models"); $select->range(0, 1); $result = $select->execute(); $this->assertTrue(is_array($result)); $this->assertEquals(1, count($result)); $test_model->delete(); }
/** * @param \Thru\ActiveRecord\DatabaseLayer\Select $thing * @return array * @throws \Thru\ActiveRecord\DatabaseLayer\Exception */ public function processSelect(DatabaseLayer\Select $thing) { $fields = array(); $tables = array(); $orders = array(); // SELECTORS foreach ($thing->getTables() as $table) { /* @var $table DatabaseLayer\Table */ $tables[] = $table->getName() . " " . $table->getAlias(); foreach ($table->getFields() as $field) { $fields[] = $table->getAlias() . "." . $field; } } $selector = "SELECT " . implode(" ", $fields); $from = "FROM " . implode(" ", $tables); $conditions = $this->processConditions($thing); // Handle LIMIT & OFFSET $limit = ''; $offset = ''; if ($thing->getLimit()) { $limit = "LIMIT {$thing->getLimit()}"; if ($thing->getOffset()) { $offset = "OFFSET {$thing->getOffset()}"; } } // Handle ORDERs if (count($thing->getOrders()) > 0) { foreach ($thing->getOrders() as $order) { /* @var $order DatabaseLayer\Order */ $column = $order->getColumn(); switch (strtolower($order->getDirection())) { case 'asc': case 'ascending': $direction = 'ASC'; break; case 'desc': case 'descending': $direction = 'DESC'; break; case 'rand()': case 'rand': case 'random()': case 'random': $column = ''; $direction = 'rand()'; break; default: throw new Exception("Bad ORDER direction: {$order->getDirection()}"); } $orders[] = $column . " " . $direction; } } if (count($orders) > 0) { $order = "ORDER BY " . implode(", ", $orders); } else { $order = null; } $query = "{$selector}\n{$from}\n{$conditions}\n{$order}\n{$limit} {$offset}"; $delay = microtime(true); $result = $this->query($query, $thing->getModel()); $delay = microtime(true) - $delay; // TODO: Make this a Collection. $results = array(); if ($result !== false) { foreach ($result as $result_item) { $results[] = $result_item; } } return $results; }