Modelling non-SQL data sources

I’m relatively new to Yii, and am still learning where everything belongs in the whole MVC structure.

Yii Models seem to be heavily based the Active Record concept, which is heavily based around retrieving data from an SQL database. I’m trying to understand from a philosophical viewpoint how to structure the MVC architecture when an application needs to display something that doesn’t come from a database such as directory/folder information from a file-system.

In this case, each file in a folder will have attributes such as "name", "size", etc associated with it. If this same information was stored in an SQL database clearly the Active Record provides a nice level of abstraction for accessing these properties, but when this information is sourced from file-system access functions, is there a philosophically correct way that this should be abstracted within a Yii Model?

I’m really trying to get my head around this concept, because an extension of this same question is: how should data be abstracted when it comes from table-like XML files rather than a MYSQL table.


&lt;row&gt;  &lt;name&gt;abc&lt;/name&gt; &lt;size&gt;123&lt;/size&gt;  &lt;/row&gt;

&lt;row&gt;  &lt;name&gt;def&lt;/name&gt; &lt;size&gt;456&lt;/size&gt;  &lt;/row&gt;


Any suggestions?



From a theoretical point of view, you are clearly in the model domain. But within Yii i wouldn’t subclass CModel for this, as you will hardly ever do something like $directory->validate(). Yii’s CModel mainly implements the methods required for validating and labelling model attributes.

So you could think about an interface that matches your requirements. Here i usually think about, how i’d like to use such a model before i implement it. My first try would e.g. be:

$dir=new MyDirectory('/path/to/dir');

foreach ($dir as $file) 

  echo $file->name.' '.$file->size.' '.$file->ctime;

Now if you check the PHP manual, you’ll find that PHP already has a class Directory. So you could build a wrapper around that for easier access. Implementing the above should be straightforward.

Or you look around for other OO implementations of a “Directory” and learn from them. I’m sure, there already is something better then my first approach out there ;)