Welcome Guest, Not a member yet? Register   Sign In
A model per library
#1

[eluser]Hyra[/eluser]
Hey hey,

So. I've been restruggling with my community website. In specific: its structure.

This has been asked time and time again, but I'm getting quite frustrated since, even when reading the other topics, I can't figure out a sollution which works for _me_.

Please note: I'm not trying to start the old "model vs library". I'm merely hoping someone can shed some light on how I should go about with the following ..

-------

For the sake of simplicity, let's say the site I'm rebuilding at the moment is some sort of DeviantART. Users can create an account, upload artworks and get comments on them. Let's just say that's that's all there is to it.

Basically, the way i set it up now is to create libraries for the general tasks the site has to perform. So there is an artistlib, which retrieves data such as ArtistName, Reputation, Location with the help of a corresponding artistmodel.

There is a commentlib which retrieves comments for various parts (comments on a profile, comments on an artwork, etc.) .. again, with the help of a model .. commentsmodel.

The list goes on with artworklib, statslib, browselib, etc.

Thing is, commentslib (ab)uses artistlib heavily to "fill missing data" for the comments such as the name of the poster, the reputation, etc.

It all works, but it feels blodgy and fragile.

The reason i went with libraries was because some stuff will be altered, ordered or rewritten without database interaction, and some stuff will. Hence the model-variant of the library for the database calls.

At the moment I'm trying to figure out whether to:
- Either stick with this structure, which doesn't feel very right
- Just put all the (non database related) library methods in the model as well
- Expand the models so they cover more areas (i.e. comments will be part of the artwork model, as they belong to them)

I do realise this isn't as much a question as it is a "braindump", but I do hope someone can help me going again and put me in some sort of direction Smile


Appendix (for those who want some more info)

Just to illustrate, here's some more problem-cases.

The artist profile would show some boxes with information:

- Latest additions
- Comments from visitors
- Latest blog entry of the artist
- Some statistics (amount of artworks, comments received, etc.)

I was gonna solve this by doing something along the lines of:

Code:
function profile() {
    // Load the needed libs
    $this->load->library("commentslib");
    $this->load->library("browselib");
    $this->load->library("commentslib");
    $this->load->library("statslib");
    $this->load->library("bloglib");
    
    // Do some setting up
    $this->browselib->setArtist($this->artistid);
    
    $this->commentslib->setArtist($this->artistid);
    $this->commentslib->setCount(5);
    
    $this->bloglib->setArtist($this->artistid);
    
    $this->statslib->setArtist($this->artistid);
    
    // Fill the boxes
    $this->smarty_library->assign(array(
                "LatestAdditions" => $this->browselib->getLatestAdditions(),
                "Comments" => $this->commentslib->getComments(),
                "LatestBlogEntry" => $this->bloglib->getLatest(),
                "Statistics" => $this->statslib->getMinimalStats()
    ));
}

As you can see the setting up is repetitive. In this case it could be solved by passing the artist id as a config-parameter to the method.

Anyway, within the commentslib method, artistlib would be called, getting data from the artists who commented. It all just feels wrong.

Even though I like keeping things "modular" by using libraries, it does feel ackward as the libraries depend on each other and 90% of the time use models to complete the task, making them less modular than intended.

I'll stop rambling now .. hope anyone can help me get back on track Wink
#2

[eluser]Eric Cope[/eluser]
can you give your library access to the CI instance? Then, $this->artistid is available within the library class. You could make the loading of the library initialize all of those items. For examples, look at the CI libraries.
#3

[eluser]Hyra[/eluser]
Thanks for your reply Smile

And yep, that's how I'm doing it at the moment.

The library gets loaded and then intialised (by passing it an ID). So for example artislib->init($artistid).

This will then find all the "usual" data that is needed for the artist and store it locally in the library. There's some methods that return objects with the data when needed and some functions to retrieve additional information if needed.

Problem i was finding is that when you, for instance, have a list of comments on an artwork it will initialise the library X times for different artistIDs which are the ids returned by the "commentslib", and you end up with 100+ queries for all the different artists to get their names and such WITHIN the commentslib.

This could be solved by expanding the queries used to get all needed fields right away, or even a select * (even though tacky). But it just feels wrong (hard to explain).

This, added with the fact each library depends on a model makes it a lumpy application.

I've been looking at IgnitedRecord and even though it seems to ease most of the tasks I haven't found yet how to do complicated structures (querying 6 related tables without 'hacking' the core model).

I might just move all the libraries into their models and extend those a fair bit. The models would contain some functions that have nothing to do with the database (formatting names, creating links, pagination, etc.) but maybe that's ok.




Theme © iAndrew 2016 - Forum software by © MyBB