Simplified a few method calls in model layer

This commit is contained in:
Marcin Kurczewski 2014-06-13 11:52:09 +02:00
parent 8a2e6948bc
commit a243f6b91a
6 changed files with 13 additions and 29 deletions

View file

@ -15,11 +15,15 @@ abstract class AbstractCrudModel implements IModel
public static function spawnFromDatabaseRows($input)
{
if (!$input)
return [];
return array_map([get_called_class(), 'spawnFromDatabaseRow'], $input);
}
public static function spawnFromDatabaseRow($row)
{
if (!$row)
return null;
$entityClassName = static::getEntityClassName();
$entity = new $entityClassName(new static);
$entity->fillFromDatabase($row);
@ -106,12 +110,8 @@ abstract class AbstractCrudModel implements IModel
$stmt->setColumn('*');
$stmt->setTable(static::getTableName());
$stmt->setCriterion(Sql\Functors::in('id', Sql\Binding::fromArray(array_unique($ids))));
$rows = Core::getDatabase()->fetchAll($stmt);
if ($rows)
return static::spawnFromDatabaseRows($rows);
return [];
return static::spawnFromDatabaseRows($rows);
}
public static function getCount()

View file

@ -58,9 +58,7 @@ final class CommentModel extends AbstractCrudModel
$stmt->setCriterion(Sql\Functors::equals('post_id', new Sql\Binding($key)));
$rows = Core::getDatabase()->fetchAll($stmt);
if ($rows)
return self::spawnFromDatabaseRows($rows);
return [];
return self::spawnFromDatabaseRows($rows);
}

View file

@ -135,9 +135,7 @@ final class PostModel extends AbstractCrudModel
$stmt->setCriterion(Sql\Functors::equals('name', new Sql\Binding($key)));
$row = Core::getDatabase()->fetchOne($stmt);
return $row
? self::spawnFromDatabaseRow($row)
: null;
return self::spawnFromDatabaseRow($row);
}
public static function getByIdOrName($key)
@ -165,9 +163,7 @@ final class PostModel extends AbstractCrudModel
$stmt->setCriterion(Sql\Functors::equals('file_hash', new Sql\Binding($key)));
$row = Core::getDatabase()->fetchOne($stmt);
return $row
? self::spawnFromDatabaseRow($row)
: null;
return self::spawnFromDatabaseRow($row);
}

View file

@ -120,9 +120,7 @@ final class TagModel extends AbstractCrudModel
$stmt->setCriterion(Sql\Functors::equals('post_tag.post_id', new Sql\Binding($key)));
$rows = Core::getDatabase()->fetchAll($stmt);
if ($rows)
return self::spawnFromDatabaseRows($rows);
return [];
return self::spawnFromDatabaseRows($rows);
}
public static function getByName($key)
@ -141,9 +139,7 @@ final class TagModel extends AbstractCrudModel
$stmt->setCriterion(Sql\Functors::noCase(Sql\Functors::equals('name', new Sql\Binding($key))));
$row = Core::getDatabase()->fetchOne($stmt);
return $row
? self::spawnFromDatabaseRow($row)
: null;
return self::spawnFromDatabaseRow($row);
}

View file

@ -55,9 +55,7 @@ final class TokenModel extends AbstractCrudModel
$stmt->setCriterion(Sql\Functors::equals('token', new Sql\Binding($key)));
$row = Core::getDatabase()->fetchOne($stmt);
return $row
? self::spawnFromDatabaseRow($row)
: null;
return self::spawnFromDatabaseRow($row);
}
public static function checkValidity($token)

View file

@ -94,9 +94,7 @@ final class UserModel extends AbstractCrudModel
$stmt->setCriterion(Sql\Functors::noCase(Sql\Functors::equals('name', new Sql\Binding(trim($key)))));
$row = Core::getDatabase()->fetchOne($stmt);
return $row
? self::spawnFromDatabaseRow($row)
: null;
return self::spawnFromDatabaseRow($row);
}
public static function getByEmail($key)
@ -119,9 +117,7 @@ final class UserModel extends AbstractCrudModel
->add(Sql\Functors::noCase(Sql\Functors::equals('email_confirmed', new Sql\Binding($key)))));
$row = Core::getDatabase()->fetchOne($stmt);
return $row
? self::spawnFromDatabaseRow($row)
: null;
return self::spawnFromDatabaseRow($row);
}