I have a site where members can:
So in my gallery view, all images are visible with their respective image uploader, likes rating (percentage of likes to dislikes), and date uploaded.
I have created an entity object class called Image that holds all of this information and more. So I am trying to find a way to loop through all the images in the gallery as Image objects. Is that possible?
Here is my Image entity class:
class Image extends Entity
{
protected $attributes = [
'viewkey' => NULL,
'id' => NULL,
'uploader' => NULL,
'filename' => NULL,
'title' => NULL,
'tags' => NULL,
'createdAt' => NULL,
'modifiedAt' => NULL,
'likeCount' => NULL,
'dislikeCount' => NULL,
'viewCount' => NULL,
'favoriteCount' => NULL,
'commentCount' => NULL,
'rating' => NULL,
'userLiked' => NULL,
'userDisliked' => NULL,
'userViewed' => NULL,
'userFavorited' => NULL,
'userCommented' => NULL,
'userSubscribed' => NULL,
'action' => NULL,
];
protected $datamap = [
'createdAt' => 'created_at',
'modifiedAt' => 'modified_at',
];
protected $dates = ['createdAt', 'updatedAt',];
protected $casts = [
'likeCount' => 'int',
'dislikeCount' => 'int',
'viewCount' => 'int',
'favoriteCount' => 'int',
'commentCount' => 'int',
'rating' => 'float',
'userDisliked' => 'bool',
'userLiked' => 'bool',
'userViewed' => 'bool',
'userFavorited' => 'bool',
];
protected $builder;
public function __construct (array $data = NULL)
{
parent::__construct($data);
$db = \Config\Database::connect();
$this->builder = $db->table('actions');
}
/**
* Custom __set Methods
*/
public function setDislikeCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 0,
];
$this->attributes['dislikeCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setLikeCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 1,
];
$this->attributes['likeCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setViewCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 2,
];
$this->attributes['viewCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setFavoriteCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 3,
];
$this->attributes['favoriteCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setCommentCount(string $viewkey)
{
$this->attributes['commentCount'] = $this->builder
->where('viewkey', $viewkey)
->countAllResults();
}
public function setRating(string $viewkey)
{
helper('arithmetic');
$whereDislike = $whereLike = [];
$whereDislike = [
'viewkey' => $viewkey,
'action' => 0,
];
$dislikes = $this->builder
->where($whereDislike)
->countAllResults();
$whereLike = [
'viewkey' => $viewkey,
'action' => 1,
];
$likes = $this->builder
->where($whereLike)
->countAllResults();
$this->attributes['rating'] = get_percentage($likes + $dislikes, $likes, 0);
}
public function setUserDisliked(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 0,
];
$userDisliked = $this->builder
->where($where)
->countAllResults();
if ($userDisliked === 1) {
$this->attributes['userDisliked'] = TRUE;
} else {
$this->attributes['userDisliked'] = FALSE;
}
}
public function setUserLiked(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 1,
];
$userLiked = $this->builder
->where($where)
->countAllResults();
if ($userLiked === 1) {
$this->attributes['userLiked'] = TRUE;
} else {
$this->attributes['userLiked'] = FALSE;
}
}
public function setUserViewed(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 2,
];
$userViewed = $this->builder
->where($where)
->countAllResults();
if ($userViewed === 1) {
$this->attributes['userViewed'] = TRUE;
} else {
$this->attributes['userViewed'] = FALSE;
}
}
public function setUserFavorited(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 3,
];
$userFavorited = $this->builder
->where($where)
->countAllResults();
if ($userFavorited === 1) {
$this->attributes['userFavorited'] = TRUE;
} else {
$this->attributes['userFavorited'] = FALSE;
}
}
public function setUserCommented(string $subscriber)
{
$db = \Config\Database::connect();
$this->builder = $db->table('comments');
$userCommented = $this->builder
->where('commenter')
->countAllResults();
if ($userCommented === 1) {
$this->attributes['userCommented'] = TRUE;
} else {
$this->attributes['userCommented'] = FALSE;
}
}
public function setUserSubscribed(string $uploader)
{
$db = \Config\Database::connect();
$this->builder = $db->table('subscribers');
$where = [];
$where = [
'profile' => $uploader,
'subscriber' => session()->get('username'),
];
$userSubscribed = $this->builder
->where($where)
->countAllResults();
if ($userSubscribed === 1) {
$this->attributes['userSubscribed'] = TRUE;
} else {
$this->attributes['userSubscribed'] = FALSE;
}
}
/**
* Custom __get Methods
*/
}
I fill the entity with a function in my ImageModel here:
public function fillImageEntity(string $viewkey)
{
$imageData = $this->builder()
->where('viewkey', $viewkey)
->get()
->getRowArray();
$image = new \App\Entities\Image();
$image->fill($imageData);
$image->setDislikeCount($viewkey);
$image->setLikeCount($viewkey);
$image->setViewCount($viewkey);
$image->setFavoriteCount($viewkey);
$image->setCommentCount($viewkey);
$image->setRating($viewkey);
$image->setUserDisliked($viewkey);
$image->setUserLiked($viewkey);
$image->setUserViewed($viewkey);
$image->setUserFavorited($viewkey);
$image->setUserCommented($viewkey);
$image->setUserSubscribed($imageData['uploader']);
return $image;
}
I have tried make a Gallery object class that would hold the images and then fill that object with Image objects but I get the error you cannot keep an array of Entity objects. Is this a logic error or am I going about it all wrong?
I was able to create a multidimensional array of my entity class Image.php and successfully display the necessary information in my view!
I pass the array into my view here in Gallery.php controller:
public function index()
{
$data = [];
$data = [
'title' => 'Image Gallery',
'gallery' => $this->imageModel->getEntireGallery(),
];
echo view('templates/header', $data);
echo view('templates/navigation');
echo view('templates/filter_bar', $data);
echo view('images/gallery', $data);
echo view('templates/footer', $data);
}
I fill the array and each image object in my model ImageModel.php:
public function fillImageEntity(string $viewkey)
{
$imageData = $this->builder()
->where('viewkey', $viewkey)
->get()
->getRowArray();
$image = new \App\Entities\Image();
$image->fill($imageData);
$image->setDislikeCount($viewkey);
$image->setLikeCount($viewkey);
$image->setViewCount($viewkey);
$image->setFavoriteCount($viewkey);
$image->setCommentCount($viewkey);
$image->setRating($viewkey);
$image->setUserDisliked($viewkey);
$image->setUserLiked($viewkey);
$image->setUserViewed($viewkey);
$image->setUserFavorited($viewkey);
$image->setUserCommented($viewkey);
$image->setUserSubscribed($imageData['uploader']);
return $image;
}
public function getEntireGallery()
{
$images = $this->builder()
->orderBy('modified_at', 'DESC')
->get()
->getResultArray();
foreach ($images as $image) {
$gallery[$image['id']] = $this->fillImageEntity($image['viewkey']);
}
return $gallery;
And here is my Image entity class:
class Image extends Entity
{
protected $attributes = [
'viewkey' => NULL,
'id' => NULL,
'uploader' => NULL,
'filename' => NULL,
'title' => NULL,
'tags' => NULL,
'createdAt' => NULL,
'modifiedAt' => NULL,
'likeCount' => NULL,
'dislikeCount' => NULL,
'viewCount' => NULL,
'favoriteCount' => NULL,
'commentCount' => NULL,
'rating' => NULL,
'userLiked' => NULL,
'userDisliked' => NULL,
'userViewed' => NULL,
'userFavorited' => NULL,
'userCommented' => NULL,
'userSubscribed' => NULL,
'action' => NULL,
];
protected $datamap = [
'createdAt' => 'created_at',
'modifiedAt' => 'modified_at',
];
protected $dates = ['createdAt', 'updatedAt',];
protected $casts = [
'likeCount' => 'int',
'dislikeCount' => 'int',
'viewCount' => 'int',
'favoriteCount' => 'int',
'commentCount' => 'int',
'rating' => 'float',
'userDisliked' => 'bool',
'userLiked' => 'bool',
'userViewed' => 'bool',
'userFavorited' => 'bool',
];
protected $builder;
public function __construct (array $data = NULL)
{
parent::__construct($data);
$db = \Config\Database::connect();
$this->builder = $db->table('actions');
}
/**
* Custom __set Methods
*/
public function setDislikeCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 0,
];
$this->attributes['dislikeCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setLikeCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 1,
];
$this->attributes['likeCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setViewCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 2,
];
$this->attributes['viewCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setFavoriteCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 3,
];
$this->attributes['favoriteCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setCommentCount(string $viewkey)
{
$this->attributes['commentCount'] = $this->builder
->where('viewkey', $viewkey)
->countAllResults();
}
public function setRating(string $viewkey)
{
helper('arithmetic');
$whereDislike = $whereLike = [];
$whereDislike = [
'viewkey' => $viewkey,
'action' => 0,
];
$dislikes = $this->builder
->where($whereDislike)
->countAllResults();
$whereLike = [
'viewkey' => $viewkey,
'action' => 1,
];
$likes = $this->builder
->where($whereLike)
->countAllResults();
$this->attributes['rating'] = get_percentage($likes + $dislikes, $likes, 0);
}
public function setUserDisliked(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 0,
];
$userDisliked = $this->builder
->where($where)
->countAllResults();
if ($userDisliked === 1) {
$this->attributes['userDisliked'] = TRUE;
} else {
$this->attributes['userDisliked'] = FALSE;
}
}
public function setUserLiked(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 1,
];
$userLiked = $this->builder
->where($where)
->countAllResults();
if ($userLiked === 1) {
$this->attributes['userLiked'] = TRUE;
} else {
$this->attributes['userLiked'] = FALSE;
}
}
public function setUserViewed(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 2,
];
$userViewed = $this->builder
->where($where)
->countAllResults();
if ($userViewed === 1) {
$this->attributes['userViewed'] = TRUE;
} else {
$this->attributes['userViewed'] = FALSE;
}
}
public function setUserFavorited(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 3,
];
$userFavorited = $this->builder
->where($where)
->countAllResults();
if ($userFavorited === 1) {
$this->attributes['userFavorited'] = TRUE;
} else {
$this->attributes['userFavorited'] = FALSE;
}
}
public function setUserCommented(string $subscriber)
{
$db = \Config\Database::connect();
$this->builder = $db->table('comments');
$userCommented = $this->builder
->where('commenter')
->countAllResults();
if ($userCommented === 1) {
$this->attributes['userCommented'] = TRUE;
} else {
$this->attributes['userCommented'] = FALSE;
}
}
public function setUserSubscribed(string $uploader)
{
$db = \Config\Database::connect();
$this->builder = $db->table('subscribers');
$where = [];
$where = [
'profile' => $uploader,
'subscriber' => session()->get('username'),
];
$userSubscribed = $this->builder
->where($where)
->countAllResults();
if ($userSubscribed === 1) {
$this->attributes['userSubscribed'] = TRUE;
} else {
$this->attributes['userSubscribed'] = FALSE;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.