CodeIgniter Forums
Models in Model, Composite Model,design problem, how to do? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Models in Model, Composite Model,design problem, how to do? (/showthread.php?tid=2189)



Models in Model, Composite Model,design problem, how to do? - El Forum - 07-21-2007

[eluser]Imkow@CN[/eluser]
As my project is getting big and messy, each single model can't accomplish the job. A complex model seems needed.
For example: my Article model might need to contain Tag model (handling tags), File model(handling uploaded files) and Categroy Model(handling categroy infomation). Together, they form an article object handling post,view,updates of each article.

But in CI, how do i write the model object that makes use of many sub-objects?($this->load->model is way not capable enough) or CI has some special way to deal in such scenario?


Models in Model, Composite Model,design problem, how to do? - El Forum - 07-21-2007

[eluser]Imkow@CN[/eluser]
rephrase the question:
1 Car has 4 Wheels, 1 Engine, and 6 windows. How to present the car with CI model?


Models in Model, Composite Model,design problem, how to do? - El Forum - 07-21-2007

[eluser]esra[/eluser]
[quote author="Imkow@CN" date="1185033916"]As my project is getting big and messy, each single model can't accomplish the job. A complex model seems needed.
For example: my Article model might need to contain Tag model (handling tags), File model(handling uploaded files) and Categroy Model(handling categroy infomation). Together, they form an article object handling post,view,updates of each article.

But in CI, how do i write the model object that makes use of many sub-objects?($this->load->model is way not capable enough) or CI has some special way to deal in such scenario?[/quote]

As well as a complex database schema capable of supporting multiple hierarchies that have relationships with other hierarchies. I'm working on something similar but have not got past the database schema design yet, although we have components (CI modules) operating independently with their own hierarchial models. One solution is to use nested sets (see Thunder's Nested Sets article and sample code on the Wiki). Any number of sibling nested set models could inheirit from his parent nested sets model (which could be a library if you are using PHP5).

You need to think in terms of something like a product's part breakdown. A car is a main assembly composed of many subassemblies, which could have subassemblies of their own. As the tree of hierarchies grows, you finally are reduced to the parts that make up all the various subassemblies. The parts breakdown forms a main hierarchy with sibling hierarchies for the subassemblies.

It gets more complex if your attempting to create hierarchial relationships for objects other than content items and this is the dilemma, I am faced with. I also have the problems of contending with the same content stored in multiple languages, as well of multiple revisions of the same content. That is, Content is only one entity requiring heirarchies in my solution. I'm also working on a CI installer component that handles the installation of components, modules, plugins, languages, templates, helpers, libraries, etc. as well as their dependencies upon one another. The project is very complex and is basically a replacement for a CMS that we can no longer use due to licensing restrictions.

If you are using MySQL, you might consider downloading a copy of FabForce DBDesigner. It's a GPL UML-like database design tool with versions for Windows and Linux. You can use it to design complex database schemas and create your constraints between tables. It's similar to Rational Rose. It can import your existing schema, you can make changes and then sychronize those changes with your database. If you do that, we might be able to exchange UML diagram files. I need a few days to cleanup my current schema but can send you something.

In my particular case, I store content generically in a single table. Articles, Blogs, Products, Help and other flavors of content are mapped (not stored) in separate tables using nested set hierarchies. Each of these tables has a content-flavor-specific id (blog id, article id, help id, etc.), as well as a content_id foreign key.

In your case, you could probably store content as I do and store your tag and file data generically in separate tables related to the content table using foreign keys. You could probably store tags and filenames in separate fields of your content tables as a comma delimited list and convert to arrays in your code. This should allow you to reuse the same tags and files in multiple articles. I'm sure that there are probably other approaches to handling that sort of thing. Another approach might be to use mapping tables to map something like an image id to a content id in order to create image lists which could be associated with a content id via foreign key constraints.

The trick to relating all of the hierarchies together is to create a entities or registry table that assigns a entity_id or registry_id to every item created for your application. My schema is currently headed in that direction. Creating the models is probably going to be much less of a problem after the schema is finished.


Models in Model, Composite Model,design problem, how to do? - El Forum - 07-22-2007

[eluser]Imkow@CN[/eluser]
Thank you for the reply very so much. Your reply was read for at least 3 times. There are something really inspiring in it. The idea of "assembly" process and "only content needs hierarchy" really provide me with a new way of thinking. Also you brought people like me out of stone age by informing of the database design tool. My problem is instantly solved upon your brilliant idea.
For a further discussion based on your opinion, are you suggesting that there is the fact CI models cannot form nestingly with each other? My problem isn't so much on datebase design. It's to organise code with CI customs that pains me. For example, it's very hard to present the following code in CI without helps of the third-party nesting tool, is it?
Code:
//in proto-PHP5 code
class Car extend Model
{
   Wheel $wheels[4];
   Window $windows[6];
   Engine $engine;
}
If true, I should write code in compromise like the following, should I?
Code:
//in proto-PHP5 code
class Car_Control extend Controller
{
function index()
{
  $this->load->model("car");
  $this->load->model("wheel");
  $this->load->model("window");

  $wheels[]=$this->wheel->getData();
  $wheels[]=$this->wheel->getData();
  $wheels[]=$this->wheel->getData();
  $wheels[]=$this->wheel->getData();
  $car=$this->car->getData();
  $car["wheels"]=&$wheels;
  //................
  //............
  //........

}

}
Thanks again for the help.