Further cleanup

Todo: fix population for hasMany, hasOne, ownsOne
This commit is contained in:
Dennis Eichhorn 2016-08-08 15:01:49 +02:00
parent 07b1264f90
commit 8ca2f07108

View File

@ -38,7 +38,7 @@ class DataMapperAbstract implements DataMapperInterface
/** /**
* Database connection. * Database connection.
* *
* @var \phpOMS\DataStorage\Database\Connection\ConnectionAbstract * @var ConnectionAbstract
* @since 1.0.0 * @since 1.0.0
*/ */
protected static $db = null; protected static $db = null;
@ -1003,7 +1003,13 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
public static function getRandom(int $relations = RelationType::ALL) public static function getRandom(int $relations = RelationType::ALL)
{ {
// todo: implement $query = new Builder(self::$db);
$query->prefix(self::$db->getPrefix())
->select(static::$primaryKey)
->from(static::$table)
->random();
return self::get(self::$db->con->prepare($query->toSql())->execute(), $relations);
} }
/** /**
@ -1021,13 +1027,14 @@ class DataMapperAbstract implements DataMapperInterface
{ {
$hasMany = count(static::$hasMany) > 0; $hasMany = count(static::$hasMany) > 0;
$hasOne = count(static::$hasOne) > 0; $hasOne = count(static::$hasOne) > 0;
$ownsOne = count(static::$ownsOne) > 0;
if ($relations !== RelationType::NONE && ($hasMany || $hasOne)) { if ($relations !== RelationType::NONE && ($hasMany || $hasOne)) {
foreach ($obj as $key => $value) { foreach ($obj as $key => $value) {
/* 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::getManyRaw($key, $relations), $obj[$key]); self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key]);
} }
if ($hasOne) { if ($hasOne) {
@ -1058,8 +1065,6 @@ class DataMapperAbstract implements DataMapperInterface
$results = $sth->fetch(\PDO::FETCH_ASSOC); $results = $sth->fetch(\PDO::FETCH_ASSOC);
// todo: implement getRawRelations() ?!!!!!
return is_bool($results) ? [] : $results; return is_bool($results) ? [] : $results;
} }
@ -1093,7 +1098,7 @@ class DataMapperAbstract implements DataMapperInterface
* @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 getManyRaw($primaryKey, int $relations = RelationType::ALL) : array public static function getHasManyRaw($primaryKey, int $relations = RelationType::ALL) : array
{ {
$result = []; $result = [];