• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need "create a helper" to codeigniter 4 in the 2021

#1
Exclamation 
Hello, as you will see I am seeing how to create an helper for codeigiter 4, as you will see in codeigniter 3 is different, I would like to know how to create the helper so that it is not a problem. The next code is CI3, as you can see I'm not entirely sure it works, if it works and how it works, because codeigniter 4 documentations are only tied to pre-built codes, but they don't talk about functional examples, let alone full integration.
PHP Code:
if (! function_exists('get_info_set')) {
  function get_info_set($id '') {
    $CI    =&    get_instance();
    $CI->load->database();

    $CI->db->where('id'$id);
    $result $CI->db->get('get_info_set')->row('data');
    return $result;
  }


error:
PHP Code:
APPPATH\Helpers\common_helper.php at line 19 
This line:
PHP Code:
$CI    =&    get_instance(); 
 
"Missing information", I'm not entirely sure if this code worked or should happen similarly to a model, in this case as serious?
I hope you know how to understand, I'm interested to know, but in the documentation this information is not found, explained, or related if it is really valid to work it as a kind of model or other necessary treatment.
Since I've been waiting for your prompt response. Greetings community.

UPDATE: Thu Jan 14 20:46:25 -03 2021
solved this mode:
PHP Code:
if (! function_exists('get_info_set')) {
  function get_info_set($type '') {
    $db = \Config\Database::connect();
    $builder $db->table('get_info_set');
    $builder->select('description')->where(['id' => $id]);
    $query $builder->get();
    foreach ($query->getResult() as $row
    {
        $result $row
    }
    return $result->description

    $db->close();
  }

Reply

#2
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);
    }

Reply

#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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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