Results 1 to 1 of 1
Thread: Handling Derived Models
May 5, 2009, 19:40 #1
- Join Date
- Jul 2006
- Augusta, Georgia, United States
- 17 Post(s)
- 5 Thread(s)
Handling Derived Models
How would you reference model entity data that was derived as a dynamic table from a subquery?
Normally, associated models are referenced via their model name:
That is common is a user has one profile. Simple enough.
However, what if a association model could be created at run time and the model wasn't based on a predefined schema in a class.
This is the scenario I'm working with. Its a basic posts table and all that is happening here is the object to create the select statement is being returned.
$innerQuery = Post::find(
Now, rather then running this query the ActiveRecord::findSelect constant returns the object that represents the query.
This makes it possible to create a "dynamic" model which is essentially a derived table represented as a domain level object.
$dynamicModel = new ActiveRecordDynamicModel($innerQuery);
As discussed above normally the model name would be used for this.
However, a dynamic model doesn't have a model class name. Any dynamic model is a instance of ActiveRecordDynamicModel.
Therefore, if included as a subquery how would one reference the data through a property if this dynamic model would be used as a subquery considering that it is joined with threads.
I understand that this entire idea skews the idea of a "model" but I don't think there is any other way in which to do so that would support the idea of a subquery. However, a select statement in theory will always return a table and table can always be translated into a model. So ideally a subquery and a model should be interchangeable.
This would be a example of the fields that would associated with this dynamic model:
Array (  => id  => user_id  => thread_id  => message  => status  => created  => recent_post )
SELECT t0.`id` AS t0_id ,t0.`user_id` AS t0_user_id ,t0.`thread_id` AS t0_thread_id ,t0.`message` AS t0_message ,t0.`status` AS t0_status ,t0.`created` AS t0_created ,MAX(t0.created) AS t0_recent_post FROM posts AS t0 GROUP BY t0.thread_id