-
-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Are there some best practices of handling relations in advanced template? #135
Labels
Comments
The best thing to do in this case is not to mix these models. It's often called bounded contexts. The idea is that contexts are different so it's best to avoid reusing model even if the name is the same and functionality is close. |
class MainModel extends ActiveRecord
{
protected function getRelationName($class)
{
$myClass = get_called_class();
$myNS = substr($myClass, 0, strrpos($myClass, '\\'));
return $myNS . substr($class, strrpos($class, '\\'));
}
} class commonModel extends MainModel
{
public function getRelatedModel()
{
return $this->hasOne(static::getRelationName(RelatedModel::className()), [...]);
}
} |
@thiagotalma technically it's possible but I'd not recommend it. |
tnx |
I'll leave this open since it could be made into recipe. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are models:
common\models\commonModel
common\models\commonRelatedModel
frontend\models\frontModel
frontModel extends commonModel
commonModel has some relation:
commonModel->getRelatedModel()
so to get commonRelatedModel from frontModel i just ask
frontModel()->getRelatedModel()
but if i add
frontend\models\frontRelatedModel
, then to getfrontRelatedModel
i have to overridegetRelatedModel()
Is it right or there is another way?
The text was updated successfully, but these errors were encountered: