Welcome Guest, Not a member yet? Register   Sign In
Best practices using multiple object instances?
#6

[eluser]Unknown[/eluser]
I am pretty new to CI and faced this challenge. This is what I devised to get around this problem without any change or addon to CI.

This is an example code to create multiple instances of an object/model

Code:
foreach($_REQUEST['users'] as $userid){

    $objectName = 'u'.$userid; // each object is given a distinct name

    $this->load->model('USER', $objectName); // load the model with a distinct name

    $usersArray[] = $objectName; //store the object/model names in an array.

}

To use the object use the following code

Code:
foreach($usersArray as $theUser){

    $this->$theUser->show(); // the value of the variable '$theUser' is considered as a variable. So its like a variable inside a variable

}

For more explanation please refer to my post How to Create and Handle an Array of Models or Objects in Codeigniter


Messages In This Thread
Best practices using multiple object instances? - by El Forum - 07-28-2010, 08:49 AM
Best practices using multiple object instances? - by El Forum - 07-28-2010, 09:07 AM
Best practices using multiple object instances? - by El Forum - 07-28-2010, 09:26 AM
Best practices using multiple object instances? - by El Forum - 07-28-2010, 09:35 AM
Best practices using multiple object instances? - by El Forum - 07-28-2010, 12:04 PM
Best practices using multiple object instances? - by El Forum - 07-31-2011, 09:20 PM
Best practices using multiple object instances? - by El Forum - 08-01-2011, 01:34 AM
Best practices using multiple object instances? - by El Forum - 08-11-2011, 06:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB