export() public method

Convert the query's properties to the data sources' syntax and return it as an array.
public export ( Source $source, array $options = [] ) : array
$source lithium\data\Source Instance of the data source to use for conversion.
$options array Options to use when exporting the data.
return array Returns an array containing a data source-specific representation of a query.
Example #1
0
 public function testExport()
 {
     $query = new Query($this->_queryArr);
     $ds = new MockDatabase();
     $export = $query->export($ds);
     $this->assertTrue(is_array($export));
     $this->skipIf(!is_array($export), 'Query::export() does not return an array');
     $expected = array('calculate', 'comment', 'conditions', 'data', 'fields', 'group', 'joins', 'limit', 'map', 'model', 'name', 'offset', 'order', 'page', 'source', 'whitelist');
     $result = array_keys($export);
     sort($expected);
     sort($result);
     $this->assertEqual($expected, $result);
     $expected = 'id, author_id, title';
     $result = $export['fields'];
     $this->assertEqual($expected, $result);
     $expected = MockQueryPost::meta('source');
     $result = $export['source'];
     $this->assertEqual($expected, $result);
 }
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
 /**
  * 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']);
 }
Example #4
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 #5
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 #7
0
 public function testInstantiationWithConditionsAndData()
 {
     $options = array('type' => 'update', 'data' => array('title' => '..'), 'conditions' => array('title' => 'FML'), 'model' => 'lithium\\tests\\mocks\\data\\model\\MockQueryPost');
     $query = new Query($options);
     $result = $query->export(Connections::get('mock-database-connection'));
     $this->assertEqual(array('title' => '..'), $result['data']);
     $this->assertEqual("WHERE title = 'FML'", $result['conditions']);
 }