H
H
HaruAtari2014-01-17 11:55:54
Yii
HaruAtari, 2014-01-17 11:55:54

How to reduce model size?

Good afternoon.
I have a site on Yii, it has a User model. The class is used very actively and itself is constantly changing.
Gradually it grew and now occupies more than a thousand lines. It becomes inconvenient to navigate in the nest. I would like to somehow refactor it to separate the methods by different entities. But the problem is that there are no methods in the class that could be taken out into a separate entity. All of them provide the work of one entity and use private fields. Just the essence has a lot of functions.
At first I wanted to make an intermediate class between User and its ancestor and put getters and setters into it. But somehow it's not right. Here I sit and think, can some methods be brought into behavior?
Share how you fight in bulk classes?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
egor_nullptr, 2014-01-17
@HaruAtari

If it is not possible to separate some of the methods into a separate class, then divide by traits. Some may think that this approach is "not kosher", but it copes well with the problem of code organization.

N
Nokyta, 2014-01-17
@Nokyta

If the model has methods that are used in forms (custom validations when creating / editing a user), then it makes sense to put them in UserForm inherited from CFormModel and will be used for forms.
Behaviors usually include methods that use two or more models, i.e. methods for one model there to take out it is senseless.

A
Arman, 2014-01-17
@Arik

It's too early for traits, especially since yii 1 was written under 5.2 and it's not known what php you have.
I don't really know yii, but:
You can make a small helper class and put the guts of your methods there. Or several such helpers, if the code can be divided/grouped into different classes. Then the User class will be just an adapter (a router, so to speak).

private $_helper;

    private function _getHelper()
        {
        if( !($this->_helper instanceof Helper_User) )
            {
            $this->_helper = new Helper_User();
            }

        return $this->_helper;
        }

    public function getData()
        {
        return $this->_getHelper()->getData();
        }

    public function setData($data)
        {
        return $this->_getHelper()->setData($data);
        }

All private methods can go directly to this helper class. The number of public methods will remain the same, but will not take up as much space.

A
Alexander A, 2014-01-19
@lordsc

Instead of traits in the old yii, it was supposed to use behavior
, it is at the same time a collection of events
+
extends model methods (i.e. public behavior methods are available from the model by the same name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question