Welcome Guest, Not a member yet? Register   Sign In
Create my own helper
#1

[eluser]Packe[/eluser]
Hi,

I'm new to CI and trying to learn it. My questions may have been already asked but I'm not sure what to search for.

I am trying to create my own helper which shall be looking up values in the database. I will have several idConfused from a few tables and I want to lookup the names for these values. For that I want to create a generic helper that will not be locked to a single table.

Here is my code:

Code:
<?php
    function db_lookup($table,$column,$id)
    {
        $query = $this->db->query("SELECT ".$column." FROM ".$table." where id=".$id."");
        $row = $query->row();
        return $row->$column;
    } ?>

When using it, I am getting "Fatal error: Using $this when not in object context in..." on the line with $this->db_query.

What am I doing wrong and what shall I do instead?

And do you have some general tips when creating helpers?

Thanks in advance!
#2

[eluser]Dam1an[/eluser]
The problem is that the helper is not in the scope of the CI super object, to get around this in helpers (and libraries) see the section on Utilizing CodeIgniter Resources within Your Library

Edit: Opps, completely missed that there's DB activity taking place outside a model, naughty me
#3

[eluser]jedd[/eluser]
[quote author="Packe" date="1249139669"]For that I want to create a generic helper that will not be locked to a single table. [/quote]

Hi Packe and welcome to the CI forums.

I think this question exposes a flaw in your understanding of the way CI, or rather the MVC architecture, is meant to work.

The code snippet you provided is something you really shouldn't ever need to implement. It subverts the intent of the model component of your application - which is to abstract your database, or if you prefer, to provide a consistent front-end to all your database needs. A one-stop shop, and all that.

Having a backdoor like this helper function, apart from being a messy design (by offering two ways of doing the same thing) will end up biting you if you ever change your schema, for example. In the ideal world (database abstraction done by the model(s)) you change your model in one and your controllers stay happy. In the approach you're looking at, you would have to search for every place in your controllers and models where you have called your db_update() helper function, check if it's going to be affected by your schema change, and if necessary modify it.
#4

[eluser]Packe[/eluser]
Hi,

Thanks for the replies.

jedd, thanks for pointing that out to me. But could you give me a hint on what I should be looking at instead in order for me to solve this? Trying to understand CI and the MVC but I got much to learn =)
#5

[eluser]jedd[/eluser]
[quote author="Packe" date="1249144570"]
jedd, thanks for pointing that out to me. But could you give me a hint on what I should be looking at instead in order for me to solve this?[/quote]

Sure, but first you'd need to give me some specific examples of what you're trying to do, some data that you want to model (in the little-m sense of the word).

In general terms, given you're just starting out with this stuff, try to keep all your database activity in your Model files. A lot of people adopt a one-Model-per-database-table approach, which might help (although I don't follow that mantra myself).
#6

[eluser]naren_nag[/eluser]
Ok, if you need to access $this->{anything} in a helper you first have to know that $this refers to the CI superclass.

The controller class extends the CI_Base class, and $this then gives us access to everything in the base class as well as in the controller we're creating.

While writing a helper, you don't extend a class in Codeigniter, so you need to create an instance of the CI superclass by writing the following

Code:
$CI =& get_instance(); // Don't forget to put the & after the =

Now, suppose you want to get a session variable in your helper, you would do the following.

Code:
$username = $CI->session->userdata("username");

That's it, just replace the $this in your statements with $CI (or whatever variable name you have chosen, though I would recommend sticking to $CI)

Now that you know how, I would strongly recommend against accessing your database in any place but your model.

If you're like me and hate writing lots of CRUD code, then check out an ORM library like Datamapper Overzealous - http://overzealous.com/dmz/index.html

You'll still need to create models, but very simple ones (typically just a class definition). And then you can write code like this in your controllers:

Code:
$productObject = new Product(); // where you have a table called products in your database
$productObject->get_by_id(1);   // where id is a field in your table

$productDetails = $productObject->to_array(); // Get all the row data into an array

Have fun discovering CI.

cheers,

naren
#7

[eluser]Packe[/eluser]
naren, thanks for the tip. I shall check it out.

jedd, I hope the following example can explain what I am trying to do:

Code:
Table Report:

title   | date | mission
-------------------------
varchar | date | smallint

Table Mission:

id       | name
------------------
smallint | varchar

When presenting data from the table "Report", a lookup in the table "Mission" shall be done with the id stored in the column "mission" in "Report" in order to present the name and not the id.

There are a few more such lookups I would like to do in my code so that is why I first thought of a generic function as the lookups are not done in the same tables.
#8

[eluser]jedd[/eluser]
Well .. what I'd do is have a model, perhaps called Report, and within that I'd have a method called mission() that did a SQL query along the lines of "SELECT title, date, name FROM report LEFT JOIN mission ON report.mission=mission.id". This returns its data to whatever controller you've got that's calling for it.

Things get complicated (in my mind) when you want to select data from multiple tables if you've adopted a single-table-per-model approach. Tis is one of the reasons I like my models to handle groupings of tables.
#9

[eluser]Packe[/eluser]
Created a model in which there was a function that did what I wanted, worked just fine =)

Many thanks for all your help!




Theme © iAndrew 2016 - Forum software by © MyBB