Skip to content

Commit

Permalink
Added findByColumn; minor improvement to findAllByColumn; minor impro…
Browse files Browse the repository at this point in the history
…vement to docs
  • Loading branch information
crocodile2u committed Dec 28, 2016
1 parent 0d1f29c commit 53af149
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
17 changes: 15 additions & 2 deletions lib/persistence/DbDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ function find($id, Entity $proto)
/**
* Find all entities with column = value.
*
* @param int $id
* @param string $column
* @param mixed $value
* @param int $limit
* @return Entity[]
*/
function findAllByColumn($column, $value, Entity $proto)
function findAllByColumn($column, $value, Entity $proto, $limit = null)
{
$sql = "SELECT * FROM {$proto->getSourceName()} WHERE {$column} = ?";
$stmt = $this->db->prepare($sql);
Expand All @@ -59,6 +61,17 @@ function findAllByColumn($column, $value, Entity $proto)
yield $item;
}
}
/**
* @param string $column
* @param mixed $value
* @return Entity|null
*/
function findByColumn($column, $value, Entity $proto)
{
/** @var \Generator $generator */
$generator = $this->findAllByColumn($column, $value, $proto, 1);
return $generator->current();
}

/**
* @param Entity $entity
Expand Down
12 changes: 10 additions & 2 deletions lib/persistence/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ interface Driver
*/
function find($id, Entity $proto);
/**
* @param int $id
* @param string $column
* @param mixed $value
* @param int $limit
* @return Entity[]
*/
function findAllByColumn($column, $value, Entity $proto, $limit = null);
/**
* @param string $column
* @param mixed $value
* @return Entity
*/
function findAllByColumn($column, $value, Entity $proto);
function findByColumn($column, $value, Entity $proto);
/**
* @param Entity $entity
* @return Entity
Expand Down
19 changes: 16 additions & 3 deletions lib/persistence/ZHandlersocketDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,30 @@ function find($id, Entity $proto)
*
* Because of the limitations of HS,
*
* @param int $id
* @param string $column
* @param mixed $value
* @param int $limit
* @return Entity[]
*/
function findAllByColumn($column, $value, Entity $proto)
function findAllByColumn($column, $value, Entity $proto, $limit = null)
{
$index = $this->getIndex($proto, $column);
$where = $index->createWhereClause("=", [$value])->setLimit(self::FIND_ALL_LIMIT);
$where = $index->createWhereClause("=", [$value])->setLimit($limit ?: self::FIND_ALL_LIMIT);
foreach ($this->getIndex($proto, $column)->findByWhereClause($where) as $row) {
yield (clone $proto)->importArray($row);
}
}
/**
* @param string $column
* @param mixed $value
* @return Entity|null
*/
function findByColumn($column, $value, Entity $proto)
{
/** @var \Generator $generator */
$generator = $this->findAllByColumn($column, $value, $proto, 1);
return $generator->current();
}

/**
* @param Entity $entity
Expand Down

0 comments on commit 53af149

Please sign in to comment.