I am rewriting an older ORM library that has not been maintained for several years. I really liked it and wanted to really dig in to see how the logic of these systems worked.
I am running more into an issue of best practice here. The orm class itself has a factory method that calls the model class based off of the supplied name, so for example:
$article = Orm::factory('article');
Would load the article.php class (which by the way extends the Orm class). The relationships are also set in arrays, which refer back to models (which also reflects the table name). So my article might have one user (in the article.php model):
$has_one = array('users');
When related objects are loaded, it calls Orm::factory('users') to load that particular model as a related object. So far, whatever, that's fine for the time being.
My real concern comes into play with name spaces. The orm class (and database class) will exist in their own name space, maybe Database\ORM, where my models may exist wherever else. What would be the best method to load these objects? The orm doesn't currently know the related or other models namespace. I could set a namespace property in the class, but that seems hacky. I've also considered the possiblity that my relations will have to include the namespace, such as:
$has_one = array('What\Ever\ArticleModel');
I've also considered passing the namespace on instantiation:
$article = Orm::factory('article', 'The\Name\Space');
or
$article = new Article_Model('The\Name\Space');
What I do not like about this, is that instantiating a model, I can pass an identifier to load data.
$article = new Article_Model(156);
Would load the article with an id of 156. I could scrap that, however.
So, what would be the more ideal solution here? Pass the namespace to each and every model, set a namespace property once in my Orm class (could even create a method to set it), or use the factory method and assign any relations with a namespace included?
Aucun commentaire:
Enregistrer un commentaire