Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:42:21+00:00 2026-06-09T14:42:21+00:00

In the CodeIgniter User Guide, the example model had an attribute for each field

  • 0

In the CodeIgniter User Guide, the example model had an attribute for each
field in the corresponding database table:

    class Blogmodel extends CI_Model {

        var $title   = '';
        var $content = '';
        var $date    = '';
        //...

However when it returns a set of blog posts, they are only generic object.

    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

If each blog post is only going to be a generic object, what is the purpose of
the attributes at the beginning of the class?

It seems like it would be nice to have each blog post as a fully fledged model
object populated with it’s content. Then you could write methods that fetched
values internally. However, all the examples in the User Guide seem to show each
controller having one instance of each model and using that object to fetch
generic objects containing the database values. Additionally, the way the
examples instantiate the models ($this->load->model('Blog');) it’s clear that
you are only expected to have one instance of the model within the controller.

If my blog post has a status value which is stored in the
database as an integer, but in several different controllers I want to show that
value as a human readable string, where do I put that function/method? Since I need it
in multiple controllers, and it’s specific to one object, I assume it should be
a method of the model object. If each blog post is an instance of the model, I
could write it like this:

    function get_human_status()
    {
        $lookup = array(
            1 => 'Active',
            2 => 'Hidden',
            3 => 'Pending');
        return $lookup[$this->status];
    }

However, if each blog post is a generic object it would have to look like this:

    function get_human_status($data)
    {
        $lookup = array(
            1 => 'Active',
            2 => 'Hidden',
            3 => 'Pending');
        return $lookup[$data->status];
    }

Which way is the more idiomatic of CodeIgniter? Assuming it’s the second, why does the example model define those internal attributes?

Finally, for bonus points, if I had other questions of this nature, what books,
articles, or examples should I be looking at?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:42:23+00:00Added an answer on June 9, 2026 at 2:42 pm

    Ehh .. the disaster that goes by name “CodeIgniter”.

    In the documentation of that abysmal framework, they portray “model” as instance which implements active record pattern. This is why you get the pointless attributes in the definition of that class. It is just another side-effect of conflict between two responsibilities (storage and logic) withing AR pattern. Also, you might notice the var keyword, which is an artifact of PHP4.

    What you call “models” are not. They are simple domain objects, which in turn are part of model later. Model layer is a mental construct, just like the other layer in MVC design pattern – presentation layer.

    The $this->load->model() method does not initialized “model” instances. It is just another example of CodeIgniters lack of proper autoloader. This method is there just to include the file, which contains the Blog class. Therefor, you cannot conclude that “Blog” i supposed to be a singular object.

    Controllers a not supposed to “show” anything. There are there to change the state of current view instance and the model layer. Views are supposed to deal with presentation logic choose how to display something and which templates to use. Views are not supposed to be dumb templates.

    As for educational materials, you will find a lost of lectures and books at the end of this post. You might also this and this somewhat relevant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to save session in database.in CodeIgniter user guide , make an example
I'm new in CodeIgniter and read user guide from website. Using pagination class, pagination
I'm diving into multiple database usage. According to the codeigniter user guide. To connect
I have a model function that updates a user in my CodeIgniter application: //
Referring to the Codeigniter user guide at http://codeigniter.com/user_guide/helpers/form_helper.html , I cannot seem to figure
After completing the tutorial from the codeigniter user guide I ran into a problem
I am trying to install hero CMS of codeigniter user guide says to go
Here is some code excerpted from Codeigniter's user guide about models. There's no explicit
In the CodeIgniter user guide , I came across this suggestion: In many databases
I'm thinking about using CodeIgniter for a new project. I was reading the user-guide

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.