views:

51

answers:

1

I am trying to simplify the process of inserting data into my database, so I have constructed a model that handles the variable extraction and data inserting.

I understand that this model embodies a function, and then you call this function (such as /controller/model) and pass the relevant data through the model for processing. However, I am unsure how to incorporate the model, whereas how to call it, or what needs to be written so I can call the function out. I am using CodeIgniter.

Here's the said model:

class Createproject extends ActiveRecord {

function __insert() // This function will handle inserting the relevant project information into the database.
    {

        $this->project_name = $this->input->get_post('project_name');

        // ... skipping ~30 variable definitions ...

        $this->db->insert('project_details', $this);
    }

So I'm confused from here; where do you place this model for processing, and how would you use it within controllers or the rest of the app?

Thanks!

+1  A: 

You can just import (require_once) it into your controller (save it into models.php), and instantiate it in your controller:

$cp = new Createproject()
$cp->__insert()
orokusaki
Is there a reason you didn't declare the variable? As opposed to $cp, for example.
dmanexe
To add on to that, I generally create a single file that contains all of my includes, the MVC classes and such, then include that file globally. That way you have all your includes in one place and it's quite a bit more manageable.
Jeremy Morgan
@danbirlem Oops, I'm a Python programmer so I always forget stuff like that in other languages. I fixed it now. Semantic errors like that can really confuse a new programmer who stumbles by.
orokusaki
@Jeremy Morgan You should try Python. You don't have just names floating around all over the place (ie, set a variable up high and it's just riding through your code wreaking havoc here and there). You instead have namespaces. Put 'import somefile' and it imports that file as a variable called 'somefile' in the file where you call 'import'. Then, let's say 'somefile' has a variable called 'name'. In the file where you called 'import somefile', you can now say 'somefile.name', and it contains the value of the other file's name variable (that's the best way I think to describe it to a PHP dev).
orokusaki
Thanks for the suggestion, I've never really delved into python 100%, only used it to solve some one-off problem and moved on. I was very impressed with it, and I like the namespace methodology quite a bit. I appreciate your comment, and can see how that type of development would be quite useful. I might have to go back and check out python a little more.
Jeremy Morgan
@Jeremy Morgan I'm glad. I think you'll enjoy it.
orokusaki