A
A
Artem00712017-11-14 23:54:00
PHP
Artem0071, 2017-11-14 23:54:00

Stick to OOP or is it overkill?

I'm self-taught
I made my own crutch engine and now I'm trying to make it more OOP's
There is a database:
user (user_id, image_id_avatar)
pictures (image_id, image_src)
There is a User class:

private $users = [];
    private $user = [];
    private $userID = null;

    public function __construct($userID = null, $user = [], $users = [])
    {
        $this->userID = $userID;
        $this->user = $user;
        $this->users = $users;
    }

    public function findBy($params = [], $multiple = false)
    {
        $Users = App::DB()->select('users', [], $params); //получаю данные из бд

        if (count($Users)){
            if ($multiple){
                $this->users = $Users;
                return $this;
            }

            if (count($Users) > 1)
                return $this;

            $this->user = $Users[0];
            $this->userID = $Users[0]['User_ID'];
            return $this;
        }

        return $this;
    }


    public function refactor($options = [])
    {
        $userDB = $this->user;

        $image = $userDB['Image_ID_Avatar']
            ? STORAGE_DOMAIN .Image::getImageByID($userDB['Image_ID_Avatar'])['Image_Path']
            : null;

        $this->user = [
            'hash' => $userDB['User_Hash'],
            'nickname' => $userDB['User_Nickname'],
            'avatar' => $image
        ];
        return $this;
    }

Image::getImageByIDreturns the src of the image:
public static function getImageByID($image_id)
    {
        $Image = App::DB()->select('images', [], ['image_id' => $image_id]); // новый запрос в БД
        return $Image[0];
    }

All this works.
But the following confuses me:
If I need not one person, but let's say 100
In this case, if all of them have pictures set, then there will be 100 new connections for pictures.
How can all this be optimized and save some kind of OOP, if I have it at all :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasyl Fomin, 2017-11-15
@Artem0071

Then you need to pull out all 100 users, and then pull out 100 pictures with one request, specifying an array of id-dov of the pulled users in the request for the pictures.
Or use join on both tables
In two cases, the methods for pulling out can be implemented through OOP, which does not need to be abandoned

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question