我有一个网站,会员可以:
- 上传图像
- 订阅其他成员
- 喜欢、不喜欢和喜爱的图像
- 查看彼此的个人资料
因此,在我的画廊视图,所有的图像都是可见的,与各自的图像上传,喜欢的评级(喜欢的百分比,不喜欢),和上传日期。
我创建了一个实体对象类Image,它包含所有这些信息以及更多信息。因此,我试图找到一种方法,将图库中的所有图像作为Image对象进行循环。这可能吗?
下面是我的Image实体类:
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
*/
}
我在ImageModel中用一个函数填充实体:
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;
}
我试过创建一个Gallery对象类来保存图像,然后用Image对象填充该对象,但是我得到了一个错误,你不能保存实体对象的数组。这是逻辑错误还是我的做法都错了?
1条答案
按热度按时间o2gm4chl1#
我能够创建我的实体类Image.php的多维数组,并成功地在视图中显示必要的信息!
我将数组传递到Gallery.php控制器的视图中:
我在我的模型ImageModel.php中填充数组和每个图像对象:
下面是我的Image实体类: