mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-10 14:08:40 +00:00
bugfix datamapper joins
This commit is contained in:
parent
0d087399bc
commit
7300b52d25
|
|
@ -356,10 +356,10 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($hasAutocompletes) {
|
if ($hasAutocompletes) {
|
||||||
$query->andWhere($where2);
|
$query->orWhere($where2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($searchDepth > 1) {
|
if ($searchDepth > 2) {
|
||||||
foreach (static::$ownsOne as $one) {
|
foreach (static::$ownsOne as $one) {
|
||||||
$one['mapper']::findQuery($search, $searchDepth - 1, $query);
|
$one['mapper']::findQuery($search, $searchDepth - 1, $query);
|
||||||
}
|
}
|
||||||
|
|
@ -1866,9 +1866,10 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
/**
|
/**
|
||||||
* Populate data.
|
* Populate data.
|
||||||
*
|
*
|
||||||
* @param string $member Member name
|
* @param string $member Member name
|
||||||
* @param array $result Result data
|
* @param array $result Result data
|
||||||
* @param int $depth Relation depth
|
* @param int $depth Relation depth
|
||||||
|
* @param mixed $default Default value
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*
|
*
|
||||||
|
|
@ -1877,11 +1878,19 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public static function populateOwnsOne(string $member, array $result, int $depth = 3)
|
public static function populateOwnsOne(string $member, array $result, int $depth = 3, $default = null)
|
||||||
{
|
{
|
||||||
/** @var self $mapper */
|
/** @var self $mapper */
|
||||||
$mapper = static::$ownsOne[$member]['mapper'];
|
$mapper = static::$ownsOne[$member]['mapper'];
|
||||||
|
|
||||||
|
if ($depth < 1) {
|
||||||
|
if (\array_key_exists(static::$ownsOne[$member]['self'] . '_' . ($depth + 1), $result)) {
|
||||||
|
return $mapper::createNullModel($result[static::$ownsOne[$member]['self'] . '_' . ($depth + 1)]);
|
||||||
|
} else {
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isset(static::$ownsOne[$member]['column'])) {
|
if (isset(static::$ownsOne[$member]['column'])) {
|
||||||
return $result[$mapper::getColumnByMember(static::$ownsOne[$member]['column']) . '_' . $depth];
|
return $result[$mapper::getColumnByMember(static::$ownsOne[$member]['column']) . '_' . $depth];
|
||||||
}
|
}
|
||||||
|
|
@ -1898,19 +1907,28 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
/**
|
/**
|
||||||
* Populate data.
|
* Populate data.
|
||||||
*
|
*
|
||||||
* @param string $member Member name
|
* @param string $member Member name
|
||||||
* @param array $result Result data
|
* @param array $result Result data
|
||||||
* @param int $depth Relation depth
|
* @param int $depth Relation depth
|
||||||
|
* @param mixed $default Default value
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public static function populateOwnsOneArray(string $member, array $result, int $depth = 3) : array
|
public static function populateOwnsOneArray(string $member, array $result, int $depth = 3, $default = null) : array
|
||||||
{
|
{
|
||||||
/** @var self $mapper */
|
/** @var self $mapper */
|
||||||
$mapper = static::$ownsOne[$member]['mapper'];
|
$mapper = static::$ownsOne[$member]['mapper'];
|
||||||
|
|
||||||
|
if ($depth < 1) {
|
||||||
|
if (\array_key_exists(static::$ownsOne[$member]['self'] . '_' . ($depth + 1), $result)) {
|
||||||
|
return $result[static::$ownsOne[$member]['self'] . '_' . ($depth + 1)];
|
||||||
|
} else {
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isset(static::$ownsOne[$member]['column'])) {
|
if (isset(static::$ownsOne[$member]['column'])) {
|
||||||
return $result[$mapper::getColumnByMember(static::$ownsOne[$member]['column']) . '_' . $depth];
|
return $result[$mapper::getColumnByMember(static::$ownsOne[$member]['column']) . '_' . $depth];
|
||||||
}
|
}
|
||||||
|
|
@ -1927,9 +1945,10 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
/**
|
/**
|
||||||
* Populate data.
|
* Populate data.
|
||||||
*
|
*
|
||||||
* @param string $member Member name
|
* @param string $member Member name
|
||||||
* @param array $result Result data
|
* @param array $result Result data
|
||||||
* @param int $depth Relation depth
|
* @param int $depth Relation depth
|
||||||
|
* @param mixed $default Default value
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*
|
*
|
||||||
|
|
@ -1937,11 +1956,19 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public static function populateBelongsTo(string $member, array $result, int $depth = 3)
|
public static function populateBelongsTo(string $member, array $result, int $depth = 3, $default = null)
|
||||||
{
|
{
|
||||||
/** @var self $mapper */
|
/** @var self $mapper */
|
||||||
$mapper = static::$belongsTo[$member]['mapper'];
|
$mapper = static::$belongsTo[$member]['mapper'];
|
||||||
|
|
||||||
|
if ($depth < 1) {
|
||||||
|
if (\array_key_exists(static::$belongsTo[$member]['self'] . '_' . ($depth + 1), $result)) {
|
||||||
|
return $mapper::createNullModel($result[static::$belongsTo[$member]['self'] . '_' . ($depth + 1)]);
|
||||||
|
} else {
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isset(static::$belongsTo[$member]['column'])) {
|
if (isset(static::$belongsTo[$member]['column'])) {
|
||||||
return $result[$mapper::getColumnByMember(static::$belongsTo[$member]['column']) . '_' . $depth];
|
return $result[$mapper::getColumnByMember(static::$belongsTo[$member]['column']) . '_' . $depth];
|
||||||
}
|
}
|
||||||
|
|
@ -1958,19 +1985,28 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
/**
|
/**
|
||||||
* Populate data.
|
* Populate data.
|
||||||
*
|
*
|
||||||
* @param string $member Member name
|
* @param string $member Member name
|
||||||
* @param array $result Result data
|
* @param array $result Result data
|
||||||
* @param int $depth Relation depth
|
* @param int $depth Relation depth
|
||||||
|
* @param mixed $default Default value
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public static function populateBelongsToArray(string $member, array $result, int $depth = 3) : array
|
public static function populateBelongsToArray(string $member, array $result, int $depth = 3, $default = null) : array
|
||||||
{
|
{
|
||||||
/** @var self $mapper */
|
/** @var self $mapper */
|
||||||
$mapper = static::$belongsTo[$member]['mapper'];
|
$mapper = static::$belongsTo[$member]['mapper'];
|
||||||
|
|
||||||
|
if ($depth < 1) {
|
||||||
|
if (\array_key_exists(static::$belongsTo[$member]['self'] . '_' . ($depth + 1), $result)) {
|
||||||
|
return $result[static::$belongsTo[$member]['self'] . '_' . ($depth + 1)];
|
||||||
|
} else {
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isset(static::$belongsTo[$member]['column'])) {
|
if (isset(static::$belongsTo[$member]['column'])) {
|
||||||
return $result[$mapper::getColumnByMember(static::$belongsTo[$member]['column']) . '_' . $depth];
|
return $result[$mapper::getColumnByMember(static::$belongsTo[$member]['column']) . '_' . $depth];
|
||||||
}
|
}
|
||||||
|
|
@ -2035,19 +2071,21 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset(static::$ownsOne[$def['internal']])) {
|
if (isset(static::$ownsOne[$def['internal']])) {
|
||||||
|
$default = null;
|
||||||
if ($depth - 1 < 1) {
|
if ($depth - 1 < 1) {
|
||||||
continue;
|
$default = $refProp->getValue($obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = self::populateOwnsOne($def['internal'], $result, $depth - 1);
|
$value = self::populateOwnsOne($def['internal'], $result, $depth - 1, $default);
|
||||||
|
|
||||||
$refProp->setValue($obj, $value);
|
$refProp->setValue($obj, $value);
|
||||||
} elseif (isset(static::$belongsTo[$def['internal']])) {
|
} elseif (isset(static::$belongsTo[$def['internal']])) {
|
||||||
|
$default = null;
|
||||||
if ($depth - 1 < 1) {
|
if ($depth - 1 < 1) {
|
||||||
continue;
|
$default = $refProp->getValue($obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = self::populateBelongsTo($def['internal'], $result, $depth - 1);
|
$value = self::populateBelongsTo($def['internal'], $result, $depth - 1, $default);
|
||||||
|
|
||||||
$refProp->setValue($obj, $value);
|
$refProp->setValue($obj, $value);
|
||||||
} elseif (\in_array($def['type'], ['string', 'int', 'float', 'bool'])) {
|
} elseif (\in_array($def['type'], ['string', 'int', 'float', 'bool'])) {
|
||||||
|
|
@ -2180,16 +2218,8 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset(static::$ownsOne[$def['internal']])) {
|
if (isset(static::$ownsOne[$def['internal']])) {
|
||||||
if ($depth - 1 < 1) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$value = self::populateOwnsOneArray(static::$columns[$column]['internal'], $result, $depth - 1);
|
$value = self::populateOwnsOneArray(static::$columns[$column]['internal'], $result, $depth - 1);
|
||||||
} elseif (isset(static::$belongsTo[$def['internal']])) {
|
} elseif (isset(static::$belongsTo[$def['internal']])) {
|
||||||
if ($depth - 1 < 1) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$value = self::populateBelongsToArray(static::$columns[$column]['internal'], $result, $depth - 1);
|
$value = self::populateBelongsToArray(static::$columns[$column]['internal'], $result, $depth - 1);
|
||||||
} elseif (\in_array(static::$columns[$column]['type'], ['string', 'int', 'float', 'bool'])) {
|
} elseif (\in_array(static::$columns[$column]['type'], ['string', 'int', 'float', 'bool'])) {
|
||||||
\settype($value, static::$columns[$column]['type']);
|
\settype($value, static::$columns[$column]['type']);
|
||||||
|
|
@ -2373,7 +2403,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
$obj = [];
|
$obj = [];
|
||||||
|
|
||||||
foreach ($keys as $key => $value) {
|
foreach ($keys as $key => $value) {
|
||||||
if (!self::isInitialized(static::class, $value)) {
|
if (!self::isInitialized(static::class, $value) || $ref !== null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2792,10 +2822,15 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$sth = self::$db->con->prepare($query->toSql());
|
try {
|
||||||
$sth->execute();
|
$sth = self::$db->con->prepare($query->toSql());
|
||||||
|
$sth->execute();
|
||||||
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
|
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
} catch (\Throwable $t) {
|
||||||
|
$results = false;
|
||||||
|
\var_dump($query->toSql());
|
||||||
|
\var_dump($t->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return $results === false ? [] : $results;
|
return $results === false ? [] : $results;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -221,7 +221,7 @@ class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
||||||
|
|
||||||
self::assertEquals(
|
self::assertEquals(
|
||||||
\reset($this->model->hasManyDirect)->string,
|
\reset($this->model->hasManyDirect)->string,
|
||||||
$for->string
|
$for[1]->string
|
||||||
);
|
);
|
||||||
|
|
||||||
self::assertCount(1, BaseModelMapper::getAll());
|
self::assertCount(1, BaseModelMapper::getAll());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user