Welcome Guest, Not a member yet? Register   Sign In
Can I make an array filled with an entity object?
#1

I'm trying to display a gallery of images with attributes like: 
  • number of likes
  • uploader name
  • rating (likes+dislikes/total likes) percentage
I have all these attributes in an image entity class called Image.php defined as follows.
PHP Code:
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$likes0);
    }

    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
    */
    
}  


And I'm trying to make a gallery entity that carries all of those attributes for each image in the gallery. How do I go about doing that?
Reply
#2

(This post was last modified: 06-13-2022, 04:19 AM by AgBRAT.)

I was able to create a multidimensional array of my entity class Image.php and fill this array using these functions defined in my ImageModel.php:
PHP Code:
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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB