Search code examples
phpmysqldatabasecodeigniter-4medoo

How To Use Medoo Database In Codeigniter 4


I couldn't find the solution and the correct way to call a Medoo class to use in codeigniter 4. I could run it in codeigniter 3, but not codeigniter 4.

Below my error :

enter image description here

And this is my code :

File 1 (Dennis_layout_center) :

namespace App\Controllers;

use App\Models\Dennis_medoo_model;
 
class Dennis_layout_center extends BaseController {

    public function __construct()
    {
            
    }
    
    // Show Command Layout
    public function index()
    {   
    
        $this->Command_Layout();
    }
    
    
    // Show Command Layout
    public function Command_Layout() {
        $Medoo = new Dennis_medoo_model();
        $Data['Response'] = $Medoo->FFetchBotCommand();
        echo view('index', $Data);
        
    }
}

File 2 : (Dennis_medoo_model)

namespace App\Models;
use CodeIgniter\Model;

    class Dennis_medoo_model extends Model {
        
        public $database;
        public $maxfolder;
        
        function __construct() 
        {
            
                include APPPATH . 'ThirdParty/vendor/autoload.php';
                include APPPATH.  'ThirdParty/medoo/vendor/catfan/medoo/src/Medoo.php';
            
            
                $this->database = new Medoo([
                // [required]
                'type' => 'mysql',
                'host' => 'localhost',
                'database' => 'db_test',
                'username' => 'test',
                'password' => 'test',

                // [optional]
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_general_ci',
                'port' => 3306,

                // [optional] Table prefix, all table names will be prefixed as PREFIX_table.
                'prefix' => '',

                // [optional] Enable logging, it is disabled by default for better performance.
                'logging' => true,

                // [optional]
                // Error mode
                // Error handling strategies when error is occurred.
                // PDO::ERRMODE_SILENT (default) | PDO::ERRMODE_WARNING | PDO::ERRMODE_EXCEPTION
                // Read more from https://www.php.net/manual/en/pdo.error-handling.php.
                'error' => PDO::ERRMODE_SILENT,

                // [optional]
                // The driver_option for connection.
                // Read more from http://www.php.net/manual/en/pdo.setattribute.php.
                'option' => [
                PDO::ATTR_CASE => PDO::CASE_NATURAL
                ],

                // [optional] Medoo will execute those commands after connected to the database.
                'command' => [
                'SET SQL_MODE=ANSI_QUOTES'
                ]
                ]);
            
        }
    }

I just call : $Medoo = new Dennis_medoo_model();

But I got error in here : $this->database = new Medoo Which shown in Line 17.

When I call another model it is working.

Edit : How is the correct way to use thirdparty in codeigniter 4

What should I do now ?

Thank You


Solution

  • I just missed this line :

    // Using Medoo namespace.

    use Medoo\Medoo;