S
S
Sergey Beloventsev2018-06-21 12:40:12
Yii
Sergey Beloventsev, 2018-06-21 12:40:12

How can I get all related Yii2 models?

Is it possible somehow to get all related models in yii2. Or should they all be enumerated in the $model->relation way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2018-06-21
@kimono

The relationship is "created and stored" in the model when it is first accessed, or it has been explicitly assigned via populateRelation.
Do you need to get exactly "already related models" or "all possible related models"?
If all are "already linked models" then $model->relatedRecords, if "all possible links" then maybe you should consider naming all links like so:

public function getRelatedClient(){}
public function getRelatedUser(){}
public function getRelatedBooks(){}

$model->relatedClient;
$model->relatedUser;
$model->relatedBooks;

Then you can go through all the methods of the class and take those that start with getRelated.
But it's a crutch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question