Ejemplo n.º 1
0
 public function test_property_api()
 {
     // Creating a structured SQL (Makes adding manipulation the query incode easy)
     $sql = new Sql();
     $sql->columns = array('*');
     $sql->setFrom('customers AS c');
     $sql->setJoin('orders', 'inner join', 'c.id = customer_id');
     $sql->where = array('AND', 'c.id = 1', 'orders.id = 1');
     $this->assertSame((string) $sql, 'SELECT * FROM customers AS c INNER JOIN orders ON (c.id = customer_id) WHERE c.id = 1 AND orders.id = 1');
     // Creating a from raw strings (makes it easy to generate the query you want)
     $sql = new Sql();
     $sql->columns = '*';
     $sql->tables = 'customers';
     $sql->where = 'id = 1';
     $this->assertSame((string) $sql, 'SELECT * FROM customers WHERE id = 1');
 }
Ejemplo n.º 2
0
 /**
  * Returns the number of elements in the collection.
  *
  * @link http://php.net/manual/en/class.countable.php
  *
  * @return int
  */
 public function count()
 {
     if ($this->data === null && $this->sql instanceof Sql && is_array($this->sql->groupBy) && count($this->sql->groupBy) === 0) {
         $sql = $this->sql->select('COUNT(*)')->limit(false)->offset(false);
         $count = intval(Connection::instance($this->dbLink)->fetchValue($sql));
         if ($this->sql->offset) {
             $count -= $this->sql->offset;
             if ($count < 0) {
                 return 0;
             }
         }
         if ($this->sql->limit !== false && $count > $this->sql->limit) {
             $count = $this->sql->limit;
         }
         return $count;
     }
     $this->dataToArray();
     return count($this->data);
 }