• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dataset from database accessible on all controllers and views

#1
Sad 
Hello thanks to the whole community and to whom you have dedicated your time to read this post.

The problem I have is that I have a table in the database called ´settings´ that inside has only two columns ´config_name´ and ´config_value´ with which I intend to control some functions of the application. The issue is that I capture all the data well but when I want to pass it globally so that all the drivers are able to read one or more given configurations, this does not happen, how can I do it? Greetings and thank you very much
Reply

#2
I think that almost every developer had a similar problem. here is my solution for CI4.

App/Config/SiteSettings.php
PHP Code:
namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
SiteSettings extends BaseConfig
{
    // some persistent params
    public $dtf = [
        'short'     => 'd.m.y H:i'
    ];
    // params from DB
    public static $registrars = [
        '\App\Models\SiteSettings'
    ];


App/Models/SiteSettings.php
PHP Code:
namespace App\Models;

class 
SiteSettings 
{
    public static function siteSettings() 
    {
        $config = [];
        
        $db 
= \Config\Database::connect();
        $res $db->query("SELECT config_name, config_value FROM settings");
        
        
foreach($res->getResult() as $row)
        {
            $config[$row->config_name] = $row->config_value;
        }

        return $config;
    }


Common.php
PHP Code:
if(!function_exists('configItem'))
{
    function configItem($name$default NULL)
    {
        $cs config('SiteSettings');

        return $cs->{$name} ?? $default;
    }


and further anywhere in the code

in controller: $param_test = configItem('param_test', 'some_default_value');
or in view: <?php configItem('param_test'); ?>
or in BaseController: $this->settings = config('SiteSettings');

at the same time, access to the database table occurs only once upon the first call of the config('SiteSettings')
Reply

#3
Thank you very much for your input, something like this i have programmed, but i am using Codeigniter3 and I really can't find a way to make it work on CI3, Thanks
Reply

#4
For CI3 the same thing can be done through the hook system

hooks/Site_settings_hook.php
PHP Code:
class Site_settings_hook {
    function load() {
        $site_config = [];
        $ci = & get_instance();
        
        $qid 
$ci->db->query("SELECT config_name, config_value FROM settings");
        
        
if($qid && $qid->num_rows() > 0) {
            foreach($qid->result() as $row) {
                $site_config[$row->unit_name] = $row->unit_value;
            }

            $config = & get_config();
            
            $config 
array_merge($config$site_config);
        }
    }


config/hooks.php
PHP Code:
$hook['post_controller_constructor'][] = array(
    'class'    => 'Site_settings_hook',
    'function' => 'load',
    'filename' => 'Site_settings_hook.php',
    'filepath' => 'hooks'
); 

as a result, all values from the database become available in the application config
Reply

#5
Thumbs Up 
Thank you very much for the help, I will implement it and then I will tell you the result, again thank you very much and greetings from Cuba
Reply

#6
Hello again, I have modified something in the code that you have put, because the CI super object did not work for me, in the end it was something like this:
PHP Code:
class Site_settings_hook extends CI_Model {

    function 
load() {

         
$this->obtain_config();

    }
    function 
obtain_config() {
        
$settings = [];

        
$query $this->db->query('SELECT config_name, config_value FROM gallery_config');

        foreach (
$query->result() as $row){
            
$settings[$row->config_name] = $row->config_value;
        }
        
$this->db->close();
        
$config = & get_config();

        
$config array_merge($config$settings);
    } 

but I still have the same problem, I have to declare each variable in each controller for each view I want to use, I would like to know if it is possible to do something like what I have developed outside of codeigniter for another project, which is something like this:
PHP Code:
    function obtain_config(){
            
$db = new db();
            
$config $_config = array();

            
$sql "SELECT config_name, config_value FROM settings";
            
$result $db->query($sql);

                while(
$row $result->fetch_array()){
                    
$config[$row['config_name']] = $row['config_value'];
                }
            
$db->close();

            return 
$config;

PHP Code:
$config obtain_config() 

so with this code when I want to get the value of a row in the confi_name table I just have to write.
PHP Code:
$config ['row name'
Thanks   Confused
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.