Welcome Guest, Not a member yet? Register   Sign In
need "create a helper" to codeigniter 4 in the 2021
#3

(01-14-2021, 05:59 PM)iRedds Wrote: Do not do that. You are violating the integrity of the MVC pattern.

Better to make a model. Even simplified it is better than how you do.

Model example
PHP Code:
<?php


namespace App\Models;


use 
CodeIgniter\Database\ConnectionInterface;
use 
Config\Database;

class 
CommonQueries
{

    /**
     * @var ConnectionInterface
     */
    protected $db;

    public function __construct(?ConnectionInterface $db null)
    {
        $this->db $db ?? Database::connect();
    }

    public function getInfoSet($id '') : ?string
    
{
        $row $this->db->table('get_info_set')
            ->select('description')
            ->where(['id' => $id])
            ->get()
            ->getFirstRow();

        return $row $row->description null;
    }


And helper if you needed. 
PHP Code:
if (! function_exists('get_info_set')) {
    function get_info_set($type '') : ?string {
        return (new \App\Models\CommonQueries())->getInfoSet($type);
    }

Is good code iRedds, thanks.
Reply


Messages In This Thread
RE: need "create a helper" to codeigniter 4 in the 2021 - by yuma2020 - 01-14-2021, 07:38 PM



Theme © iAndrew 2016 - Forum software by © MyBB