Welcome Guest, Not a member yet? Register   Sign In
need some tips on reusing code/models
#1

[eluser]Yednotek[/eluser]
Hi guys,

I'm currently redesigning the site www.muzikantenbank.net from scratch and have something bothering me that I am not sure how to figure out.

The current core functionality is actually quite limited, users can place and search ads of 1 kind. That's it. In the new version users will be able to add many different types of content (aka database objects). These objects will have a lot of similar fields but will also have a few distinctive fields. These objects will all have to be searchable and browseable in a similar fashion.

I was wondering if anyone has tips on what would be a good approach to structure the models/database objects and code in such a way that I will have the highest flexibility and the least amount of code repetition.

For example should I create a single shared database table with all columns combined for all these different objects and have a 'objectType' column? Or should I create all separate tables? If I used a single table for example I might be able to reuse the search function, pagination, editing forms etc.

I'm kind of daunted by the number of models, forms, form fields and functions that this application will have if I don't take a smart approach here.

Any gurus out there that can shed some light on this or have tips or pointers to good articles on this? I'm pretty sure I'm not the first person to run into this problem but not sure where to start looking for answers.

Thanks!

#2

[eluser]Yednotek[/eluser]
Ok so I have decided to go with formgenlib to reduce the amount of code for forms+validation and datamapper ORM. This should help in making the code more modular and reusable.. It's quite a new approach for me as I started the site in CI about 5 years ago and quite a lot has changed. I'll see where it goes!
#3

[eluser]Davcon[/eluser]
When you look at the logic, you'll find that there's only a small handful of things that you would reasonably want to do with models. They are:

1. Insert stuff

2. Select a row where something is something

3. Select some rows where something is something

4. Update a row or rows where something is something

5. Delete a single row or multiple rows

6. (very occasionally) a custom query with something like a table join going on



With this in mind the strategy that I have came up with is to create one model with all of those functions and with a function at the top called get_table().

So, all I have to do is change the name of the table, save the file with a new name and voila!

My advice therefore is to create a perfect model with everything you need and then forget about models. It's easy!


#4

[eluser]Yednotek[/eluser]
Thanks Davcon. I think I get your point and I've used a similar method. However I'm talking about more than just database transactions here. I've made a list of the common end-user functionality for each object and it more or less boils down to this:

item view
search items (searchform+validation, result pages with pagination)
browse items (using "drill-down navigation" or filters)
insert new items (form+validation)
update existing items (form+validation)

For all the above I'll need to build models, views and controllers. I'm looking for a smart way to organize my code and to reuse code where possible. Would it be beneficial to apply the HMVC pattern here?




#5

[eluser]Davcon[/eluser]
I think that the HMVC addon for Codeigniter is brilliant. In my opinion it should be added to the core (main) version of Codeigniter as a priority. If that means paying the guy who wrote the code (he's in our midst on this forum) then so be it. I'd pay.
#6

[eluser]Davcon[/eluser]
For what it's worth, here's the code I use for my models. All I have to do is change the table name and it seems to work.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mdl_modeltest extends CI_Model {



function __construct() {      
    parent::__construct();    
}


function get_table() {
    $table = "tablename";
    return $table;
}


function get($order_by){
            $table = $this->get_table();
            $this->db->order_by($order_by);
            $query=$this->db->get($table);
            return $query;
}


function get_with_limit($limit, $offset, $order_by) {
            $table = $this->get_table();
            $this->db->limit($limit, $offset);
            $this->db->order_by($order_by);
            $query=$this->db->get($table);
            return $query;    
}


function get_where($id){
        $table = $this->get_table();
$this->db->where('id', $id);
$query=$this->db->get($table);
return $query;
}

function get_where_custom($col, $value) {
    $table = $this->get_table();
    $this->db->where($col, $value);
    $query=$this->db->get($table);
    return $query;
}

function _insert($data){
    $table = $this->get_table();
    $this->db->insert($table, $data);
}


function _update($id, $data){
        $table = $this->get_table();
$this->db->where('id', $id);
$this->db->update($table, $data);
}


//deletearow
function _delete($id){
        $table = $this->get_table();
$this->db->where('id', $id);
$this->db->delete($table);
}

function count_where($column, $value) {
        $table = $this->get_table();
        $this->db->where($column, $value);
$query=$this->db->get($table);
        $num_rows = $query->num_rows();
return $num_rows;
}


function count_all() {
        $table = $this->get_table();
$query=$this->db->get($table);
        $num_rows = $query->num_rows();
return $num_rows;
}




function get_max() {
        $table = $this->get_table();
        $this->db->select_max('id');
        $query = $this->db->get($table);
        $row=$query->row();
        $id=$row->id;
        return $id;
}


}
#7

[eluser]Damon[/eluser]
The site you have mentioned is not working on my pc. I don't know if there is any problem in the site or its my pc. I want to get rid of this situation as early as possible.
_________________
portable countertop dishwashers




Theme © iAndrew 2016 - Forum software by © MyBB