Welcome Guest, Not a member yet? Register   Sign In
add object methods into CI Database Result
#1

[eluser]adwin[/eluser]
Hi,

I thinks this is not very related but I just need to know how to change from stdclass into model class so that I can have a methods ...

here is the example

$customerlist = $this->db->get($this->_table)->result();

I have customers model like this

class customers extends model{
var id = '';
....
function getCustomerOrders(){
$this->load('orders');
return $this->orders->getordersbycustid($this->id);
}
}

I want all the customer result from db->get()->result(); return as customers obj.

can I do that ?

thx
#2

[eluser]adwin[/eluser]
any suggestion ? Big Grin
#3

[eluser]TheFuzzy0ne[/eluser]
[quote author="adwin" date="1209325724"]Hi,

I thinks this is not very related but I just need to know how to change from stdclass into model class so that I can have a methods ...

here is the example

$customerlist = $this->db->get($this->_table)->result();

I have customers model like this

class customers extends model{
var id = '';
....
function getCustomerOrders(){
$this->load('orders');
return $this->orders->getordersbycustid($this->id);
}
}

I want all the customer result from db->get()->result(); return as customers obj.

can I do that ?

thx[/quote]

I'm sure it can be done. Can you show us more source code? I'm not sure what your code is doing.
#4

[eluser]xwero[/eluser]
Code:
$customerlist = $this->db->get($this->_table)->result();
$customerlist is an object. Or is that not your question? Or do you want your method to return an object?
#5

[eluser]TheFuzzy0ne[/eluser]
[quote author="xwero" date="1209404745"]
Code:
$customerlist = $this->db->get($this->_table)->result();
$customerlist is an object. Or is that not your question? Or do you want your method to return an object?[/quote]

I think he wants his model to return a custom object.
#6

[eluser]Seppo[/eluser]
There is no built-in easy-way to do it, but you can create a helper for this purpose.

Code:
<?php
function set_object_class($object, $class)
{
    if (is_array($object))
    {
        return array_map(__FUNCTION__, $object, array($class));
    }
    if (is_object($object))
    {
        $new_object = new $class;
        foreach (get_object_vars($object) as $key => $value)
        {
            $new_object->$key = $value;
        }
        return $new_object;
    }

    log_message('debug', 'Called ' . __FUNCTION__ . ' with no object/array parameter. ' . $object . ' given.');
}
?>


And you'll just
Code:
return set_object_class($this->db->get()->result(), 'customer');

By the way, I didn't test the function, but you get the idea...
#7

[eluser]adwin[/eluser]
Code:
$customerlist = $this->db->get($this->_table)->result();

This code will return array of stdclass .. what i need is to return array of customers object.

in my customers model (class customer extends model) i want to have methods that load another model ... (like the one in rails)

Code:
$customer->getTransactions($date1,$date2); --> this also will return array of Transaction Model Object.

i think it is more flexible.

at the moment, after i got the result() I do a loop and convert it into Customer Object ... and in $customer->getTransactions I did the same. it is repetitive ... and boring :p
#8

[eluser]wiredesignz[/eluser]
You probably should look at using ORM (Object Relational Model) instead of Active Record.
#9

[eluser]adwin[/eluser]
has CI any stable ORM yet ? I wish to have it but I want the stable and ready for production use.
#10

[eluser]m4rw3r[/eluser]
I'm working on an ORM, but it is not really finished (I aim it to be PHP 4 compatible).
I have a bit of manual work and implementation of JOINs.
I also have not tested it thoroughly, yet (only a release candidate, but the next candidate will have a lot more features).




Theme © iAndrew 2016 - Forum software by © MyBB