Welcome Guest, Not a member yet? Register   Sign In
Can I use Models in My libraries?
#1

[eluser]Ghassem Tofighi[/eluser]
Hi All, I wrote a model for fetching some columns of my tables from my database. It works fine when I use in normal situation, e.g. :

Code:
$this->load->model('ModelName');
$this->something = $this->ModelName->get('something');

But when I want to use it in my own library I encounter error, e.g. :

Code:
$this->CI =& get_instance();
$this->CI->load->model('ModelName');
$this->CI->something = $this->CI->ModelName->get('something');

Then I think I can not use models in my own libraries, and I can not use libraries that use my models. Is it true?

Please help me
#2

[eluser]adamp1[/eluser]
Of course you can use model in your own library. The bit I don't like about your code is
Code:
$this->CI->something

Why are you trying to save the variable to the CodeIgniter global object? You don't need the CI-> bit for when you want to save data to class variables.
#3

[eluser]xwero[/eluser]
You can do it but there is discussion if a model should not be in a library because a library is supposed to be an entity. The word library in CI speak is liberal. it can be a core class, an extended class, a custom class that uses CI or an entity class.

If you want to create 'correct' code you better use a model class for a custom class that uses CI this has one advantage, you don't have to do
Code:
$this->CI =& get_instance();
#4

[eluser]webthink[/eluser]
The code looks fine and should work for accessing models from within your libraries. What error are you getting?
#5

[eluser]webthink[/eluser]
Hey xwero,

Slightly off topic. I agree the the definition of library in CI varies. That's why I think there should be three levels of libraries System, Shared, and Application. There's a thread to this effect in the requested features forum.

There are times when all I want to do is farm out some of my application specific code that would otherwise be repeated in a number of controllers so I use libraries. These are more than likely highly specific to the application and by the strict definition aren't really 'libraries' as such, but under CI's loose definition It allows me a very effective way to house and use this kind of code.
One example may be an auth library. There can be logic in there that accesses the database via models, sessions via the session library etc. All very app specific stuff.

On the other hand We have a number of non-application specific libraries that stay true to the stricter definition and are abstracted to a level that they can be dropped into any application and used without modification.

Currently the former and the latter all sit together under applications/libraries folder which IMO is not ideal.
#6

[eluser]xwero[/eluser]
Webthink the topic about shared libraries is another case. That is about the reuse of libraries when you have multiple applications. In this thread i'm talking about the definition of a library and the use of models.

You mentioned you use 'fake' libraries to reuse the code. But you can do the same if you put the code in a model. A model is a dummy class which has nothing to do with databases. Let's say you create a xml/xpath parser class that is a data fetching/altering class so in fact it's a model. It doesn't matter where your data comes from or if it's text or binary.

The authentication library is a model because it can't function without data. You can make it a library if you don't use CI methods.

If you want to reuse it in other applications you can create a shared model directory. For the other topic this means in my view there should be a view and models directory in the system directory for shared code.

ps : the topic webthink and me are referring to
#7

[eluser]webthink[/eluser]
I brought up shared libraries because any library that doesn't have application logic in it is a candidate for being shared -- they aren't the same thing but the two topics are inherently linked.

I don't believe either of us are "wrong" here but the idea of putting application logic inside models makes me feel the same way, I suspect, that putting it inside libraries makes you feel (icky Wink )
#8

[eluser]xwero[/eluser]
True it's not about who is wrong it's about the consequences if you go one way or the other. The most visible consequence is the use of get_instance in libraries and not in models.

Models don't have to be application bound. Instead of a database scheme you can set the tables and fields in the model code. A database scheme is an easy way to go but it prevents database errors so it's not wrong. To complete the authentication model the validation can be abstracted and the session values can be prefixed so there is no danger of overwriting application session values. Which makes a authentication model application independent.

I misread thread 4 that's why my previous response was a bit weird Smile




Theme © iAndrew 2016 - Forum software by © MyBB