Welcome Guest, Not a member yet? Register   Sign In
Related models & controllers
#1

[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
Company Name
Company Registration Number
Credit Limit
Website
Address
Postcode
Country

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
Company ID (this is the foreign key)
First Name
Last Name
Position
Email Address
Telephone
Mobile

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);
$this->contacts_model->delete_contacts_list($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.

#2

[eluser]Scared[/eluser]
Any answer to this? Sad
#3

[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".




Theme © iAndrew 2016 - Forum software by © MyBB