Welcome Guest, Not a member yet? Register   Sign In
Accessing models from helpers
#1

[eluser]Mr. Pickle[/eluser]
Is it allowed to access a model from a helper? And if so, how should you set this up for an own helper file?

In my case some views have an id of a record. I'd like to have this converted to a name with a helper function allowing me to run a function like:

Excerpt of viewfile code
Code:
<?php echo get_record_name($id); ?>

My helper file has:
Code:
/**
*
* Convert an id to a name
*
* @access    public
* @param    integer
* @return    string
*/
if ( ! function_exists('get_record_name')) {
    function get_record_name($id) {
        // I think I have to do something here to get access to models
        $name = $this->record_model->get_name_by_id($id);
        return $name;
    }
}

I seem to have no connection to my models in helpers. I understand that helpers should be simple procedural and independent so I like to hear your opinions on if this is possible and do-able.
#2

[eluser]Mr. Pickle[/eluser]
@admin/moderator:
Please move this thread to Code and Application Development I accidentally posted it in the wrong category.
#3

[eluser]Zack Kitzmiller[/eluser]
Code:
function foo($id) {
   $CI =& get_instance();
   $CI->load->model('record');
   $CI->record->get_name($id);
}
#4

[eluser]Mr. Pickle[/eluser]
Thanks! Would you say this is an accepted method as well, or can I best have this retrieved somewhere else?

I mean, is it a big performance load this way, in addition to retrieve them all beforehand with and pass it along with the base data to the view?
#5

[eluser]Zack Kitzmiller[/eluser]
This is how you're supposed to do it.

By using =& you are passing a REFERENCE to the CodeIgniter Super Global with is a Singleton.
#6

[eluser]Mr. Pickle[/eluser]
Hi Zack, I understand now that this method is valid, and it works!

But.... will this be the optimal way of retrieving the name?
I mean I can have all id's looped and call the model from my controller before I load my view or even have a join or something in my model when I query the id's so I won't have to call the helper each record loop. I'm just not that good into helpers to know what method is best for performance.
#7

[eluser]Zack Kitzmiller[/eluser]
It's more of a best practice thing vs. a performance thing.

I usually call helpers in a few to format things I get from a database. So.

Example:

Controller
Code:
function all_users() {
    ...
    $data['users'] = $this->model->get_all_users();
    ...
}

View
Code:
<? if (!empty($users) : ?>
<ul>
    &lt;? foreach($users as $user): ?&gt;
        <li>&lt;?= full_name($user); ?&gt;</li>
    &lt;? endforeach; ?&gt;
</ul>
&lt;? endif; ?&gt;

Helper
Code:
function full_name($user) {
  return $user['firstname'] . ' ' . $user['lastname'];
}
#8

[eluser]james182[/eluser]
ok so i am having issues getting this to work, it shows the first but that's it.

Helper
Code:
function product_by_id($id)
    {
        $ci =& get_instance();
        $ci->load->model('store/products_m');

        $title = $ci->products_m->get_product_title($id);
        
        return $title->title;
    }

Model
Code:
public function get_product_title($id)
    {
        return $this->db->select('title')
                        ->where('id', $id)
                        ->get('store_products')
   ->row();
    }

View
Code:
<ul>
                            &lt;?php foreach($order->ordered_items as $itm): ?&gt;
                                <li>&lt;?php echo product_by_id($itm->product_id) .' -- $'.$itm->price; ?&gt;</li>
                            &lt;?php endforeach; ?&gt;
                            </ul>
#9

[eluser]InsiteFX[/eluser]
How many forum topics are you going to post this into?




Theme © iAndrew 2016 - Forum software by © MyBB