Meet MY_Model (again) for CodeIgniter

Revisiting MY_Model in CodeIgniter: Copying from Jamie Rumbelow and looking at Eloquent and Establishing relationships between tables inside the MY_Model in CodeIgniter (1): has_one and has_many relations were really long tutorials, but we ended up creating a good MY_Model, so I thought “why not post this on Github?”

And I did. You can find CodeIgniter MY_Model at the following repository: https://github.com/avenirer/CodeIgniter-MY_Model

I sure hope you will use it and report me any issues you encounter. if you feel smart, you can also do some pull request. I’ll be happy to merge them if they look good.

The instructions about using the MY_Model can be found on Github. Also, you can look and the tutorials I’ve mentioned earlier. Be careful though, as the final code can be different than what you’re seeing on the Github repository.

0 thoughts on “Meet MY_Model (again) for CodeIgniter

  • Sorry, still learning about MySQL. I wondered if tables need to be related with foreign keys in case using relation between them. Now I can tell, those don’t have. Ok.
    But here is chance for better™ question:
    table names should be without underscore in it because of ‘has_many_pivot’?
    I really appreciate your code. Thank you again.

    • There are two ways (that I know of…) of correlating data between tables. One way is to have for every row a field that acts as foreign key. And one that involves another table (a third table), that keeps the id of both tables in one row. That third table is called a pivot table, and it’s used for many to many relationships (when one row in a table can have multiple rows in another table). Regarding your “better question”, if you plan do use a pivot table, the conditions of MY_Model are pretty simple: the table name must be created by using the two table names united by an underscore (the names having to be alphabetically ordered) – for example a table that unites the “users” table and the “posts” should be named “posts_users” and not “posts_whatever_users” nor “users_posts” nor “usersPosts” (I hope you get the idea). And to make things simpler for you and the MY_Model, the columns must be named by using the singular form of the table names plus an “_id” string at the end (or, if you wanted to be original in naming the columns in your tables, the identifying columns of those two tables with “_” prepended). So, for example table I just gave you, you should have two columns “program_id” and “user_id”. For a pivot table you don’t even need another column (field) but these two, as the combinations of these two fields can uniquely identify the rows.

  • I’ve already achieved that. To simplify: joining local_user and sallary table over many to many. Or it need to be localUser table name to make it working.

    • Well… you didn’t respect the prerequisites for using MY_Model. The table names should be in plural: so… local_users and sallaries. From that you can create a pivot table: local_users_sallaries.

  • I am starting to bother you. 😀
    So user_sallaries and annual_reports are valid table names for making pivot table called:
    annual_reports_user_sallaries?

    Side note: I decided always to use singular, wich is set in respective model as table name. It is not an issue at all.

    • Sorry about not replying on the site. You can at any time approach me by email for this kind of questions. The answer to your question is yes. Regarding the naming of tables and models, I think is best to name the tables with plural and models with singular. But in the end is just a matter of preference. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

No spam? * Time limit is exhausted. Please reload CAPTCHA.