Welcome Guest, Not a member yet? Register   Sign In
What object do I need to use?
#1

[eluser]solepixel[/eluser]
Helpers, models, libraries.. I'm a bit confused. What i want to do is setup a class that I can autoload and use to quickly spit out a select element. The contents of the select element are variant based upon a category which fetches a list from the database. For example:

$obj = $this->load_select('states');
$select = $obj->build();

would lookup into a table in my database all items that are in the 'states' category and return a list sorted by an 'order' column, then alphabetically. My problem is I tried to use a helper, and I couldn't figure out how to connect to my database inside the helper. So I assumed that's not what I needed. Then I set it up as a library, but now i continue to get this error:

Message: Missing argument 1 for Load_select::Load_select(), called in \system\libraries\Loader.php on line 931 and defined

Does this mean that libraries cannot have a constructor method? If so, then a library is not what I need. I guess I need a plain old class. Can someone tell me what I need to do here?
#2

[eluser]jedd[/eluser]
Hi solepixel, and welcome to the CI forums.

Have you read the [url="http://ellislab.com/codeigniter/user-guide/toc.html"]CI user guide[/url], particularly the pages on Controllers, Views and Models?

Now, in the classic way of explaining something complex by basically lying about it - know that I am lying to you now, but assume that I am not.

You want to use a model.


Longer answer ..

The model is a class, as you deduce. It extends a native CI class. The manual will help you here. It should, in general terms, be an interface to one database table. But if it's more logical to have it provide an interface to several tables, that's quite okay too.

Have a go and come back when^H^H^H^Hif you have problems. Wink
#3

[eluser]bretticus[/eluser]
This is all possible within the MVC coding paradigm and with CI. You shouldn't need your own class (library.)

Your controller can either call a model that calls your database resources or you can call database resources directly from the controller.

See the database library

You will then pass the data for populating your select to your view (or template but called views in CI.)

See views in CI Docs

Finally you can just use the form helper.

See loading helpers

form_dropdown() under form helper can take an associative array to populate it with options. You can also set the selected option.
#4

[eluser]solepixel[/eluser]
Ok. I think i understand a little now. I setup a model that simply fetches the data of the type i'm requesting, although I really wish I could use:

$data = $this->misc_types('mytype');
rather than
$data = $this->misc_types->get_types('mytype');

Anyway, then I moved my select creation into a helper, where i'm preparing my data to go into the form_dropdown() function.

So that has helped a bit to understand how these pieces fit together, I just wish I could use some native class calls that allow me to use the constructor (with variables) to make the code a little simpler.
#5

[eluser]jedd[/eluser]
[quote author="solepixel" date="1237423394"]

$data = $this->misc_types('mytype');
rather than
$data = $this->misc_types->get_types('mytype');
[/quote]

When I used to be a sysadmin, I'd always have a short length of timber on hand, and would take it when I visited the desk of someone who had just created a 'misc' sub-directory in the shared directory on the organisation's file server.

Do you have a short length of timber? There's something I'd like you to do for me ... Wink

I'd suggest that you prefer the former over the latter form because your nomenclature is not well considered.

'misc_types' is a combination of two words, neither of which has any real meaning, and combined they form a variable name with less than no meaning. Imagine yourself, in six months from now, looking at the code and trying to work out what 'misc_types' is. (This is always a good exercise.)

If you design your model a bit better, you'll probably find the $this->Thing->Action approach to doing stuff is far more useful. Or if you prefer, consider it as Noun->Verb.


Quote:So that has helped a bit to understand how these pieces fit together, I just wish I could use some native class calls that allow me to use the constructor (with variables) to make the code a little simpler.

I'm not sure what you mean here - could you elucidate a bit more on what you'd like to do?
#6

[eluser]solepixel[/eluser]
I appreciate the input however the table structure I'm using for this is actually "misc_types". sure it could be worded differently, however the idea is i have a web application and throughout the application there are various types which need to be manageable in some sort of admin system. it's usually the same for each:
form_element_types: form_element_type_id, form_element_type_value, form_element_type_order
address_types: address_type_id, address_type_value, address_type_order
insurance_types: insurance_type_id, insurance_type_value, insurance_type_order

SO instead of having 3 or more different tables for these manageable types, i created one table with a 'category' for each
type_id, category, value, order

And i want to query a set easily.




Theme © iAndrew 2016 - Forum software by © MyBB