Revert "Implemented language support"

This reverts commit 6ce5a7b29f.
This commit is contained in:
Dennis Eichhorn 2017-03-06 19:53:06 +01:00
parent 1e907a6e07
commit d575046609

View File

@ -13,7 +13,7 @@
* @version 1.0.0 * @version 1.0.0
* @link http://orange-management.com * @link http://orange-management.com
*/ */
declare(strict_types = 1); declare(strict_types=1);
namespace phpOMS\DataStorage\Database; namespace phpOMS\DataStorage\Database;
@ -69,14 +69,6 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
protected static $createdAt = ''; protected static $createdAt = '';
/**
* Language
*
* @var string
* @since 1.0.0
*/
protected static $language_field = '';
/** /**
* Columns. * Columns.
* *
@ -405,7 +397,7 @@ class DataMapperAbstract implements DataMapperInterface
private static function getObjectId($obj, \ReflectionClass $reflectionClass = null) private static function getObjectId($obj, \ReflectionClass $reflectionClass = null)
{ {
$reflectionClass = $reflectionClass ?? new \ReflectionClass(get_class($obj)); $reflectionClass = $reflectionClass ?? new \ReflectionClass(get_class($obj));
$reflectionProperty = $reflectionClass->getProperty(static::$columns[static::$primaryField]['internal']); $reflectionProperty = $reflectionClass->getProperty(static::$columns[static::$primaryField]['internal']);
if (!($isPublic = $reflectionProperty->isPublic())) { if (!($isPublic = $reflectionProperty->isPublic())) {
@ -482,7 +474,6 @@ class DataMapperAbstract implements DataMapperInterface
throw new \Exception('No mapper set for relation object.'); throw new \Exception('No mapper set for relation object.');
} }
/** @var DataMapperAbstract $mapper */
$mapper = static::$hasMany[$propertyName]['mapper']; $mapper = static::$hasMany[$propertyName]['mapper'];
$objsIds = []; $objsIds = [];
$relReflectionClass = null; $relReflectionClass = null;
@ -510,7 +501,6 @@ class DataMapperAbstract implements DataMapperInterface
// Setting relation value (id) for relation (since the relation is not stored in an extra relation table) // Setting relation value (id) for relation (since the relation is not stored in an extra relation table)
/** @var string $table */ /** @var string $table */
/** @var array $columns */
if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table) { if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table) {
$relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['dst']]['internal']); $relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['dst']]['internal']);
@ -534,7 +524,7 @@ class DataMapperAbstract implements DataMapperInterface
private static function createHasOne(\ReflectionClass $reflectionClass, $obj) private static function createHasOne(\ReflectionClass $reflectionClass, $obj)
{ {
throw new \Exception(); throw new \Excpetion();
foreach (static::$hasOne as $propertyName => $rel) { foreach (static::$hasOne as $propertyName => $rel) {
@ -557,8 +547,7 @@ class DataMapperAbstract implements DataMapperInterface
private static function createOwnsOne(string $propertyName, $obj) private static function createOwnsOne(string $propertyName, $obj)
{ {
if (is_object($obj)) { if (is_object($obj)) {
/** @var DataMapperAbstract $mapper */ $mapper = static::$ownsOne[$propertyName]['mapper'];
$mapper = static::$ownsOne[$propertyName]['mapper'];
$primaryKey = $mapper::getObjectId($obj); $primaryKey = $mapper::getObjectId($obj);
if (empty($primaryKey)) { if (empty($primaryKey)) {
@ -587,8 +576,7 @@ class DataMapperAbstract implements DataMapperInterface
private static function createBelongsTo(string $propertyName, $obj) private static function createBelongsTo(string $propertyName, $obj)
{ {
if (is_object($obj)) { if (is_object($obj)) {
/** @var DataMapperAbstract $mapper */ $mapper = static::$belongsTo[$propertyName]['mapper'];
$mapper = static::$belongsTo[$propertyName]['mapper'];
$primaryKey = $mapper::getObjectId($obj); $primaryKey = $mapper::getObjectId($obj);
if (empty($primaryKey)) { if (empty($primaryKey)) {
@ -707,7 +695,6 @@ class DataMapperAbstract implements DataMapperInterface
throw new \Exception('No mapper set for relation object.'); throw new \Exception('No mapper set for relation object.');
} }
/** @var DataMapperAbstract $mapper */
$mapper = static::$hasMany[$propertyName]['mapper']; $mapper = static::$hasMany[$propertyName]['mapper'];
$objsIds = []; $objsIds = [];
$relReflectionClass = null; $relReflectionClass = null;
@ -734,8 +721,6 @@ class DataMapperAbstract implements DataMapperInterface
} }
// create if not existing // create if not existing
/** @var string $table */
/** @var array $columns */
if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table) { if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table) {
$relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['dst']]['internal']); $relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['dst']]['internal']);
@ -768,14 +753,11 @@ class DataMapperAbstract implements DataMapperInterface
* *
* @return mixed * @return mixed
* *
* @throws \Exception
*
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
private static function updateRelationTable(string $propertyName, array $objsIds, $objId) private static function updateRelationTable(string $propertyName, array $objsIds, $objId)
{ {
/** @var string $table */
if ( if (
!empty($objsIds) !empty($objsIds)
&& static::$hasMany[$propertyName]['table'] !== static::$table && static::$hasMany[$propertyName]['table'] !== static::$table
@ -783,17 +765,17 @@ class DataMapperAbstract implements DataMapperInterface
) { ) {
$many = self::getHasManyRaw($objId); $many = self::getHasManyRaw($objId);
foreach (static::$hasMany as $member => $value) { foreach(static::$hasMany as $member => $value) {
// todo: definately an error here. needs testing // todo: definately an error here. needs testing
throw new \Exception(); throw new \Exception();
$removes = array_diff_key($many[$member], $objsIds[$member]); $removes = array_diff_key($many[$member], $objsIds[$member]);
$adds = array_diff_key($objsIds[$member], $many[$member]); $adds = array_diff_key($objsIds[$member], $many[$member]);
if (!empty($removes)) { if(!empty($removes)) {
self::deleteRelationTable($propertyName, $removes, $objId); self::deleteRelationTable($propertyName, $removes, $objId);
} }
if (!empty($adds)) { if(!empty($adds)) {
self::createRelationTable($propertyName, $adds, $objId); self::createRelationTable($propertyName, $adds, $objId);
} }
} }
@ -814,7 +796,6 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
private static function deleteRelationTable(string $propertyName, array $objsIds, $objId) private static function deleteRelationTable(string $propertyName, array $objsIds, $objId)
{ {
/** @var string $table */
if ( if (
!empty($objsIds) !empty($objsIds)
&& static::$hasMany[$propertyName]['table'] !== static::$table && static::$hasMany[$propertyName]['table'] !== static::$table
@ -850,7 +831,6 @@ class DataMapperAbstract implements DataMapperInterface
private static function updateOwnsOne(string $propertyName, $obj) private static function updateOwnsOne(string $propertyName, $obj)
{ {
if (is_object($obj)) { if (is_object($obj)) {
/** @var DataMapperAbstract $mapper */
$mapper = static::$ownsOne[$propertyName]['mapper']; $mapper = static::$ownsOne[$propertyName]['mapper'];
// todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one? // todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one?
@ -877,7 +857,6 @@ class DataMapperAbstract implements DataMapperInterface
private static function updateBelongsTo(string $propertyName, $obj) private static function updateBelongsTo(string $propertyName, $obj)
{ {
if (is_object($obj)) { if (is_object($obj)) {
/** @var DataMapperAbstract $mapper */
$mapper = static::$belongsTo[$propertyName]['mapper']; $mapper = static::$belongsTo[$propertyName]['mapper'];
return $mapper::update($obj); return $mapper::update($obj);
@ -890,7 +869,7 @@ class DataMapperAbstract implements DataMapperInterface
* Update object in db. * Update object in db.
* *
* @param Object $obj Model to update * @param Object $obj Model to update
* @param mixed $objId Model id * @param mixed $objId Model id
* @param \ReflectionClass $reflectionClass Reflection class * @param \ReflectionClass $reflectionClass Reflection class
* *
* @return mixed * @return mixed
@ -922,7 +901,7 @@ class DataMapperAbstract implements DataMapperInterface
foreach (static::$columns as $key => $column) { foreach (static::$columns as $key => $column) {
if (isset(static::$ownsOne[$propertyName]) && $column['internal'] === $propertyName) { if (isset(static::$ownsOne[$propertyName]) && $column['internal'] === $propertyName) {
$id = self::updateOwnsOne($propertyName, $property->getValue($obj)); $id = self::updateOwnsOne($propertyName, $property->getValue($obj));
$value = self::parseValue($column['type'], $id); $value = self::parseValue($column['type'], $id);
// todo: should not be done if the id didn't change. but for now don't know if id changed // todo: should not be done if the id didn't change. but for now don't know if id changed
@ -936,7 +915,7 @@ class DataMapperAbstract implements DataMapperInterface
$query->update($column['name'])->value($value, $column['type']); $query->update($column['name'])->value($value, $column['type']);
break; break;
} elseif ($column['internal'] === $propertyName && $column['type'] !== static::$primaryField) { } elseif ($column['internal'] === $propertyName && $column['type'] !== static::$primaryField) {
$value = self::parseValue($column['type'], $property->getValue($obj)); $value = self::parseValue($column['type'], $property->getValue($obj));
$query->update($column['name'])->value($value, $column['type']); $query->update($column['name'])->value($value, $column['type']);
break; break;
@ -954,7 +933,7 @@ class DataMapperAbstract implements DataMapperInterface
/** /**
* Update object in db. * Update object in db.
* *
* @param mixed $obj Object reference (gets filled with insert id) * @param mixed $obj Object reference (gets filled with insert id)
* @param int $relations Create all relations as well * @param int $relations Create all relations as well
* *
* @return int * @return int
@ -966,10 +945,10 @@ class DataMapperAbstract implements DataMapperInterface
{ {
self::extend(__CLASS__); self::extend(__CLASS__);
$reflectionClass = new \ReflectionClass(get_class($obj)); $reflectionClass = new \ReflectionClass(get_class($obj));
$objId = self::getObjectId($obj, $reflectionClass); $objId = self::getObjectId($obj, $reflectionClass);
$update = true; $update = true;
if (empty($objId)) { if(empty($objId)) {
$update = false; $update = false;
self::create($obj, $relations); self::create($obj, $relations);
} }
@ -978,7 +957,7 @@ class DataMapperAbstract implements DataMapperInterface
self::updateHasMany($reflectionClass, $obj, $objId); self::updateHasMany($reflectionClass, $obj, $objId);
} }
if ($update) { if($update) {
self::updateModel($obj, $objId, $reflectionClass); self::updateModel($obj, $objId, $reflectionClass);
} }
@ -991,7 +970,7 @@ class DataMapperAbstract implements DataMapperInterface
* @param \ReflectionClass $reflectionClass Reflection class * @param \ReflectionClass $reflectionClass Reflection class
* @param Object $obj Object to create * @param Object $obj Object to create
* @param mixed $objId Id to set * @param mixed $objId Id to set
* @param int $relations Delete all relations as well * @param int $relations Delete all relations as well
* *
* @return void * @return void
* *
@ -1019,7 +998,6 @@ class DataMapperAbstract implements DataMapperInterface
throw new \Exception('No mapper set for relation object.'); throw new \Exception('No mapper set for relation object.');
} }
/** @var DataMapperAbstract $mapper */
$mapper = static::$hasMany[$propertyName]['mapper']; $mapper = static::$hasMany[$propertyName]['mapper'];
$objsIds = []; $objsIds = [];
$relReflectionClass = null; $relReflectionClass = null;
@ -1040,7 +1018,7 @@ class DataMapperAbstract implements DataMapperInterface
// already in db // already in db
if (!empty($primaryKey)) { if (!empty($primaryKey)) {
if ($relations === RelationType::ALL) { if($relations === RelationType::ALL) {
$objsIds[$key] = $mapper::delete($value); $objsIds[$key] = $mapper::delete($value);
} else { } else {
$objsIds[$key] = $primaryKey; $objsIds[$key] = $primaryKey;
@ -1073,7 +1051,6 @@ class DataMapperAbstract implements DataMapperInterface
private static function deleteOwnsOne(string $propertyName, $obj) private static function deleteOwnsOne(string $propertyName, $obj)
{ {
if (is_object($obj)) { if (is_object($obj)) {
/** @var DataMapperAbstract $mapper */
$mapper = static::$ownsOne[$propertyName]['mapper']; $mapper = static::$ownsOne[$propertyName]['mapper'];
// todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one? // todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one?
@ -1099,7 +1076,6 @@ class DataMapperAbstract implements DataMapperInterface
private static function deleteBelongsTo(string $propertyName, $obj) private static function deleteBelongsTo(string $propertyName, $obj)
{ {
if (is_object($obj)) { if (is_object($obj)) {
/** @var DataMapperAbstract $mapper */
$mapper = static::$belongsTo[$propertyName]['mapper']; $mapper = static::$belongsTo[$propertyName]['mapper'];
return $mapper::delete($obj); return $mapper::delete($obj);
@ -1112,8 +1088,8 @@ class DataMapperAbstract implements DataMapperInterface
* Delete object in db. * Delete object in db.
* *
* @param Object $obj Model to delete * @param Object $obj Model to delete
* @param mixed $objId Model id * @param mixed $objId Model id
* @param int $relations Delete all relations as well * @param int $relations Delete all relations as well
* @param \ReflectionClass $reflectionClass Reflection class * @param \ReflectionClass $reflectionClass Reflection class
* *
* @return mixed * @return mixed
@ -1131,7 +1107,7 @@ class DataMapperAbstract implements DataMapperInterface
$properties = $reflectionClass->getProperties(); $properties = $reflectionClass->getProperties();
if ($relations === RelationType::ALL) { if($relations === RelationType::ALL) {
foreach ($properties as $property) { foreach ($properties as $property) {
$propertyName = $property->getName(); $propertyName = $property->getName();
@ -1168,7 +1144,7 @@ class DataMapperAbstract implements DataMapperInterface
/** /**
* Delete object in db. * Delete object in db.
* *
* @param mixed $obj Object reference (gets filled with insert id) * @param mixed $obj Object reference (gets filled with insert id)
* @param int $relations Create all relations as well * @param int $relations Create all relations as well
* *
* @return int * @return int
@ -1180,9 +1156,9 @@ class DataMapperAbstract implements DataMapperInterface
{ {
self::extend(__CLASS__); self::extend(__CLASS__);
$reflectionClass = new \ReflectionClass(get_class($obj)); $reflectionClass = new \ReflectionClass(get_class($obj));
$objId = self::getObjectId($obj, $reflectionClass); $objId = self::getObjectId($obj, $reflectionClass);
if (empty($objId)) { if(empty($objId)) {
return null; return null;
} }
@ -1450,17 +1426,16 @@ class DataMapperAbstract implements DataMapperInterface
/** /**
* Get object. * Get object.
* *
* @param mixed $primaryKey Key * @param mixed $primaryKey Key
* @param int $relations Load relations * @param int $relations Load relations
* @param mixed $fill Object to fill * @param mixed $fill Object to fill
* @param string $lang Object language
* *
* @return mixed * @return mixed
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public static function get($primaryKey, int $relations = RelationType::ALL, $fill = null, string $lang = '') public static function get($primaryKey, int $relations = RelationType::ALL, $fill = null)
{ {
self::extend(__CLASS__); self::extend(__CLASS__);
@ -1476,14 +1451,14 @@ class DataMapperAbstract implements DataMapperInterface
next($fill); next($fill);
} }
$obj[$value] = self::populate(self::getRaw($value, $lang), $toFill); $obj[$value] = self::populate(self::getRaw($value), $toFill);
if (method_exists($obj[$value], 'initialize')) { if(method_exists($obj[$value], 'initialize')) {
$obj[$value]->initialize(); $obj[$value]->initialize();
} }
} }
self::fillRelations($obj, $relations, $lang); self::fillRelations($obj, $relations);
self::clear(); self::clear();
return count($obj) === 1 ? reset($obj) : $obj; return count($obj) === 1 ? reset($obj) : $obj;
@ -1624,18 +1599,17 @@ class DataMapperAbstract implements DataMapperInterface
* *
* @param mixed $obj Objects to fill * @param mixed $obj Objects to fill
* @param int $relations Relations type * @param int $relations Relations type
* @param string $lang Language
* *
* @return array * @return array
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public static function fillRelations(array &$obj, int $relations = RelationType::ALL, string $lang = '') public static function fillRelations(array &$obj, int $relations = RelationType::ALL)
{ {
$hasMany = !empty(static::$hasMany); $hasMany = !empty(static::$hasMany);
$hasOne = !empty(static::$hasOne); $hasOne = !empty(static::$hasOne);
$ownsOne = !empty(static::$ownsOne); $ownsOne = !empty(static::$ownsOne);
$belongsTo = !empty(static::$belongsTo); $belongsTo = !empty(static::$belongsTo);
if ($relations !== RelationType::NONE && ($hasMany || $hasOne || $ownsOne)) { if ($relations !== RelationType::NONE && ($hasMany || $hasOne || $ownsOne)) {
@ -1643,7 +1617,7 @@ class DataMapperAbstract implements DataMapperInterface
/* loading relations from relations table and populating them and then adding them to the object */ /* loading relations from relations table and populating them and then adding them to the object */
if ($relations !== RelationType::NONE) { if ($relations !== RelationType::NONE) {
if ($hasMany) { if ($hasMany) {
self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key], $lang); self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key]);
} }
if ($hasOne) { if ($hasOne) {
@ -1666,22 +1640,17 @@ class DataMapperAbstract implements DataMapperInterface
* Get object. * Get object.
* *
* @param mixed $primaryKey Key * @param mixed $primaryKey Key
* @param string $lang Language
* *
* @return mixed * @return mixed
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public static function getRaw($primaryKey, string $lang = '') : array public static function getRaw($primaryKey) : array
{ {
$query = self::getQuery(); $query = self::getQuery();
$query->where(static::$table . '.' . static::$primaryField, '=', $primaryKey); $query->where(static::$table . '.' . static::$primaryField, '=', $primaryKey);
if (!empty(self::$language_field)) {
$query->where(static::$table . '.' . static::$language_field, '=', $lang, 'AND');
}
$sth = self::$db->con->prepare($query->toSql()); $sth = self::$db->con->prepare($query->toSql());
$sth->execute(); $sth->execute();
@ -1714,14 +1683,13 @@ class DataMapperAbstract implements DataMapperInterface
* *
* @param mixed $primaryKey Primary key * @param mixed $primaryKey Primary key
* @param int $relations Load relations * @param int $relations Load relations
* @param string $lang Language
* *
* @return array * @return array
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public static function getHasManyRaw($primaryKey, int $relations = RelationType::ALL, string $lang = '') : array public static function getHasManyRaw($primaryKey, int $relations = RelationType::ALL) : array
{ {
$result = []; $result = [];
@ -1730,7 +1698,6 @@ class DataMapperAbstract implements DataMapperInterface
$query->prefix(self::$db->getPrefix()); $query->prefix(self::$db->getPrefix());
if ($relations === RelationType::ALL) { if ($relations === RelationType::ALL) {
/** @var string $primaryField */
$src = $value['src'] ?? $value['mapper']::$primaryField; $src = $value['src'] ?? $value['mapper']::$primaryField;
$query->select($value['table'] . '.' . $src) $query->select($value['table'] . '.' . $src)
@ -1757,12 +1724,6 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
} }
/** @var string $language_field */
if (!empty($value['mapper']::$language_field)) {
/** @var string $table */
$query->where($value['mapper']::$table . '.' . $value['mapper']::$language_field, '=', $lang, 'AND');
}
$sth = self::$db->con->prepare($query->toSql()); $sth = self::$db->con->prepare($query->toSql());
$sth->execute(); $sth->execute();
$result[$member] = $sth->fetchAll(\PDO::FETCH_COLUMN); $result[$member] = $sth->fetchAll(\PDO::FETCH_COLUMN);
@ -1807,7 +1768,7 @@ class DataMapperAbstract implements DataMapperInterface
/** /**
* Get model based on request object * Get model based on request object
* *
* @todo : change to graphql * @todo: change to graphql
* *
* @param RequestAbstract $request Request object * @param RequestAbstract $request Request object
* *