mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 09:48:40 +00:00
add exists functionality (similar to count but just checks existence)
This commit is contained in:
parent
febcc20001
commit
e873f909fd
|
|
@ -254,6 +254,20 @@ class DataMapperFactory
|
|||
return (new ReadMapper(new static(), $db ?? self::$db))->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create read mapper
|
||||
*
|
||||
* @param ConnectionAbstract $db Database connection
|
||||
*
|
||||
* @return ReadMapper
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function exists(ConnectionAbstract $db = null) : ReadMapper
|
||||
{
|
||||
return (new ReadMapper(new static(), $db ?? self::$db))->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create read mapper
|
||||
*
|
||||
|
|
|
|||
|
|
@ -38,6 +38,8 @@ abstract class MapperType extends Enum
|
|||
|
||||
public const COUNT_MODELS = 12;
|
||||
|
||||
public const MODEL_EXISTS = 13;
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
||||
public const CREATE = 1001;
|
||||
|
|
|
|||
|
|
@ -102,6 +102,20 @@ final class ReadMapper extends DataMapperAbstract
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create count mapper
|
||||
*
|
||||
* @return self
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function exists() : self
|
||||
{
|
||||
$this->type = MapperType::MODEL_EXISTS;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create random mapper
|
||||
*
|
||||
|
|
@ -158,6 +172,8 @@ final class ReadMapper extends DataMapperAbstract
|
|||
return $this->executeGetRaw();
|
||||
case MapperType::COUNT_MODELS:
|
||||
return $this->executeCount();
|
||||
case MapperType::MODEL_EXISTS:
|
||||
return $this->executeExists();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
|
@ -227,7 +243,7 @@ final class ReadMapper extends DataMapperAbstract
|
|||
try {
|
||||
$results = false;
|
||||
|
||||
$sth = $this->db->con->prepare($a = $query->toSql());
|
||||
$sth = $this->db->con->prepare($query->toSql());
|
||||
if ($sth !== false) {
|
||||
$sth->execute();
|
||||
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
|
@ -285,6 +301,20 @@ final class ReadMapper extends DataMapperAbstract
|
|||
return (int) $query->execute()?->fetchColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if any element exists
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function executeExists() : bool
|
||||
{
|
||||
$query = $this->getQuery(null, ['1']);
|
||||
|
||||
return ($query->execute()?->fetchColumn() ?? 0) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get random object
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user