P
P
Pavel Gogolinsky2015-05-20 18:23:40
Yii
Pavel Gogolinsky, 2015-05-20 18:23:40

What is wrong with creating classes?

There is a website that publishes news. News can be created by both the administrator and registered visitors of the site. News is stored in the database in a single news table .

id: integer
type: integer
model_id: integer DEFAULT NULL
image: varchar(255)
content: blob

type — type of news (1 - news from the administrator, 2 — news from the site visitor).
model_id - ID of the user who wrote the news.
At the current stage, the main difference between the administrator's news and the user's news is different folders for storing pictures. Pictures for the administrator's news are stored in the path /images/news/site/ , pictures from the user are stored in the path /images/news/user/user_id .
I created a News class, which is the MVC model associated with the database. Responsible for the main parameters (id, content);
Next, I created the NewsAdmin extends News and NewsUser extends News classes. They are responsible for saving pictures to the correct folder. To do this, they implement the getPathToImages($image);
The problem is this.
It is necessary to create a "News" page on the site. The page should have switches: "All news", "Administrator's news", "News from visitors".
Getting a list of admin news is easy: NewsAdmin::find()->all(); It's easy to get the path to the folder with pictures $model->getPathToImages($model->image);
The same goes for user news.
But there is a problem with general news. Even if they are retrieved from the database using News::find()->all(), it is no longer possible to get the path to the folder, since the getPathToImages() method is not implemented in the News class;
Where is the mistake? I created the table incorrectly and it was worth making two tables? Or did you implement the methods incorrectly? How to make beautiful?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
restyler, 2015-05-20
@gogolinsky

If we discuss "architecturally correct" ways to resolve such a situation in OOP and in Yii2 in particular - I can say that your case falls under the case of single table inheritance - here is an excellent example from the manual how to prepare it.
As a result, when iterating over the News::find()->all() array, you will not have News instances there, but NewsAdmin and NewsUser. Is the above pattern overengineering in your situation? It's up to you to decide :) perhaps it's easier to really not worry and hardcode everything in the base class.

S
Sergey, 2015-05-20
@serega_kaktus

You can put the type->path connection into the config, then you won't need the NewsUser and NewsAdmin classes. And the getPathToImages() method will return the path depending on the type.
You can put the type->className connection into the config, then having received the class name from the config in the code, you can create an object $news = new $className($param);
The first option is preferable if there is no other reason to use inheritance.

M
Maxim Timofeev, 2015-05-20
@webinar

2 tables for this task - it's really strange
Create a common one in getPathToImages(); pass parameters:
getPathToImages($image, $user)
and pass via $user everything, admin or user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question