mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-05-24 02:08:40 +00:00
fix #89
This commit is contained in:
parent
8a6659cdd2
commit
b66feafa7e
|
|
@ -1945,11 +1945,26 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
$obj = [];
|
$obj = [];
|
||||||
|
|
||||||
foreach ($refKey as $key => $value) {
|
foreach ($refKey as $key => $value) {
|
||||||
// todo: this only works for belongsTo not for many-to-many relations. Implement many-to-many
|
$toLoad = [];
|
||||||
$obj[$value] = self::get(self::getPrimaryKeyBy($value, self::getColumnByMember($ref)), $relations, $fill);
|
|
||||||
|
if(isset(static::$hasMany[$ref]) && static::$hasMany[$ref]['src'] !== null) {
|
||||||
|
$toLoad = self::getHasManyPrimaryKeys($value, $ref);
|
||||||
|
} else {
|
||||||
|
$toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($ref));
|
||||||
|
}
|
||||||
|
|
||||||
|
$obj[$value] = self::get($toLoad, $relations, $fill);
|
||||||
}
|
}
|
||||||
|
|
||||||
return count($obj) === 1 ? reset($obj) : $obj;
|
$countResulsts = count($obj);
|
||||||
|
|
||||||
|
if($countResulsts === 0) {
|
||||||
|
return null;
|
||||||
|
} elseif($countResulsts === 1) {
|
||||||
|
return reset($obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1976,8 +1991,15 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
$obj = [];
|
$obj = [];
|
||||||
|
|
||||||
foreach ($refKey as $key => $value) {
|
foreach ($refKey as $key => $value) {
|
||||||
// todo: this only works for belongsTo not for many-to-many relations. Implement many-to-many
|
$toLoad = [];
|
||||||
$obj[$value] = self::getArray(self::getPrimaryKeyBy($value, self::getColumnByMember($ref)), $relations, $fill);
|
|
||||||
|
if(isset(static::$hasMany[$ref]) && static::$hasMany[$ref]['src'] !== null) {
|
||||||
|
$toLoad = self::getHasManyPrimaryKeys($value, $ref);
|
||||||
|
} else {
|
||||||
|
$toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($ref));
|
||||||
|
}
|
||||||
|
|
||||||
|
$obj[$value] = self::get($toLoad, $relations, $fill);
|
||||||
}
|
}
|
||||||
|
|
||||||
return count($obj) === 1 ? reset($obj) : $obj;
|
return count($obj) === 1 ? reset($obj) : $obj;
|
||||||
|
|
@ -2250,7 +2272,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public static function getPrimaryKeyBy($refKey, string $ref) : array
|
public static function getPrimaryKeysBy($refKey, string $ref) : array
|
||||||
{
|
{
|
||||||
$query = new Builder(self::$db);
|
$query = new Builder(self::$db);
|
||||||
$query->prefix(self::$db->getPrefix())
|
$query->prefix(self::$db->getPrefix())
|
||||||
|
|
@ -2266,6 +2288,32 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get object.
|
||||||
|
*
|
||||||
|
* @param mixed $refKey Key
|
||||||
|
* @param string $ref Ref
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public static function getHasManyPrimaryKeys($refKey, string $ref) : array
|
||||||
|
{
|
||||||
|
$query = new Builder(self::$db);
|
||||||
|
$query->prefix(self::$db->getPrefix())
|
||||||
|
->select(static::$hasMany[$ref]['table'] . '.' . static::$hasMany[$ref]['dst'])
|
||||||
|
->from(static::$hasMany[$ref]['table'])
|
||||||
|
->where(static::$hasMany[$ref]['table'] . '.' . static::$hasMany[$ref]['src'], '=', $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.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user