Using Custom Libraries with Model instances. |
08-24-2018, 11:47 PM
(This post was last modified: 08-24-2018, 11:57 PM by deltatangodt. Edit Reason: adding more data )
Hello, trying to understand the best way to have multiple instances of a class with its own Model "instance" & CI capability.
As an example, let's say I am handling a transaction between 2 users who interact, and I want to load/create 2 instances of a User class where each has methods to update their records. Would a library be the best way to do this? What I am thinking is: /libraries/User.php PHP Code: class User{ Then, in my controller, I am thinking of doing: PHP Code: $sender = new User($senderId); After reading the documentation and thinking this over, it seems to be the best way but want to make sure.
So delving more into the documentation and looking all over the internet there seems to be a couple ways to do this.
I am documenting it for future users: 1. If I do the above code [creating a library], I need to actually then load it in my controller like so: $this->load->library('User',array('id'=>$senderId),"sender"); $this->load->library('User',array('id'=>$recipientId),"recipient"); And then do: $this->sender->set('name','Thomas Smith'); $this->sender->set('location','Los Angeles'); $this->sender->save(); $this->recipient->set('name','Peter Jones'); $this->recipient->set('location','London'); $this->recipient->save(); OR, you could probably do (in your controller): $this->load->library('User',array('id'=>$senderId),"sender"); $this->load->library('User',array('id'=>$recipientId),"recipient"); $sender =& $this->sender; $recipient =& $this->recipient; And then use them in your code as: $sender->set('name','Thomas Smith'); $sender->set('location','Los Angeles'); $sender->save(); $recipient->set('name','Peter Jones'); $recipient->set('location','London'); $recipient->save(); --- |
Welcome Guest, Not a member yet? Register Sign In |