mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-04 03:38:42 +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();
|
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
|
* Create read mapper
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,8 @@ abstract class MapperType extends Enum
|
||||||
|
|
||||||
public const COUNT_MODELS = 12;
|
public const COUNT_MODELS = 12;
|
||||||
|
|
||||||
|
public const MODEL_EXISTS = 13;
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
public const CREATE = 1001;
|
public const CREATE = 1001;
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,20 @@ final class ReadMapper extends DataMapperAbstract
|
||||||
return $this;
|
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
|
* Create random mapper
|
||||||
*
|
*
|
||||||
|
|
@ -158,6 +172,8 @@ final class ReadMapper extends DataMapperAbstract
|
||||||
return $this->executeGetRaw();
|
return $this->executeGetRaw();
|
||||||
case MapperType::COUNT_MODELS:
|
case MapperType::COUNT_MODELS:
|
||||||
return $this->executeCount();
|
return $this->executeCount();
|
||||||
|
case MapperType::MODEL_EXISTS:
|
||||||
|
return $this->executeExists();
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -227,7 +243,7 @@ final class ReadMapper extends DataMapperAbstract
|
||||||
try {
|
try {
|
||||||
$results = false;
|
$results = false;
|
||||||
|
|
||||||
$sth = $this->db->con->prepare($a = $query->toSql());
|
$sth = $this->db->con->prepare($query->toSql());
|
||||||
if ($sth !== false) {
|
if ($sth !== false) {
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
|
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
|
@ -285,6 +301,20 @@ final class ReadMapper extends DataMapperAbstract
|
||||||
return (int) $query->execute()?->fetchColumn();
|
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
|
* Get random object
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user