Skip to content
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

Open
antonmarin opened this issue Jul 27, 2016 · 5 comments
Labels

Comments

@antonmarin
Copy link

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 get frontRelatedModel i have to override getRelatedModel()

Is it right or there is another way?

@samdark
Copy link
Owner

samdark commented Jul 27, 2016

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.

@thiagotalma
Copy link

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()), [...]);
    }
}

@samdark
Copy link
Owner

samdark commented Jul 29, 2016

@thiagotalma technically it's possible but I'd not recommend it.

@antonmarin
Copy link
Author

tnx

@samdark samdark reopened this Aug 1, 2016
@samdark
Copy link
Owner

samdark commented Aug 1, 2016

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
Labels
Projects
None yet
Development

No branches or pull requests

3 participants