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
#2

[eluser]WanWizard[/eluser]
There are basically two approaches to using Models.

One is to use the model class to encapsulate a data object, so every instance of the model represents a distinct piece of data (for example a database record). This is what you do in your Java code, and how most ORM implementations use the model.

In CodeIgniter however, the Model is just an abstraction class, to keep your controller code clean and simplify interaction with whatever datasource you want to access. In that sense it is basically the same as a library. A CI model is loaded as a singleton.

I suggest you check out one of the ORM solutions available for CI, if you want to use data objects.
#3

[eluser]avramovic[/eluser]
Thank you, I'm reading about this one in your signature, it looks good, I will probably give it a try.
#4

[eluser]wiredesignz[/eluser]
Actually WanWizard is incorrect. Models are not data objects, models should contain business rules that control your application as well as providing access to your database or any other data source that you might need. Models should be used to encapsulate your data objects or data access. The confusion stems from his lack of understanding of the MVC design pattern and that he has inherited a poorly designed ORM implementation. The models he is referring to are actually data objects based on what should be a library class. He just calls them models for lack of understanding.

EDIT:
If there is a dispute about this I would ask WanWizard to prove why his data objects should be classed as models? I can prove they are not models because they do not contain business rules governing how their data is represented.
#5

[eluser]avramovic[/eluser]
Okay, but you said "Models should be used to encapsulate your data objects or data access."

I understand encapsulating data access with a singleton class, but how can I encapsulate distinct piece(s) of data in CI?
#6

[eluser]wiredesignz[/eluser]
Create and use your data objects in the model rather than the controller. Use ORM or Active Record as you wish.

READ:
http://kore-nordmann.de/blog/why_active_...sucks.html




Theme © iAndrew 2016 - Forum software by © MyBB