コード例 #1
0
ファイル: SqlTableTest.php プロジェクト: jivoo/data
 public function testCount()
 {
     $db = $this->getDb();
     $table = new SqlTable($db, 'Foo');
     // Count all
     $selection = new ReadSelectionBuilder($table);
     $db->expects($this->exactly(2))->method('query')->withConsecutive([$this->equalTo('SELECT COUNT(*) AS _count FROM {Foo}')], [$this->equalTo('SELECT COUNT(*) AS _count FROM (SELECT 1 FROM {Foo} GROUP BY a) AS _selection_count')])->willReturnCallback(function () {
         return $this->getResultSet([['_count' => 42]]);
     });
     $this->assertEquals(42, $selection->count());
     // Count groups
     $this->assertEquals(42, $selection->groupBy('a')->count());
 }
コード例 #2
0
 public function testRead()
 {
     $data = [['id' => 1, 'name' => 'foo', 'group' => 'admin'], ['id' => 2, 'name' => 'bar', 'group' => 'user'], ['id' => 3, 'name' => 'foobar', 'group' => 'user'], ['id' => 4, 'name' => 'foo', 'group' => 'admin'], ['id' => 5, 'name' => 'baz', 'group' => 'user'], ['id' => 6, 'name' => 'foobaz', 'group' => 'user']];
     // Select all
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $this->assertEquals(6, count($selection->toArray()));
     $this->assertEquals($data, $selection->toArray());
     // Select with a predicate
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $selection = $selection->where('group = %s', 'user');
     $this->assertEquals(4, count($selection->toArray()));
     $this->assertNotContains($data[0], $selection->toArray());
     $this->assertContains($data[1], $selection->toArray());
     // Select with groups
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $selection = $selection->groupBy(['group']);
     $this->assertEquals(2, count($selection->toArray()));
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $selection = $selection->groupBy(['group', 'name'], E::e('name in %s()', ['foo', 'bar', 'foobar']));
     $this->assertEquals(3, count($selection->toArray()));
     // Select with limit
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $selection = $selection->limit(1);
     $this->assertEquals(1, count($selection->toArray()));
     $this->assertContains($data[0], $selection->toArray());
     // Select with projection
     $source = new ArrayDataSource($data);
     $selection = new ReadSelectionBuilder($source);
     $projection = $selection->select(['n' => 'name']);
     $this->assertEquals(6, iterator_count($projection));
     foreach ($projection as $record) {
         $this->assertEquals(['n'], array_keys($record));
     }
 }
コード例 #3
0
ファイル: ReadableTrait.php プロジェクト: jivoo/data
 /**
  * Get iterator.
  *
  * @return \Iterator Iterator
  */
 public function getIterator()
 {
     $selection = new ReadSelectionBuilder($this->getSource());
     return $selection->getIterator();
 }
コード例 #4
0
ファイル: ActiveModel.php プロジェクト: jivoo/data
 /**
  * {@inheritdoc}
  */
 public function lastSelection(ReadSelectionBuilder $selection)
 {
     $resultSet = $this->source->readSelection($selection->reverseOrder()->limit(1));
     if (!$resultSet->hasRows()) {
         return null;
     }
     return $this->createExisting($resultSet->fetchAssoc(), $selection);
 }