bug fixes

This commit is contained in:
Dennis Eichhorn 2021-06-05 11:02:50 +02:00
parent 5b9315bf71
commit 2e4af70139

View File

@ -14,6 +14,7 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database;
use Mpdf\Tag\P;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Exception\InvalidMapperException;
use phpOMS\DataStorage\Database\Query\Builder;
@ -678,15 +679,16 @@ class DataMapperAbstract implements DataMapperInterface
*
* @param object $obj Model to create
* @param \ReflectionClass $refClass Reflection class
* @param string $member Member name for the id, if it is not the primary key
*
* @return mixed
*
* @since 1.0.0
*/
public static function getObjectId(object $obj, \ReflectionClass $refClass = null) : mixed
public static function getObjectId(object $obj, \ReflectionClass $refClass = null, string $member = null) : mixed
{
$refClass ??= new \ReflectionClass($obj);
$propertyName = static::$columns[static::$primaryField]['internal'];
$propertyName = static::$columns[$member ?? static::$primaryField]['internal'];
$refProp = $refClass->getProperty($propertyName);
if (!$refProp->isPublic()) {
@ -1006,8 +1008,9 @@ class DataMapperAbstract implements DataMapperInterface
/** @var self $mapper */
$mapper = static::$belongsTo[$propertyName]['mapper'];
$primaryKey = $mapper::getObjectId($obj);
$primaryKey = $mapper::getObjectId($obj, member: static::$belongsTo[$propertyName]['by'] ?? null);
// @todo: the $mapper::create() might cause a problem is 'by' is set. because we don't want to create this obj but the child obj.
return empty($primaryKey) ? $mapper::create($obj) : $primaryKey;
}
@ -2172,6 +2175,15 @@ class DataMapperAbstract implements DataMapperInterface
return $mapper::createNullModel();
}
// get the belongs to based on a different column (not primary key)
// this is often used if the value is actually a different model:
// you want the profile but the account id is referenced
// in this case you can get the profile by loading the profile based on the account reference column
if (isset(static::$belongsTo[$member]['by'])) {
return $mapper::getBy($result[$mapper::getColumnByMember(static::$belongsTo[$member]['by']) . '_' . $depth], static::$belongsTo[$member]['by']);
}
$obj = $mapper::getInitialized($mapper, $result[$mapper::$primaryField . '_' . $depth], $depth);
return $obj ?? $mapper::populateAbstract($result, $mapper::createBaseModel(), $depth);