簡體   English   中英

CodeIgniter 4:如何顯示圖像實體對象庫?

[英]CodeIgniter 4: How do I display a gallery of image entity objects?

我有一個網站,會員可以:

  • 上傳圖片
  • 訂閱其他成員
  • 喜歡、不喜歡和最喜歡的圖像
  • 查看彼此的個人資料

因此,在我的圖庫視圖中,所有圖像都可以通過其各自的圖像上傳器、喜歡評級(喜歡到不喜歡的百分比)和上傳日期來查看。

我創建了一個名為 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 對象填充該對象,但我收到錯誤消息,您無法保留 Entity 對象數組。 這是一個邏輯錯誤還是我做錯了?

我能夠創建我的實體類 Image.php 的多維數組,並成功地在我的視圖中顯示必要的信息!

我在 Gallery.php 控制器中將數組傳遞到我的視圖中:

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);
    }

我在我的模型 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;

這是我的圖像實體類:

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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM