5

Not sure the best way of phrasing this so bear with me.

Within Codeigniter I can return a record set of my object no problem but this is returned as a stdClass object not as an "model" object (for example a Page Object) which I can then use to make use of other methods within that model.

Am I missing a trick here? Or is this the standard functionality within CI?

3 Answers 3

8

Yes, basically in order for this to work you need to declare your Model objects properties class-wide, and refer to $this being the current model object.

class Blogmodel extends CI_Model {

    var $title   = '';
    var $content = '';   // Declare Class wide Model properties
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function get_entry()
    {
        $query = $this->db->query('query to get single object');
        $db_row = $query->row();            //Get single record

        $this->title   = $db_row->title;
        $this->content = $db_row->content;  //Populate current instance of the Model
        $this->date    = $db_row->date;

        return $this;                       //Return the Model instance
    }
}

I believe get_entry() will return an object type Blogmodel.

Sign up to request clarification or add additional context in comments.

2 Comments

Genuine question here, why not just do this: function get_entry($id) { return $this->db->where('id', $id)->get('blog_table')->row(0, "Blogmodel"); }?
with CI, mkoistinen's way is the right way to do. jondavidjohn's way is working though.
0

You don't need sth like:

$this->title   = $db_row->title;
        $this->content = $db_row->content;  //Populate current instance of the Model
        $this->date    = $db_row->date;

Just put to the result() method ur model:

result(get_class($this));

or

result(get_called_class());

And you'll get instance of ur model!

Comments

0

My solution to this problem consisted of a combination of jondavidjohn's answer and mkoistinen's comment.

According to the CodeIgniter documentation:

You can also pass a string to result() which represents a class to instantiate for each result object (note: this class must be loaded)

Armed with that knowledge, we can rewrite jondavidjohn's solution in this way:

class Blogmodel extends CI_Model {

    var $title   = '';
    var $content = '';   // Declare Class wide Model properties
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function get_entry()
    {
        $query = $this->db->query('query to get single object');
        $blogModel = $query->row('Blogmodel'); //Get single record

        return $blogModel; //Return the Model instance
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.