How to get class object with $query->row on Codeigniter
Asked Answered
B

5

5

I'm currently working with the Codeigniter framemwork. In the code below, I would like to get an Animal_model object, not a stdClass Object.

<?php
class Animal_model extends CI_Model{

    var $idanimal;
    var $name;
    public static $table = 'animals';

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

    function getone(self $animal){
        $query = $this->db->get_where(self::$table, array('id_animal' => $animal->idanimal));

        if($query == FALSE){
            return FALSE;
        }else{
            return $query->row(); // How to get an Animal_model object here? 
        }
    }
}

$lion = new Animal_model();
$lion->idanimal = 25; 
var_dump($lion); // It says "object(Animal_model)".

$lion = $lion->getone($lion);
var_dump($lion); // Now it says "object(stdClass)".

?>

How to convert $query->row() to an Animal_model object?

Bernardabernardi answered 17/12, 2012 at 14:38 Comment(0)
T
6

CodeIgniter has built-in functionality for this!

return $query->row(0, 'Animal_model');
Tyus answered 17/12, 2012 at 15:20 Comment(0)
R
1
    if($query == FALSE){
        return FALSE;
    }else{
       // How to get an Animal_model object here? 
       $row = $query->row();

       $animal = new Animal_model();
       $animal->idanimal = $row->idanimal;
       $animal->name= $row->name;
       return $animal;
    }

The above will do what you want but I don't think it's a very good idea. It would be better to have a second class such as Animal which doesn't extend any model that you can use to represent an animal.

class Animal
{
    public name = '';
    public id = 0;

    public function __construct($id, $name)
    {
        $this->id = $id;
        $this->name = $name;
    }
}

In your model, you can create instances of this and return them, instead of returning model objects.

    if($query == FALSE){
        return FALSE;
    }else{
        $row = $query->row();
        return new Animal($row->idanimal, $row->name);
    }

I would also change the getone function to take an ID instead, and select where this ID matches:

function getone($id){

This way you use the model as a manager for the animals, the model deals with the data and returns Animal objects.

Rabideau answered 17/12, 2012 at 14:49 Comment(0)
L
0

Your first var_dump($lion) is of type object(Animal_model).

You then overwrite the value of $lion with the result of $lion->getone($lion); $lion is now either FALSE, or the value of $query->row(), which returns an object(stdClass).

$query->row() will always return a database result object (a stdClass object).

Your code is doing what it's supposed to.

http://ellislab.com/codeigniter/user-guide/database/results.html

May I ask why you need that object to be of type Animal_model?

Longcloth answered 17/12, 2012 at 14:48 Comment(1)
The row method will not alwaus return a database result object. You can choose between a stdClass object, an array, or a custom class. See this or have a look inside system/database/DB_Result.php.Tyus
B
0

Instead of return $query->row(); you can instantiate an object of the Animal_model class and assign the values from $query->row() to the properties of the object. But I'm not sure I see any value in doing this. You will still be getting the same data back. Is there a method you need in the Animal_Model class that you want to call after retrieving the row?

Sidebar note... You might want to avoid the use of "var" to describe your properties. Probably you want to use "Protected" unless you can think of a reason for Public or Private.

Boyse answered 17/12, 2012 at 14:52 Comment(1)
Thank you for the tip about var / protected.Bernardabernardi
B
0

If I'm understanding your question; when you make a call to your database table instead of individual column data you want a custom Class object (of type Animal_model) right?

First of all then, when you make a call to $query->row() you don't retrieve an Animal_model object. You're retrieving instead a database row object. Which you actually have to extract individual data instances from:

   $row = $query->row(); 

   echo $row->title;
   echo $row->name;
   echo $row->body;

Therefore, because of the way you've designed your class I would either recommend creating a custom static function that would take an instance of a database row object and instantiate a new Animal_model instance. Or a method that takes an instance of a database row object and fills in the fields of said object.

Buonarroti answered 17/12, 2012 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.