Creating a website tutorial questions

edited June 2012 in News & Announcements
Hello,

I am trying another way of learning how to work with fuelcms.

I am trying to learn as much as I can from the user guide and tutorials, but sometimes I don't know where to look in order to find the answer to my question.

I took a simple web site that I have and I am trying to recreate it with fuel.

I am following the Creating a Website Tutorial.

First question is:

In the header _block, I see there are two lines:

echo css('main');
echo css($css);

Why do we have these two lines, isn't echo css('main') enough? What do these lines actually do?

Second question:

I have created a sponsors module following the news module in the tutorial.

Everything works fine besides the frontend sponsors view.

I have the sponsors module on the dashboard.
I see the list of sponsors that I have created.
I can creat, edit and delete a sponsor.

Now I want to have a frontend page that displays these spnosors.

I created this file:
sponsors.php in the views folder.

The code:

<?php $sponsors = fuel_model('sponsors'); ?>

Our Sponsors



<?php
foreach($sponsors as $sp):
$name = $sp->name;
echo "\n \n
\n";
endforeach;
?>
$name


If I go to: localhost/mysite/sponsors

I get this php error:

"A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: fuel/Loader.php(298) : eval()'d code

Line Number: 9"


I checked the sponsors array by print_r($sponsors); and it has all the values in it and everything looks fine.

So what am I doing wrong?

Question 3:

If I look at the Creating Simple Modules tutorial I see they are using $CI->load->model('model_name').
When should I use:
$sponsors = fuel_model('spnosors');
and when do I use
$CI->load->model('sponsors');
$sponsors = sponsors->find_all(); ?

And I don't understand how does: $CI->model_name->find_all() works, there is no function in the module called find_all so what is it? Is it a function built into fuelcms with the table_class so it just works out of the box?

Thank you.

Comments

  • edited 3:24PM
    1. The $css refers to a variable that you can set on your pages or in a variables file so that additional CSS files can be added to your page. Examples:
    // from within a view file fuel_set_var('css', array('my_css')); // from within your global variables file $vars['css'] = array('my_css'); // using the page array in your variables file $pages['my_page'] = array('css' => array('my_css'));

    2. Does your Sponsors_model also contain a "Sponsor_model" record class? If it works using an array syntax instead of the object syntax, then you may need to include that:
    foreach($sponsors as $sp): $name = $sp['name']; echo "\n $name\n \n"; endforeach;

    3. fuel_model is a convenience function that wraps around $CI->load->model('model_name'). It will load the model and then call the method in a more compact format.
    // using fuel_model $sponsors = fuel_model('sponsors'); // is the same as $CI->load->model('sponsors_model'); $sponsors = $CI->sponsors_model->find_all();
    Also, with regards to the second part of your 3rd question, a simple module is essentially a wrapper around a model. The model class contains the find_all method (and a bunch more). If you haven't read the documentation on models, I'd recommend it:
    http://www.getfuelcms.com/user_guide/libraries/my_model
    http://www.getfuelcms.com/user_guide/libraries/my_model/table_class_functions
    http://www.getfuelcms.com/user_guide/libraries/my_model/data_record_class_functions
    http://www.getfuelcms.com/user_guide/libraries/base_module_model (extends MY_Model for modules)
  • edited 3:24PM
    Hi admin,

    1. Regarding the css, just want to make sure I understand correctly.
    The echo css('main') will link to the main.css file.
    The echo css($css) will link to any css file they I add in the variables file, right?

    2. My sponsors_model has this function (Please see my comments in the code):
    class Sponsor_item_model extends Base_module_record
    {
    // Not sure what this function is doing but I copied it from the news model in the tutorial//
    function get_url()
    {
    if (!empty($this->link)) return $this->link;
    return site_url('sponsors/'.$this->slug);
    }
    }

    * Just wondering why do I need to use the array syntax when all the tutorials and user guides use the object syntax. I tested it now with the array syntax and it works, but I need to understand why, if possible.

    If you don't mind, I have another question now, I am not sure what does this function do:
    function _common_query()
    {
    parent::_common_query(); // to do active and published ?? What does that mean?
    $this->db->order_by('category desc');
    }

    3. Thanks, I understand point 3.

    4. THANK YOU SO SO MUCH, for taking the time to answer all my questions :)
  • edited 3:24PM
    1. That is correct... you can add the variables to the variables file or use fuel_set_var in your view file to set the variable. It's essentially just a view variable.

    2. If you have "Sponsor_item_model" as the name of your record class, you'll need to set the name of the record class as a property in the Sponsors_model if you haven't already like so:
    public $record_class = 'Sponsor_item'
    The reason you need to set it in this case is because by default it looks for "Sponsor_model' (basically removes the "s" from Table class model Sponsors_model). If no record class is found, then it will default to an array.

    3. The _common_query() method is run whenever you use a find_{...} method. It's convenient for adding joins and in this case where statements. The comment refers to the parent class Base_module_model that has in it's _common_query method to automatically remove "published ='no'" or "active = 'no'" records in queries performed in the front end. Therefore we make the call to the parent to make sure that automatically happens for this model.
Sign In or Register to comment.