Home » Php » php – Slim 4: Use a container to share DB connection with models and controllers without Eloquent

php – Slim 4: Use a container to share DB connection with models and controllers without Eloquent

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am starting a PET project with SLIM 4 and I can’t find a way to use the connection to the database within a Model. Even the controller works ok.

I have used a container to be able to move the connection between the different layers.

app.php

<?php
use Slim\Factory\AppFactory;

require __DIR__ . '/../../vendor/autoload.php';

//set container
$aux_container = new \DI\Container();
AppFactory::setContainer($aux_container);

$app = AppFactory::create();

//get container to manage dependencies on the rest of app files
$container = $app->getContainer(); 
require __DIR__ . '/../App/routes.php';
require __DIR__ . '/../App/configs.php';
require __DIR__ . '/../App/dependencies.php';

$app->run();

configs.php

<?php

$container->set('db_settings',function(){
    return (object)[
        "DB_NAME" => "prohip_operations",
        "DB_USER" => "root",
        "DB_PASS" => "root",
        "DB_CHAR" => "utf8mb4",
        "DB_HOST" => "localhost",
        "DB_PORT" => "33066",
    ];
});

dependencies.php

<?php

use Psr\Container\ContainerInterface;

$container->set('db', function(ContainerInterface $c){

    $config = $c->get('db_settings');

    $host = $config->DB_HOST;
    $pass = $config->DB_PASS;
    $charset = $config->DB_CHAR;
    $user = $config->DB_USER;
    $dbname = $config->DB_NAME;
    $port = $config->DB_PORT;

    $opt = [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
    ];

    $dsn = "mysql:host=".$host.";port=".$port.";dbname=".$dbname.";charset=".$charset.";";

    return new PDO($dsn, $user, $pass, $opt);
}); 

/Models/BaseModel.php

<?php

namespace App\Models;

use Psr\Container\ContainerInterface;

class BaseModel{

    protected $container;

    public function __construct(ContainerInterface $c){
        $this->container = $c;
    }
}

/Models/ApplicantStatus.php

<?php

namespace App\Models;

use App\Models\BaseModel;

class ApplicantStatus extends BaseModel{

    public function __construct($id)
    {
        if(isset($id)){
            $this->id = $id;
        }

    }

    public function getStatusName(){
        var_dump($this->container);
        //get container db
        $pdo = $this->container->get('db');
        $query = $pdo->query('SELECT description_spanish FROM applicant_status WHERE id='.$this->id);

        if($query->rowCount()>0){
            return $query;
        }
    }
}

When make var_dump() of $ this-> container printa NULL, when in theory they threaten to initialize and extend it from BaseModel …

This method ApplicantStatus-> getStatusName(); it’s called inside a controller.

$status = new ApplicantStatus(11);
$status->getStatusName()

Maybe this is where i do it wrong …

Thank you

How to&Answers:

SOLVED:

I miss the parent contructor declaration.

class ApplicantStatus extends BaseModel
{
    private $id;

    public function __construct(ContainerInterface $c, int $id = null)
    {
        parent::__construct($c);

        $this->id = $id
    }
...
}