Related models & controllers - 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: Related models & controllers (/showthread.php?tid=60129) |
Related models & controllers - El Forum - 01-21-2014 [eluser]Scared[/eluser] Hello I've started my first project in CI and got myself into a knot again! I've created a model, controller, view and database table for "company" A company consists of the following: Code: Company ID My controller allows you to view a list of companies, edit a company or create a new company. Now I want each company to have a list of "contacts". There would be one or more contact per company. My contacts table is like this: Code: Contact ID This is where I've gotten confused and have numerous questions as follows: 1) Should I have a completely separate model, controller and view for "contacts"? I'm unclear whether they should be separate or whether they should be part of the "company" controller and model, since they are related. 2) My inclination is that the contacts should be a separate model. If this is the case, when you delete a company you would want to also delete all related contacts for that company. Should you take care of the entire deletion process in: Code: $this->company_model->delete_company($company_id) /* Delete from both company & contacts */ Or should you call both models like this: Code: $this->company_model->delete_company($company_id); 3) What would/should my routing look like. Viewing, adding and deleting contacts would only take place via a company view. So if we view the company with ID 123 like this: Code: /company/view/123 How would we view the related contacts for company 123? Like this: Code: /company/view/123/contacts Or like this: Code: /contacts/123 I hope these questions are clear enough and thanks for looking. Related models & controllers - El Forum - 01-24-2014 [eluser]Scared[/eluser] Any answer to this? Related models & controllers - El Forum - 01-24-2014 [eluser]ivantcholakov[/eluser] @Scared I have no much time for a full suggestion. Just a hint: Have you seen this project? - https://github.com/jamierumbelow/codeigniter-base-model . I use a clone of this library, it is very useful for me. The logic there implies for every table you to have a special model. When you create your models, CRUD operations are available for you, you don't need to write them again and again. There is a possibility for cascading deletion to be done. See about so called "observers". |