Welcome Guest, Not a member yet? Register   Sign In
Populating an array of objects using CI's load->model() functionality
#3

[eluser]Dan Horrigan[/eluser]
n0xie: I could be wrong, but I believe that the second load of the same model would just be a reference to the first...no?
EDIT: No, I was wrong, it creates a new object every time, provided you give it a different name each time.

Anyways, why would you want to do this? You wouldn't want to load the model multiple times. That is not efficient. You should have the detail model return all the details for a given header. It is easy and efficient:
Code:
//WARNING:  UNTESTED CODE  Intended as example only

class Program extends Controller
{
    function Program()
    {
        parent::Controller();
        $this->load->model('tx_header');
        $this->load->model('tx_detail');
    }

    //This loads all the details for a header
    function view_header($tx_header_id)
    {
        $details = $this->tx_detail->get_all($tx_header_id);
        $this->load->view('header', $details);
    }
}

// Your tx_detail model
class Tx_detail extends Model
{
    function Tx_detail()
    {
        parent::Model();
    }

    function get_all($tx_header_id)
    {
        $this->db->select('*')->from('tx_details')->where('parent_tx', $tx_header_id);
        return $this->db->get();
    }
}

You could also create 2 libraries (not models), one called TxHeader and the other TxDetail. Then the TxHeader can contain an array of TxDetail objects.

Either way is better than trying to load the same model a bunch of times.

Dan


Messages In This Thread
Populating an array of objects using CI's load->model() functionality - by El Forum - 04-21-2010, 09:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB