📜 ⬆️ ⬇️

There are many entities - code one

As it is known in the work of a programmer, you often need to do something quickly enough, for this you have invented many all kinds of OOP tools, frameworks and a lot of things, but how not to generate monsters, but do everything quickly and efficiently?
Often, being fascinated by design, it turns out that not only would we like it; from project to project, skill is improved and experience grows.
Now I would like to share my experience.

Consider a commonplace example.

Posts - Comments and users who generate all this.
Everything has a rating, it will be our problem.

we could do something like this:

What is not an option? For each table has its own table with a rating in terms of databases, all right.
But you hardly want to describe the class for each table with a rating, especially considering that this code will be repeated from time to time, and if you have a lot more entities that need rating? As a result, everything will breed to an incredible scale.
')
Therefore, I propose a more flexible option:


We now have a table with a rating, and an additional field has been added to it where it is indicated what this rating is from.
Is it really that simple?

The class for setting the rating will be very simple (solutions for the Zend Framework):

class Rating extends Zend_Db_Table_Abstract
{
public function setRating($item_id, $user_id, $table)
{
/*
* ,
* $table - item_type,
*/
}

public function getRating($item_id, $table)
{
/*
* ,
* mysql count
*/
}

public function isVoted($item_id, $user_id, $table)
{
//
}
}


* This source code was highlighted with Source Code Highlighter .


Now you can add a rating to any entity in your project, but an example is an example, and if you digress a little, then according to this scheme you can organize a lot of things and quite quickly and simply for example: tags, comments on something or not, and in general it is not enough head in the head.
If you add to this some helpers of the form that will help you to display the same thing several times, the task is made easier several more times.

And this is only a small fraction of how to facilitate your work, I would like to hear more different solutions for various problems.

PS: sorry for the disgusting pictures.

Source: https://habr.com/ru/post/67117/


All Articles