Simple database module

edited July 2011 in Modules
Hi,

I think Fuel is awesome! It's a great free tool.

Could you help me understand the Model View relationship a little better?

I made a module called help_module.php that looks like this:


<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Help_model extends Base_module_model { function __construct() { parent::__construct('help'); // table name } } class Helper_model extends Base_module_record { } ?>


I changed MY_fuel_modules and added this:


$config['modules']['help'] = array( 'preview_path' => 'about/faq', );

And I made a view file called help.php

<?php $CI->load->model('help_model'); ?> <?php $help = $CI->help_model->find_all(); ?> <?php foreach ($help as $helpist) : ?> <div id="block_help"> <?=$helpist->name?> </div> <?php endforeach; ?>


I'm getting the error:

Message: Trying to get property of non-object

Filename: views/help.php

Line Number: 5

Comments

  • edited 4:09PM
    Oh, also - I swear on my grandmother's eyes there is data in the help table.
  • edited 4:09PM
    Because the name of your model doesn't end in an "s", you will need to add the following to your model to identify your record model:
    public $record_class = 'Helper';

    Also, as a side note, you can use the following as an alternative to get your data from the module:
    <?php $help = fuel_model('help', array('find' => 'all)); ?> <?php foreach ($help as $helpist) : ?> <?=$helpist->name?> <?php endforeach; ?>
  • edited 4:09PM
    Hooray! Thanks for your quick, succinct help. You are the best helpist!

    I'm new at PHP, brand new at CI, and haven't even taken the stickers off Fuel, so it took me a second to figure out where to put that code. For those playing along at home, that would make help_model.php look like this:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Help_model extends Base_module_model { function __construct() { parent::__construct('help'); // table name } public $record_class = 'Helper'; } class Helper_model extends Base_module_record { } ?>

    Is there any benefit to using fuel_model('help', array('find' => 'all')); ? Is it quicker? Is it so that if I'm not going to use all the variables I can tell it which ones like this:

    $help = fuel_model('help', array('find' => 'one', 'where' => array('name' => $name)));
  • edited 4:09PM
    The only main advantage is that it is less code and if you import your view file later into the CMS, that function is one that is allowed with Dwoo syntax.
Sign In or Register to comment.