$news_item->get_categories_linked() ends in error in libraries/Fuel_posts.php

Hi,

I'm trying to understand the "Generated Post Pages" in Simple Modules and I went through all docs that deal with that topic, but I get stuck in this error:

The uri I call is something like this:

https://domain.com/en/service/news/slug-headline

/en => forced language string
/service/news => Opt-in Controller => (/views/service/news.php)
/slug-headline => news.slug DB entry

10  1.1349      14197184    include( '...fuel\application\views\service\news.php' ) ..\Loader.php:396
11  1.1851      14319192    Base_post_item_model->get_categories_linked( ???, ??? ) ..\news.php:30
12  1.2001      14342000    Fuel_posts->url( 'category/my-category' )   ..\Base_posts_model.php:301
13  39.0921     14357600    Fuel_posts->base_uri( ) ..\Fuel_posts.php:111
14  112.5062    14379568    Fuel_posts->module_config( 'base_uri', ??? )    ..\Fuel_posts.php:96

Dump $_REQUEST
$_REQUEST['/en/service/news/slug-headline'] =

string '' (length=0)
A PHP Error was encountered
Severity: Error

Message: Call to a member function info() on null

Filename: libraries/Fuel_posts.php

Line Number: 138

Backtrace:

MY_fuel_models.php

$config['modules']['news'] = array(
    'module_uri'     => 'news',
    'model_name'     => 'News_model',

    // ...

    // frontend pages
    // @link http://docs.getfuelcms.com/modules/simple => Generate Post Pages
    'pages' => array(
        'base_uri' => 'service/news',
        'per_page' => 10,
        'view' => 'service/news',

        'list' => 'service/news',
        'post' => 'service/news',
        'tag' => array(
            'route' => 'service/news/tag/:any',
            'view' => 'service/news', 
            'empty_data_show_404' => TRUE, 
            'per_page' => 5
        ),
        'category' => array(
            'route' => 'service/news/category/:any',
            'view' => 'service/news', 
            'empty_data_show_404' => TRUE, 
            'per_page' => 5
        )
    )
);

/views/service/news.php is like the example, but uses get_categories_linked()

<?php
$slug = uri_segment(3);

if (!empty($slug))
{
    $news_item = fuel_model('news', array('find' => 'one', 'where' => array('slug' => $slug)));
    if (empty($news_item)) show_404();
}
else
{
    $news = fuel_model('news');
}

?>



<?php if (!empty($news_item)) : ?>

    <div class="news_item">

        <?php $news_item->get_categories_linked() ?>


        <?php foreach($news_item->tags as $tag): ?>
            <?php echo $tag->name; ?>
        <?php endforeach; ?>

    </div>

<?php else: ?>

        <?php foreach($news as $item) : ?>


        <?php endforeach; ?>

<?php endif; ?>

What I can't figure out is the 'pages' part in My_fuel_modules.php and the effect on /views/_post/*.php files because the script crashes in loading the category model or something

Here is the part where the script crashes:

    /**
     * Returns the all the segments as a string without the base_uri() value. 
     *
     * @access  public
     * @param   string  The key value in the $config['posts']['my_module'][$key] (optional)
     * @param   string  The module's name to grab the config information from (optional)
     * @return  mixed
     */
    public function module_config($key = NULL, $module = NULL)
    {
        if (!isset($module))
        {
            $module = $this->get_module();
        }
        elseif(is_string($module))
        {
            $module = $this->fuel->modules->get($module, FALSE);
        }

        //$this->get_module(); returns null so null->info('pages') breaks it.
        $config = $module->info('pages');

Thanks in advance.

Comments

  • The error in the Fuel_posts.php file on line 138 means it's not finding a $module. What's the name of the module before that error do you know (is it "category" or "news" or something else?). Does the page load without the get_categories_linked() method call?

  • The page loads fine without get_categories_linked() - I think it tries to gather info() about the category module, but can't access it. It's hard to tell.

  • Does your new table have a foreign key relationships of "category_id" set?

    public $foreign_keys = array('category_id' => array(FUEL_FOLDER => 'fuel_categories_model')); // map foreign keys to table models
    
  • It's all there, yes.

        // renders select dropdowns in $this->form_fields()
        public $foreign_keys  = array(
            'category_id' => array(
                FUEL_FOLDER => 'fuel_categories_model', 
                //'where' => 'context = "news" AND language = "{language}" OR language = ""'
                'where' => ['context' => 'news']
            ),
            'author_id' => 'authors_model',
            #'author_id' => array(
            #   'authors_model', 
            #   'where' => ['authors.active' => 'yes']
            #)
        );
    
  • After more experimentation (and figuring out generated post pages) I suspect the **get_categories_linked() **problem has to do with me extending Fuel_categories_model class.

    Here is what I did:

    in MY_fuel_models.php add

    $config['module_overwrites']['categories'] = ['model_name' => 'my_fuel_categories_model', 'model_location' => 'app'];
    

    And then create application/models/My_fuel_categories_model.php with the following content (I added some more fields)

    require_once(FUEL_PATH.'models/Fuel_categories_model.php');
    
    /**
     * Overload Fuel_categories_model
     */
    
    class My_fuel_categories_model extends Fuel_categories_model {
        public $auto_encode_entities = FALSE;
    
        function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Site variable form fields array
         *
         * @access  public
         * @param   array Values of the form fields (optional)
         * @param   array An array of related fields. This has been deprecated in favor of using has_many and belongs to relationships (deprecated)
         * @return  array An array to be used with the Form_builder class
         */
            public function form_fields($values = array(), $related = array()){
    
                $fields = parent::form_fields($values, $related);
    
                $fields['icon'] = ['type' => '', 'label' => 'Icon'];
                $fields['class'] = ['type' => '', 'label' => 'CSS Klasse'];
    
                $fields['context'] = [
                    'type' => 'select',
                    'first_option' => 'Choose context...',
                    // TODO: put in config file
                    'options' => [
                        'news'     => 'news',
                        'events'   => 'events',
                    ]
                ];
    
                return $fields;
            }
    
    }
    
    // NEEDED!
    class My_fuel_category_model extends Fuel_category_model {
    
    }
    
    

    After I added class My_fuel_category_model extends Fuel_category_model {} some things started to work as expected and get_categories_linked() didn't threw an error anymore - but it just returned null :)

    So my question is, when I extend the original Fuel_category_model what else do I have to look for?

    Thanks.

  • You may try adding the following property to your model to see if that fixes the issue:

    protected $record_class = ''Fuel_category";
    
Sign In or Register to comment.