A
A
Anton Shilov2015-10-14 09:01:18
Yii
Anton Shilov, 2015-10-14 09:01:18

Is this approach normal in organizing data models?

Is this approach acceptable in the organization of data model classes (see code example) or is it the wrong way? It is more convenient for me when the classes accompanying the main data model are located in the same file and access to them is provided precisely through the main class, but is this use case acceptable or categorically not, and what problems can this cause?
User model, User.php file

class User extends ActiveRecord
{
    public static function getUserLinksObj()
    {
        return new UserLinks();
    }
}

class UserLinks extends ActiveRecord
{
    public static function findLinks($userId)
    {
        return static::find()->where(['user_id' => $userId])->all();
    }
}

Usage
$userLinks = User::getUserLinksObj()->findLinks($userId);
// дальнейшая обработка

I would like to hear your opinion on this matter.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2015-10-14
@Siggard

Basically, you have two questions:

Namespaces and classes MUST follow an "autoloading" PSR: [PSR-0, PSR-4].
This means each class is in a file by itself, and is in a namespace of at least one level: a top-level vendor name.
PSR-1
This approach can only cause problems if you start working in a team that has adopted some other standard. With modern IDEs, there is no difference in which file the class is located and therefore there is no reason not to write each class in a separate file - it will be easier to read and look for them.
But in this case, why not write normally right away?
Why such a strong relationship between classes? Each class has its own area of ​​responsibility.

D
dmitriy, 2015-10-14
@dmitriylanets

It seems to me that here the author needs to know whether the approach of obtaining data through another class is correct,
and how to store the class in this file or in another matter of taste.
Aggregate Root .
If a UserLinks object cannot exist without a User object, then User acts as the parent element in the node through which child elements are retrieved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question