Welcome Guest, Not a member yet? Register   Sign In
MVC structural advice - separation of code
#15

[eluser]coffeeandcode[/eluser]
[quote author="Lyon" date="1279837537"]@coffeeandcode
I'm not sure if that would have been what I wanted as the idea would have been to load more than one user at a time.
e.g.
Code:
$user1 = new User();
$user1->Load_From_Id(0);
$user2 = new User();
$user2->Load_From_Username("fred");

//Do something with the 2 users here
[/quote]

Interesting. Why would you want to load 2 users at once? Is this from a user management perspective, or would you have 2 users logged in at once? I'm having a hard time understanding why one would want to do this.

In any case, I'd probably use one user model, and allow my load function to accept any number of ids and usernames. That way you don't need to worry about 2 instances of the class, and can benefit from using a model (which you really should be doing for data access). Doing it the way you've described would be a nightmare to maintain if you ever need to modify your interface to user data.. the code will be all over the place, instead of collected all in a users model. That's what models are for - to provide a "closed" interface to data.

ie:
Code:
class User extends Model {

    var $db;
    var $loadedUsers;    // collection of loaded users is kept within the model

    function User() {
        parent::Model();
        // db and any other initialization
    }

    function load($data = array()) {
        extract($data);

        foreach ($ids as $id) {
            // load by id
        }

        foreach ($usernames as $username) {
            // load by username
        }
    }
}

and to use it from your controller:
Code:
$this->load->model("user");

$users = array(
    "ids" => array(23, 17, 52),
    "usernames" => array("fred", "bob", "alice")
);

$this->user->load($users);

I guarantee you CI provides a neat and simple way to do almost anything. I don't see why one would ever need to use New and maintain their own classes. But I could be wrong - stranger things have happened Wink


Messages In This Thread
MVC structural advice - separation of code - by El Forum - 06-15-2010, 02:31 PM
MVC structural advice - separation of code - by El Forum - 06-15-2010, 06:07 PM
MVC structural advice - separation of code - by El Forum - 06-16-2010, 07:32 AM
MVC structural advice - separation of code - by El Forum - 06-16-2010, 07:37 AM
MVC structural advice - separation of code - by El Forum - 06-16-2010, 09:11 AM
MVC structural advice - separation of code - by El Forum - 06-16-2010, 03:00 PM
MVC structural advice - separation of code - by El Forum - 06-16-2010, 03:07 PM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 06:09 AM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 06:24 AM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 09:29 AM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 11:05 AM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 11:09 AM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 11:16 AM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 11:25 AM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 11:51 AM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 12:25 PM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 12:55 PM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 01:03 PM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 01:29 PM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 01:50 PM
MVC structural advice - separation of code - by El Forum - 07-22-2010, 02:29 PM
MVC structural advice - separation of code - by El Forum - 07-01-2012, 09:30 AM



Theme © iAndrew 2016 - Forum software by © MyBB