CodeIgniter Forums
Objects of Models within models? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Objects of Models within models? (/showthread.php?tid=9695)



Objects of Models within models? - El Forum - 07-04-2008

[eluser]Carl_A[/eluser]
I have a structure some what like this:

user_model.php
Code:
Class User_model extends Model
  {
    //some properties and functions
  }

attributes_model.php
Code:
Class Attributes_model extends Model
  {
    //some properties and functions
  }



Now what I need is
Code:
Class Member_model extends Model
  {
     //some properties

     Object of User_model class
     Object Array of Attributes_model class

      //some more functions that may also call functions from those objects

  }


Before you all tell me to put it all into 1 model/class, I need to have them in separate models because they're all user dependent. Everything is variable.

Also, I dont want to simply call functions of User_model and Attributes_Model from within the member_model, i need to access it as a complete object (similar to C++)


How can I make objects of a model within a model?


Objects of Models within models? - El Forum - 07-04-2008

[eluser]Randy Casburn[/eluser]
Code:
$CI =& get_instance();
$CI->load->model('User_model');

Will load the model make a mode available to you in the $CI super object. This really only provides you access through static calls to members of the class thus: User_model::memberName. Depending on PHP version you may have problems with visibility using the class as a static class. Your methods could still be access through CI's calls.

Your other option would be to think about turning these into libraries and instantiating them properly if you need true objects that provide you more than just static classes.

Hope this is helpful.

Randy


Objects of Models within models? - El Forum - 07-05-2008

[eluser]wiredesignz[/eluser]
If you need objects then create objects!

Code:
Class Member_model extends Model
  {
     //some properties

     $objUser = new User_model();
     $objAttributes[] = new Attributes_model();

      //some more functions that may also call functions from those objects

  }



Objects of Models within models? - El Forum - 07-05-2008

[eluser]Carl_A[/eluser]
Thanks for both your replies

[quote author="wiredesignz" date="1215268348"]If you need objects then create objects!

Code:
Class Member_model extends Model
  {
     //some properties

     $objUser = new User_model();
     $objAttributes[] = new Attributes_model();

      //some more functions that may also call functions from those objects

  }
[/quote]

Yes, but now im wondering, do those work as true objects or just allows me static calls?

forexample, objattributes[0], and objattributes[1] would be different objects right?

EDIT:
i meant
objattributes[0]->name = "name1";
objattributes[1]->name = "name2";

something like that.. not just function calls.


Objects of Models within models? - El Forum - 06-07-2011

[eluser]Julian[/eluser]
Hello, good post, please visit my post and see how you can become a millionaire in the forex. [URL="http://millionaireinforex.com"]Become a Millionaire in Forex[/URL]