Because of this, Query objects are the primary method of communication between Model classes and backend data sources. This helps to keep APIs abstract and flexible, since a model is only required to call a single method against its backend. Since the Query object simply acts as a structured data container, each backend can choose how to operate on the data the Query contains. See each class method for more details on what data this class supports.
See also: lithium\data\Model
See also: lithium\data\Source
Inheritance: extends lithium\core\Object
Example #1
0
 /**
  * Tests basic property accessors and mutators.
  *
  * @return void
  */
 public function testBasicAssignments()
 {
     $query = new Query();
     $group = array('key' => 'hits', 'reduce' => 'function() {}');
     $calculate = 'count';
     $this->assertNull($query->group());
     $query->group($group);
     $this->assertEqual($group, $query->group());
     $this->assertNull($query->calculate());
     $query->calculate($calculate);
     $this->assertEqual($calculate, $query->calculate());
     $query = new Query(compact('calculate', 'group'));
     $this->assertEqual($group, $query->group());
     $this->assertEqual($calculate, $query->calculate());
 }
Example #2
0
 public function testCreate()
 {
     $entity = new Record(array('model' => $this->_model, 'data' => array('title' => 'new post', 'body' => 'the body')));
     $query = new Query(compact('entity') + array('type' => 'create', 'model' => $this->_model));
     $hash = $query->export($this->db);
     ksort($hash);
     $expected = sha1(serialize($hash));
     $result = $this->db->create($query);
     $this->assertTrue($result);
     $result = $query->entity()->id;
     $this->assertEqual($expected, $result);
     $expected = "INSERT INTO {mock_database_posts} ({title}, {body})";
     $expected .= " VALUES ('new post', 'the body');";
     $result = $this->db->sql;
     $this->assertEqual($expected, $result);
 }
Example #3
0
 public function testExportWithUndefinedStrategy()
 {
     $query = new Query(array('alias' => 'MyAlias', 'model' => 'lithium\\tests\\mocks\\data\\model\\MockGallery', 'calculate' => 'MyCalculate', 'comment' => 'No comment', 'conditions' => array('id' => 2), 'fields' => array('Image.ImageTag.Tag'), 'type' => 'read', 'with' => array('Image.ImageTag.Tag', 'Image', 'Image.ImageTag'), 'strategy' => 'custom'));
     $this->expectException('Undefined query strategy `custom`.');
     $export = $query->export($this->db);
 }
Example #4
0
 /**
  * The `Query` object shouldn't overwrite custom values with model-supplied values.
  */
 public function testQueryWithCustomAlias()
 {
     $model = 'lithium\\tests\\mocks\\data\\model\\MockQueryComment';
     $query = new Query(compact('model') + array('source' => 'my_custom_table', 'alias' => 'MyCustomAlias'));
     $result = $query->export(Connections::get('mock-database-connection'));
     $this->assertEqual('{my_custom_table}', $result['source']);
     $this->assertEqual('AS {MyCustomAlias}', $result['alias']);
 }
 public function testRespondsTo()
 {
     $query = new Query();
     $this->assertTrue($query->respondsTo('calculate'));
     $this->assertFalse($query->respondsTo('foobarbaz'));
 }
Example #6
0
 /**
  * Assert that Mongo and the Mongo Exporter don't mangle manual geospatial queries.
  */
 public function testGeoQueries()
 {
     $coords = array(84.13, 11.38);
     $coords2 = array_map(function ($point) {
         return $point + 5;
     }, $coords);
     $conditions = array('location' => array('$near' => $coords));
     $query = new Query(compact('conditions') + array('model' => $this->_model));
     $result = $query->export($this->_db);
     $this->assertEqual($result['conditions'], $conditions);
     $conditions = array('location' => array('$within' => array('$box' => array($coords2, $coords))));
     $query = new Query(compact('conditions') + array('model' => $this->_model));
     $result = $query->export($this->_db);
     $this->assertEqual($conditions, $result['conditions']);
 }
 public function testExportedFieldsWithJoinedStrategyAndRecursiveRelation()
 {
     $query = new Query(array('model' => $this->_gallery, 'with' => array('Parent.Parent')));
     $result = $query->export($this->db);
     $expected = '*';
     $this->assertEqual($expected, $result['fields']);
     $query = new Query(array('model' => $this->_gallery, 'fields' => 'Parent.name', 'with' => array('Parent.Parent')));
     $result = $query->export($this->db);
     $expected = '{Gallery}.{id}, {Parent}.{name}';
     $this->assertEqual($expected, $result['fields']);
     $query = new Query(array('model' => $this->_gallery, 'fields' => 'ParentOfParent.name', 'with' => array('Parent.Parent' => array('alias' => 'ParentOfParent'))));
     $result = $query->export($this->db);
     $expected = '{Gallery}.{id}, {ParentOfParent}.{name}, {Parent}.{id}';
     $this->assertEqual($expected, $result['fields']);
 }
Example #8
0
 /**
  * Returns a LIMIT statement from the given limit and the offset of the context object.
  *
  * @param integer $limit
  * @param \lithium\data\model\Query $context
  * @return string
  */
 public function limit($limit, $context)
 {
     if (!$limit) {
         return;
     }
     if ($offset = $context->offset() ?: '') {
         $offset = " OFFSET {$offset}";
     }
     return "LIMIT {$limit}{$offset}";
 }
Example #9
0
 public function testFluentInterface()
 {
     $query = new Query();
     $conditions = array('foo' => 'bar');
     $fields = array('foo', 'bar', 'baz', 'created');
     $order = array('created' => 'ASC');
     $result = $query->conditions($conditions)->fields($fields)->order($order);
     $this->assertEqual($result, $query);
     $this->assertEqual($conditions, $query->conditions());
     $this->assertEqual($fields, $query->fields());
     $this->assertEqual($order, $query->order());
 }