Welcome Guest, Not a member yet? Register   Sign In
My Class Libraries Living Between Methods Calls in Controller
#1

[eluser]CodeIgniterNewbie[/eluser]
Please help me. I am new to PHP and CI.

1. I created class libraries that mimic some of my DB tables (where I have class members/properties that correspond to DB fields). They also have methods that help me with do a number of things (e.g. I have a Birthdate member/property and have a GetAgeInYears method).

2. For each of the above class libraries, I have an associated model for it. The model has basic CRUD operations for each of the class library member/property -- plus a few more methods.

3. In the controller, I want to instantiate one of my class libraries, assign values to its members/properties via some model call, use some of the object's methods, AND STILL HAVE ACCESS TO THAT OBJECT between pages.

I realize the web is stateless, but is there something that CI can do for me (besides sessions, that is)? If not, did I make an architectural mistake by creating class libraries to represent certain DB tables? Should I just put all these methods I tried putting in the class library in the controller instead?

If I end up having to use sessions, do I just store the record ID and keep querying the DB each time I need something from it? That's expensive, isn't it?

Also, right now, some of the methods in my model return associative arrays. I'm a strongly type kind of guy. Should I convert these associative arrays into my objects (using the classes I built) and return that instead?
#2

[eluser]m4rw3r[/eluser]
You can use some type of cache to save those objects and load them from the cache in every request (provided that they exists in the cache).
Then all the properties are saved and used for all requests.
Memcahced can be a good cache: http://en.wikipedia.org/wiki/Memcached .
#3

[eluser]CodeIgniterNewbie[/eluser]
But what about my methods? I'm not only interested in the properties.

Again, did I make an architectural mistake with my class libraries? Should those methods just exist in the controller?
#4

[eluser]CodeIgniterNewbie[/eluser]
I supposed I can re-instantiate the object and populate it using the cache. That would eliminate the need to query the DB. But if all I am after is the methods in the class, I might as well just take the methods out of my class libraries and put them in the controller. That way, I don't need to be bothered instantiating classes.

I wonder if this breaks OOP. In an object, I would have, say, the birthdate information. I could have a GetAgeInYears method in that object. If I cache the birthdate, my GetAgeInYears function would be placed in the controller and would need to accept "birthdate" as an argument to the function. That seems a bit procedural to me. Thoughts?
#5

[eluser]CodeIgniterNewbie[/eluser]
And, if I put all my class library methods in the controller, I might as well get rid of my class libraries. My controller will end up with so many unrelated functions. That breaks OOP.
#6

[eluser]m4rw3r[/eluser]
Use serialize() and unserialize() to store the data as a string in a cache, when the data gets unserialized the data in the object will be restored and the __wakeup() method of the object will be called.
The memcache PHP extension does this automaticly, I think.
I also recommend looking at object serialization and __sleep() and __wakeup().
#7

[eluser]CodeIgniterNewbie[/eluser]
Were there architectural problems with my approach?
#8

[eluser]m4rw3r[/eluser]
I personally think it is easier if the data gets tied up into objects, provided that those objects can be reused.
On the other hand, arrays can also be a great way to store data, if the data only needs to be manipulated/saved in the same class or if the data is not complex.

Of course, the greatest benefit by using objects is that you have the methods tied to the data and that the data is encapsulated in the object.
#9

[eluser]CodeIgniterNewbie[/eluser]
Well, the classes I made that mirror some of the tables in my DB seems to make sense to me. I guess my problems are:

1. My objects seem useless in the controller since they are not persisted between page loads. I guess I had this idea of the object just being available to me whenever I need it.

2. I'm still not sure if I should instantiate my class in the model and assign its members the values returned from the DB -- and have objects returned to the controller instead of arrays. Does this break MVC?
#10

[eluser]m4rw3r[/eluser]
1. Speed. If you load everything if you don't need it, your code spends a lot of time loading the data that doesn't need to be loaded.

2. No it does not break MVC, the model still fetches the data, the controller makes some validation and/or some processing of the data and the view shows the result. The data doesn't have to be in arrays for it to be MVC compliant.

In your model, you can do something like this:
Pseudo code:
Code:
If requested object is in cache:
    load object from cache
    maybe make some method calls or assign some other properties to the object
    return the object
else:
    prepare query
    fetch data from the database
    instantiate the object and load the data into it
    maybe make some method calls or assign some other properties to the object
    save the object in the cache
    return the object
If you will only use MySQL as a database, you can use the mysql_fetch_object() to load the data directly into the object (I do not know if the other databases have the ability to load the data into any class, or if they are limited to stdClass).




Theme © iAndrew 2016 - Forum software by © MyBB