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.

PHP Code:
$innerQuery Post::find(
The above query would be used to select the most recent creation date for a post of each thread.

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.

PHP Code:
$dynamicModel = new ActiveRecordDynamicModel($innerQuery); 
Now, I would like to be able to use this as a subquery. However, the first major problem is how to reference data from this model.

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:

PHP Code:
echo '<pre>',print_r($innerTable->getFields()),'</pre>'
HTML Code:
    [0] => id
    [1] => user_id
    [2] => thread_id
    [3] => message
    [4] => status
    [5] => created
    [6] => recent_post
This would be an example of the select statement associated with the dynamic model in this instance:

Code SQL:
     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 
      posts AS t0