I'm not very familiar with reflection but, I'll take a look.
The bottom level class ArticlesModel is created via matching a a controller name. If a controller articles is passed than the the model ArticlesModel is created. This is fine if the class was a direct subclass of just AppModel becasue AppModel is brought in before initiation takes place. However, the PostsModel class belongs to a conceptual group of classes that adds additional functionality to a model. So these classes need to be brought in to be able to create the actual model object. In this case that model is ArticlesModel. However, I can't actually bring in file to figure out which class it extends without an error. My current solution involves just bringing in all the "dynamic model" classes. However, if there were a large number this would not be the optimal solution. I only want to bring in ones that are needed not all of them. A dynamic model which PostsModel relates conceptually to is a table in a database with a column called type. The type allows the ability to use the the same table for different sets of data that have a common interface. For example, all posts have common columns such as created,updated,title,content,etc. Therefore, via extending that class with another the subclass gains the interface but the type becomes the sub classes name. So essentially, if a user would like to add a news section they can extend the Posts interface. The interface is inhereited but since the type changes all columns with type = news are essentially a "different" table although the data is being stored in one posts. If that makes sense…
Code:
abstract class PostsModel extends AppModel {
var $has = array(
'cols'=> array(
array('col'=>'id'),
array('col'=>'title'),
array('col'=>'subtitle'),
array('col'=>'message'),
array('col'=>'update'),
array('col'=>'created')
),
'where'=>array()
);
}
Code:
class ArticlesModel extends PostsModel {
}
Code:
class NewsModel extends PostsModel {
}
Dynamically generated sql query from AppModel
query: SELECT posts.`id`,posts.`title`,posts.`subtitle`,posts.`message`,posts.`update`,posts.`created` FROM posts WHERE posts.`type`='articles';
query: SELECT posts.`id`,posts.`title`,posts.`subtitle`,posts.`message`,posts.`update`,posts.`created` FROM posts WHERE posts.`type`='news';
The concept is essentially an interface. These two sets of data have the same columns(interface) so they share the same table in the database. Which makes it easy to add a new "data set" such a blogs, etc if it has the same column data as the posts table.
Bookmarks