mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-04 19:58:39 +00:00
Supporting hasOne.
This commit is contained in:
parent
80d7f78e72
commit
a22bc5d3a0
|
|
@ -253,6 +253,25 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
} else {
|
} else {
|
||||||
/* is not a has many property */
|
/* is not a has many property */
|
||||||
foreach (static::$columns as $key => $column) {
|
foreach (static::$columns as $key => $column) {
|
||||||
|
/* Insert has one first */
|
||||||
|
if (isset(static::$hasOne[$pname]) && is_object($relObj = $property->getValue($obj))) {
|
||||||
|
/* only insert if not already inserted */
|
||||||
|
$mapper = static::$hasOne[$pname]['mapper'];
|
||||||
|
$mapper = new $mapper($this->db);
|
||||||
|
|
||||||
|
$relReflectionClass = new \ReflectionClass(get_class($relObj));
|
||||||
|
$relProperty = $relReflectionClass->getProperty($mapper::$columns[$mapper::$primaryField]['internal']);
|
||||||
|
$relProperty->setAccessible(true);
|
||||||
|
$primaryKey = $relProperty->getValue($relObj);
|
||||||
|
$relProperty->setAccessible(false);
|
||||||
|
|
||||||
|
if ($primaryKey !== 0 && $primaryKey !== null && $primaryKey !== '') {
|
||||||
|
$primaryKey = $mapper->create($property->getValue($relObj));
|
||||||
|
}
|
||||||
|
|
||||||
|
$property->setValue($primaryKey);
|
||||||
|
}
|
||||||
|
|
||||||
if ($column['internal'] === $pname) {
|
if ($column['internal'] === $pname) {
|
||||||
$value = $property->getValue($obj);
|
$value = $property->getValue($obj);
|
||||||
|
|
||||||
|
|
@ -260,7 +279,7 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
$value = isset($value) ? $value->format('Y-m-d H:i:s') : null;
|
$value = isset($value) ? $value->format('Y-m-d H:i:s') : null;
|
||||||
} elseif ($column['type'] === 'Json') {
|
} elseif ($column['type'] === 'Json') {
|
||||||
$value = isset($value) ? json_encode($value) : '';
|
$value = isset($value) ? json_encode($value) : '';
|
||||||
} elseif($column['type'] === 'Serializable') {
|
} elseif ($column['type'] === 'Serializable') {
|
||||||
$value = $value->serialize();
|
$value = $value->serialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -278,7 +297,7 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
$objId = $this->db->con->lastInsertId();
|
$objId = $this->db->con->lastInsertId();
|
||||||
|
|
||||||
// handle relations
|
// handle relations
|
||||||
if($relations) {
|
if ($relations) {
|
||||||
foreach (static::$hasMany as $member => $rel) {
|
foreach (static::$hasMany as $member => $rel) {
|
||||||
/* is a has many property */
|
/* is a has many property */
|
||||||
$property = $reflectionClass->getProperty($member); // throws ReflectionException
|
$property = $reflectionClass->getProperty($member); // throws ReflectionException
|
||||||
|
|
@ -511,6 +530,41 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populate data.
|
||||||
|
*
|
||||||
|
* @param mixed $obj Object to add the relations to
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
|
public function populateOneToOne(&$obj)
|
||||||
|
{
|
||||||
|
$reflectionClass = new \ReflectionClass(get_class($obj));
|
||||||
|
|
||||||
|
foreach (static::$hasOne as $column => $one) {
|
||||||
|
if ($reflectionClass->hasProperty(static::$columns[$column]['internal'])) {
|
||||||
|
$reflectionProperty = $reflectionClass->getProperty(static::$columns[$column]['internal']);
|
||||||
|
|
||||||
|
if (!($accessible = $reflectionProperty->isPublic())) {
|
||||||
|
$reflectionProperty->setAccessible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$mapper = static::$hasOne[$column]['mapper'];
|
||||||
|
$mapper = new $mapper($this->db);
|
||||||
|
|
||||||
|
$value = $mapper->get($reflectionProperty->getValue($obj));
|
||||||
|
$reflectionProperty->setValue($obj, $value);
|
||||||
|
|
||||||
|
if (!$accessible) {
|
||||||
|
$reflectionProperty->setAccessible(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Populate data.
|
* Populate data.
|
||||||
*
|
*
|
||||||
|
|
@ -661,10 +715,19 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($obj)) {
|
if (isset($obj)) {
|
||||||
if ($relations && count(static::$hasMany) > 0) {
|
$hasMany = count(static::$hasMany) > 0;
|
||||||
/* loading relations from relations table and populating them and then adding them to the object */
|
$hasOne = count(static::$hasOne) > 0;
|
||||||
|
|
||||||
|
if ($relations && ($hasMany || $hasOne)) {
|
||||||
foreach ($primaryKey as $key) {
|
foreach ($primaryKey as $key) {
|
||||||
$this->populateManyToMany($this->getRelationsRaw($key), $obj[$key]);
|
/* loading relations from relations table and populating them and then adding them to the object */
|
||||||
|
if ($hasMany) {
|
||||||
|
$this->populateManyToMany($this->getManyRaw($key), $obj[$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($hasOne) {
|
||||||
|
$this->populateOneToOne($obj[$key]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -729,7 +792,7 @@ abstract class DataMapperAbstract implements DataMapperInterface
|
||||||
return $this->populateIterable($results);
|
return $this->populateIterable($results);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRelationsRaw($primaryKey) : array
|
public function getManyRaw($primaryKey) : array
|
||||||
{
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user