Welcome Guest, Not a member yet? Register   Sign In
Models in CI are confusing to me
#1

[eluser]avramovic[/eluser]
Hello,

I've been doing PHP for a long time, but only using procedural PHP and using my custom classes (where needed). I like the CI concept so I've chosen CI to start my next project, but now I'm confused about something:

I'm fresh off the college and we've been working with Java, C++, C#, etc... even some PHP but nothing object oriented. Now, in all other languages I've already mentioned we've been using models like class which is used to store and calculate data, so I'd have completely independent class User which represents the User model, and other class named e.g. UserManager which works with data (of "type" User). So this is some pseudo-java code:

This would be User class:
Code:
public class User {

private string name;
private DateTime date_of_birth;

public User(string name, datetime date_of_birth)
{
  this.name = name;
  this.date_of_birth = date_of_birth;
}

public DateTime getDateOfBirth()
{
  //return date of birth
  return this.date_of_birth;
}

public string getName()
{
  //return name
  return this.name;
}

public int getAge()
{
  //CALCULATE and return age
  return DateTime.Now() - this.date_of_birth;
}

}

This would be UserManager:
Code:
public class UserManager()
{
//store list of users
private ArrayList<User> list_of_users;

//
public UserManager()
{
  loadUsers();
}

private loadUsers()
{
//fetch user data from database, and for each record in
//database, create object User and place it in the ArrayList list_of_users
}

public void addUser(string name, DateTime date_of_birth)
{
  User myUser = new User(name, date_of_birth);
  this.list_of_users.items.add(myUser);
}

// ...

public ArrayList<User> getAllUsers()
{
  return this.list_of_users;
}

public User getUser(int index)
{
  return this.list_of_users[index];
}


}

Later in the code I can access each users' data even if I don't have it (data) in the database by calling e.g. UserManager.getUser(5).getAge(); and everywhere I need to do any work with user(s), I can initialize object of User class and play with it.

Now I know there is number of differences between desktop and web apps (e.g. I don't need local storage for all users (ArrayList in this example) as data is not persistent between two pages and I need to read it again and again (on each page), but I really like the idea to represent certain objects in my app (e.g. user of the app) with object in the code (class User). I've been playing around with CI 2 for a while and I know how to make library and model, but that's good for UserManager when I need only one instance of the class ($this->load->library/model('name'), and then use it with $this->name->function()), but is there a way to initialize object of my class somewhere in the code and pass it to another (my) class to work with it?

Sorry for the long post but I wanted to make sure you understand what I'm talking about Smile


Messages In This Thread
Models in CI are confusing to me - by El Forum - 02-09-2011, 03:10 PM
Models in CI are confusing to me - by El Forum - 02-09-2011, 03:36 PM
Models in CI are confusing to me - by El Forum - 02-09-2011, 04:59 PM
Models in CI are confusing to me - by El Forum - 02-10-2011, 04:08 AM
Models in CI are confusing to me - by El Forum - 02-10-2011, 06:17 AM
Models in CI are confusing to me - by El Forum - 02-10-2011, 02:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB