Dumplie\Metadata\Infrastructure\Doctrine\Dbal\DoctrineStorage::findBy PHP Method

findBy() public method

Needs to return metadata in following format: [ 'id' => 'e94e4c36-3ffb-49b6-b8a5-973fa5c4aee6', 'sku' => 'DUMPLIE_SKU_1', 'name' => 'Product name' ] Key 'id' is required.
public findBy ( string $schema, string $typeName, array $criteria = [] ) : array
$schema string
$typeName string
$criteria array
return array
    public function findBy(string $schema, string $typeName, array $criteria = []) : array
    {
        $builder = $this->connection->createQueryBuilder();
        $builder->select('*');
        $builder->from($this->tableName($schema, $typeName));
        $builder->setMaxResults(1);
        foreach ($criteria as $field => $value) {
            $builder->andWhere(sprintf('%1$s = :%1$s', $field));
            $builder->setParameter($field, $value);
        }
        return $builder->execute()->fetch(\PDO::FETCH_ASSOC) ?: [];
    }

Usage Example

Example #1
0
 public function test_find_by_returns_empty_list_when_no_matches_found()
 {
     $this->storage->create($this->schema);
     $this->storage->save('test', 'foo', (string) Uuid::uuid4(), ['text' => 'value']);
     $result = $this->storage->findBy('test', 'foo', ['id' => (string) Uuid::uuid4()]);
     $this->assertEmpty($result);
 }