Welcome Guest, Not a member yet? Register   Sign In
Question regarding models
#1

[eluser]Comanche[/eluser]
Hi there,

I just started using CI and I have a question regarding the models.
I was given the task to develop an interface which can be used to manage customers, their domains, virtual hosts, mail accounts etc..
I already programmed something which does everything what's needed, but I did it without using MVC, classes or something like. With other words, it was a real mess Wink After reaching the point where my own code just confused me I decided to start all over again and to do it using a MVC-Framework (CI).

I have the following database structure:

1. Table 'customers' (holds information about a customer, his ID etc.)
2. Table 'domains' (holds information about a domain, it's name- and mailserver and the customer ID it belongs to)
3. Table 'vhost' (holds information about the host running the domain)

There are much more tables but I just need these to describe my "problem".

When calling the controller "domain.php" (with function 'index') it reads all information about the domain with the corresponding ID from the database. This function is done by "Domain_model.php" which holds the class "Domain_model".

But I also need to know, who's the customer the domain belongs to and on which host its running on, so I need to get this information.

Now there are two possible approches:

1. I tell the controller to get all domain information, then use the delivered customer- und domain-id to determine the customer and the host via "Customer_model" and "Vhost_model". Something like:

Code:
<?php
class Domain extends Controller {
    function __construct()
    {
        parent::Controller();
    }

    function index($id='')
    {
        $this->load->model('Domain_model');
        $data['domain'] = $this->Domain_model->index($id);
        
        $this->load->model('Customer_model');
        $data['customer'] = $this->Customer_model->getName($data['domain']['cid']);

        $this->load->model('Vhost_model');
        $data['host'] = $this->Vhost_model->getHost($id);
        ...
    }
}
?>

2. I do the whole thing within "Domain_model" by doing direct queries on "customers" and "vhost"-table.
Code:
<?php
class Domain_model extends Model
{
    function Domain_model()
    {
        parent::Model();
    }
    function index($id='')
    {
        $this->load->database();
        $query = $this->db->query('SELECT * FROM domains WHERE id='.$id);
        $domain = $query->result_array();

        $query = $this->db->query('SELECT name FROM customers WHERE id='.$domain[0]['cid']);
        $customer = $query->result_array();

        $query = $this->db->query('SELECT host FROM vhosts WHERE id='.$id);
        $vhost = $query->result_array();
        ...
    }
?>

Well, in my opinion I should use the first variant, but since I'm new to the whole thing I'd like to ask before doing some strange things Smile

P.S.: Sorry for the english, I don't talk and/or write it that often.
#2

[eluser]jcopling[/eluser]
Well, to be honest, both of these look like perfectly valid approaches. The question that you need to ask is, will you ever be requesting these pieces of information for more than 1 reason. If the answer is yes, then it is probably appropriate to split the 3 query functions into 3 different models. If the Domain index is the only place that you want to get this information then putting them all under 1 model would be perfectly fine.

As an alternative, it looks like you are simply requesting the name of the customer and the host of the vhost based on the domain id, is there any reason you couldn't accomplish this with 1 query such as:

Code:
$query = $this->db->query("SELECT domains.*, customers.name, vhosts.name FROM domains INNER JOIN customers ON customers.id = domains.id INNER JOIN vhosts ON vhosts.id = domains.id WHERE domain.id=".$id);

Hope this helps a little.
#3

[eluser]Comanche[/eluser]
Hi,

thanks for your reply.
Your alternative would also meet my requirements, the reason why I didn't use it is that I am not that experienced with MySQL and often got stuck when trying to put different queries in one single query.

I think I will use your approach but at the same time will keep the functions to query Hosts, Customers etc. since I need them on some other pages independetly from the domain index.

Thank you very muich for your helb.




Theme © iAndrew 2016 - Forum software by © MyBB