diff options
Diffstat (limited to 'core/namespace/ORM')
-rw-r--r-- | core/namespace/ORM/Repositories/Category.php | 17 | ||||
-rw-r--r-- | core/namespace/ORM/Repositories/Page.php | 10 | ||||
-rw-r--r-- | core/namespace/ORM/Repositories/Post.php | 21 | ||||
-rw-r--r-- | core/namespace/ORM/Repositories/User.php | 27 |
4 files changed, 42 insertions, 33 deletions
diff --git a/core/namespace/ORM/Repositories/Category.php b/core/namespace/ORM/Repositories/Category.php index c00fdf3..df32f69 100644 --- a/core/namespace/ORM/Repositories/Category.php +++ b/core/namespace/ORM/Repositories/Category.php @@ -9,6 +9,19 @@ class Category extends Repository { public static function getClassName(): string { return 'ORM\Entities\Category'; } #=============================================================================== + # Get number of *posts* assigned to $Category + #=============================================================================== + public function getNumberOfPosts(CategoryEntity $Category): int { + $query = 'SELECT COUNT(id) FROM %s WHERE category = ?'; + $query = sprintf($query, Post::getTableName()); + + $Statement = $this->Database->prepare($query); + $Statement->execute([$Category->getID()]); + + return $Statement->fetchColumn(); + } + + #=============================================================================== # Find category with parents based on primary key #=============================================================================== public function findWithParents($id): array { @@ -60,9 +73,9 @@ class Category extends Repository { } #=============================================================================== - # Get children count of $Category + # Get number of children categories assigned to $Category #=============================================================================== - public function getChildrenCount(CategoryEntity $Category): int { + public function getNumberOfChildren(CategoryEntity $Category): int { $query = 'WITH RECURSIVE tree AS ( SELECT * FROM %s WHERE id = ? UNION SELECT c.* FROM %s c, tree WHERE tree.id = c.parent diff --git a/core/namespace/ORM/Repositories/Page.php b/core/namespace/ORM/Repositories/Page.php index 595b420..714605e 100644 --- a/core/namespace/ORM/Repositories/Page.php +++ b/core/namespace/ORM/Repositories/Page.php @@ -6,14 +6,4 @@ use ORM\Entities\User; class Page extends Repository { public static function getTableName(): string { return 'page'; } public static function getClassName(): string { return 'ORM\Entities\Page'; } - - public function getCountByUser(User $User): int { - $query = 'SELECT COUNT(id) FROM %s WHERE user = ?'; - $query = sprintf($query, static::getTableName()); - - $Statement = $this->Database->prepare($query); - $Statement->execute([$User->getID()]); - - return $Statement->fetchColumn(); - } } diff --git a/core/namespace/ORM/Repositories/Post.php b/core/namespace/ORM/Repositories/Post.php index d6328e6..7f672e5 100644 --- a/core/namespace/ORM/Repositories/Post.php +++ b/core/namespace/ORM/Repositories/Post.php @@ -7,25 +7,4 @@ use ORM\Entities\Category; class Post extends Repository { public static function getTableName(): string { return 'post'; } public static function getClassName(): string { return 'ORM\Entities\Post'; } - - public function getCountByUser(User $User): int { - $query = 'SELECT COUNT(id) FROM %s WHERE user = ?'; - $query = sprintf($query, static::getTableName()); - - $Statement = $this->Database->prepare($query); - $Statement->execute([$User->getID()]); - - return $Statement->fetchColumn(); - } - - # TODO: This only gets the count of the direct category, not its children - public function getCountByCategory(Category $Category): int { - $query = 'SELECT COUNT(id) FROM %s WHERE category = ?'; - $query = sprintf($query, static::getTableName()); - - $Statement = $this->Database->prepare($query); - $Statement->execute([$Category->getID()]); - - return $Statement->fetchColumn(); - } } diff --git a/core/namespace/ORM/Repositories/User.php b/core/namespace/ORM/Repositories/User.php index 629d9c1..ce3ff2c 100644 --- a/core/namespace/ORM/Repositories/User.php +++ b/core/namespace/ORM/Repositories/User.php @@ -1,8 +1,35 @@ <?php namespace ORM\Repositories; use ORM\Repository; +use ORM\Entities\User as UserEntity; class User extends Repository { public static function getTableName(): string { return 'user'; } public static function getClassName(): string { return 'ORM\Entities\User'; } + + #=============================================================================== + # Get number of *pages* assigned to $User + #=============================================================================== + public function getNumberOfPages(UserEntity $User): int { + $query = 'SELECT COUNT(id) FROM %s WHERE user = ?'; + $query = sprintf($query, Page::getTableName()); + + $Statement = $this->Database->prepare($query); + $Statement->execute([$User->getID()]); + + return $Statement->fetchColumn(); + } + + #=============================================================================== + # Get number of *posts* assigned to $User + #=============================================================================== + public function getNumberOfPosts(UserEntity $User): int { + $query = 'SELECT COUNT(id) FROM %s WHERE user = ?'; + $query = sprintf($query, Post::getTableName()); + + $Statement = $this->Database->prepare($query); + $Statement->execute([$User->getID()]); + + return $Statement->fetchColumn(); + } } |