My models all tend to look the same

I noticed that all my models are very similar. Most of them tend to follow a pattern where they are collections of methods containing active recording code, which are only small variations on each other. Here is an example:

class Site extends CI_Model {

    public function get_site_by_id($id)
    {
        // Active record code to get site by id
    }

    public function get_sites_by_user_id($user_id)
    {
        // ...
    }

    // ...

    public function get_site_by_user_id_and_url_string($user_id, $url_string)
    {
        // ...
    }

    // Non active record methods and business logic
    // ...

}

This approach worked fine for me, but I wonder if there is a more elegant solution. It just doesn't seem right to me that I have to create a new method every time I need to search for data in a new way. Is this a common practice, or am I missing a way to reorganize this?

+5
source share
5 answers

, (CI_Model) (), -

class MyCommonMethodsClass extends CI_Model {
}

(Site), . - "". , crud, .

, "", . , , . "" . , . , , ( ), - db. ?

+2

, , , - Create, Retrieve, Update, Delete (CRUD), SQL, GetID, UpdateByID, GetById ..

CRUD , . GetId , .

, CRUD . , , , , , . , , .

, .

+1

. , , , , , .

0

, , , - ( " " ). , , - . , , , , , , .

:

  • (SPOT, DRY)
  • , , , ()
0

, - . , . CRUD ?

, , .

, , question, .

.

0

All Articles