mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-12 06:48:41 +00:00
Create getFor()
This commit is contained in:
parent
a154cb4892
commit
e193a42007
|
|
@ -1886,6 +1886,66 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
return count($obj) === 1 ? reset($obj) : $obj;
|
return count($obj) === 1 ? reset($obj) : $obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get object.
|
||||||
|
*
|
||||||
|
* @param mixed $refKey Key
|
||||||
|
* @param string $ref The field that defines the for
|
||||||
|
* @param int $relations Load relations
|
||||||
|
* @param mixed $fill Object to fill
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public static function getFor($refKey, string $ref, int $relations = RelationType::ALL, $fill = null)
|
||||||
|
{
|
||||||
|
if(!isset(self::$parentMapper)) {
|
||||||
|
self::setUpParentMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
self::extend(__CLASS__);
|
||||||
|
|
||||||
|
$refKey = (array) $refKey;
|
||||||
|
$obj = [];
|
||||||
|
|
||||||
|
foreach ($refKey as $key => $value) {
|
||||||
|
// todo: this only works for belongsTo not for many-to-many relations. Implement many-to-many
|
||||||
|
$obj[$value] = self::get(self::getPrimaryKeyBy($value, self::getColumnByMember($ref)), $relations, $fill);
|
||||||
|
}
|
||||||
|
return count($obj) === 1 ? reset($obj) : $obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get object.
|
||||||
|
*
|
||||||
|
* @param mixed $refKey Key
|
||||||
|
* @param string $ref The field that defines the for
|
||||||
|
* @param int $relations Load relations
|
||||||
|
* @param mixed $fill Object to fill
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public static function getForArray($refKey, string $ref, int $relations = RelationType::ALL, $fill = null)
|
||||||
|
{
|
||||||
|
if(!isset(self::$parentMapper)) {
|
||||||
|
self::setUpParentMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
self::extend(__CLASS__);
|
||||||
|
|
||||||
|
$refKey = (array) $refKey;
|
||||||
|
$obj = [];
|
||||||
|
|
||||||
|
foreach ($refKey as $key => $value) {
|
||||||
|
// todo: this only works for belongsTo not for many-to-many relations. Implement many-to-many
|
||||||
|
$obj[$value] = self::getArray(self::getPrimaryKeyBy($value, self::getColumnByMember($ref)), $relations, $fill);
|
||||||
|
}
|
||||||
|
return count($obj) === 1 ? reset($obj) : $obj;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get object.
|
* Get object.
|
||||||
*
|
*
|
||||||
|
|
@ -2120,6 +2180,31 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
return is_bool($results) ? [] : $results;
|
return is_bool($results) ? [] : $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get object.
|
||||||
|
*
|
||||||
|
* @param mixed $refKey Key
|
||||||
|
* @param string $ref Ref
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public static function getPrimaryKeyBy($refKey, string $ref) : array
|
||||||
|
{
|
||||||
|
$query = self::getQuery();
|
||||||
|
$query->select(static::$primaryField)
|
||||||
|
->from(static::$table)
|
||||||
|
->where(static::$table . '.' . $ref, '=', $refKey);
|
||||||
|
|
||||||
|
$sth = self::$db->con->prepare($query->toSql());
|
||||||
|
$sth->execute();
|
||||||
|
|
||||||
|
$results = array_column($sth->fetchAll(\PDO::FETCH_NUM) ?? [], 0);
|
||||||
|
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all in raw output.
|
* Get all in raw output.
|
||||||
*
|
*
|
||||||
|
|
@ -2352,4 +2437,15 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
|
|
||||||
return count($results) === 0;
|
return count($results) === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function getColumnByMember(string $name) : string
|
||||||
|
{
|
||||||
|
foreach(static::$columns as $cName => $column) {
|
||||||
|
if($column['internal'] === $name) {
|
||||||
|
return $cName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw \Exception();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user