Hello all -
This is a bit long but I think it can help people who are struggling using yii to build complex applications using DAO & AR.
I started using yii to develop a complex web application a few months ago. My web application is broken up into integrated sub-applications. My plan is to use AR to implement some of the rather simple sub-appls such as the administrative and use DAO to implement the complex sub-apps. Most of my work to date has been on one of the complex sub-apps. This sub-app is composed of several model classes that extend CModel, where each class uses CModel validation and has functions I wrote to perform load and save operations using DAO.
Most of my web pages in the complex sub-app present the user with hierarchical data pulled from the database that could span as many as 5 hierarchy levels and contain a total of 100 or so items in the hierarchy.
For example:
(hierarchy top level to bottom level): category <- group <- item type 1 <- group <- item type 2
The categories and groups levels are solely used to organize the items on the page.
The user has the ability to select / deselect type 1 items and type 2 items and their state is posted back upon clicking save.
My design processes this posted tabular input in the controller. The controller iterates over all of the type 1 posted data, creating an instance of a CModel type 1 class for each. For each type 1 instance, I then iterate over all of its posted type 2 children, creating an instance of a CModel type 2 class for each and adding the type 2 model instance to an array property in its parent type 1 instance.
This allows me to load the posted data in model instances while also capturing their hierarchical relationship.
I validate all of the type 1 and type 2 models using CModel validation and if all are valid, I call the model’s custom save function that iterates over the models, executing a DAO update operation on each.
I have a few questions regarding the design for complex hierarchical tabular input:
- Does the DAO appear to be the proper tool for this job as opposed to AR given the hierarchical nature and considering the fact that the data pulled from the database to display the hierarchy spans anywhere from 6 to 10 tables?
(my database is currently normalized - we may denormalize in the future once we are ready to measure performance).
-
If DAO is the appropriate tool, is it best practice to load the data into models when the data is retrieved from the database for presentation to the user, and when the data is posted by the user? I believe it is the right tool when the data is posted back since the data needs to be validated etc via the model, but when the data is retrieved from the database for display I am not so sure. In that case, the data retrieved from the db would be in an array and the array could simply be passed to the view for display rather than loading it into models first. I know it is a little more expensive to load it into models first, however, you do gain the advantage of dealing with the data in object-oriented manner within the view.
-
Regardless of whether or not DAO is the right tool, if I were to use AR for such a task, how would I go about doing this?
As I mentioned above, the data pulled together to form the hierarchy on the page spans anywhere from 6-10 tables. I know you can create relations inside an AR model that will allow you to retrieve model instances of related tables but can you capture a 5 level hierarchy where each level is represented by a table and each relationship is one-to-many and be able to easily iterate over the hierarchical data to display, manipulate, etc?
I am having a hard time seeing how such data can be handled using AR…
- I have recently given consideration to a hybrid method but am not sure how to connect the dots. In some of the threads I’ve read on this forum regarding DAO and AR, I saw a few suggestions indicating that it may be advantageous to combine the two methods where DAO is used for complex data retrieval and AR is used to perform updates, inserts, and deletes. If I were to use a hybrid method though, I still don’t see how to load all the posted hierarchical tabular data into the AR models. Can you capture all the data in a 5 level hierarchy within the AR models?
Hopefully this discussion can help others who are working on complex applications and are not sure how to use DAO / AR for complex relations and tabular input. Thanks in advance for your insight. It is much appreciated.