Ditch Eloquent for PDO? How to add PDO to Models

Hi Guys!

Im trying, for the last day or so, to figure out how to use Models with PDO. But without any success. I have made all my code available here: https://github.com/ArneAnka/slim3-skeleton So go ahead and check out what I’m talking about. Currently I’m getting Using $this when not in object context in User.php Here you can review my User.php file.

And I’m injection User.php with the PDO instance from dependencies.php

// -----------------------------------------------------------------------------
// Model factories
// -----------------------------------------------------------------------------
$container['User'] = function ($container) {
    return new App\Models\User($container->get('db'));
};

So what I’m doing wrong here? Cant wrap my head around it.

(If I’m not abel to solve this, my next question will be how to add FluentPDO to a slim project :grin:)

Go ahead with Push request if you wish!

You cannot use $this in a static method. You either need to change that method to not be static or change the $db property to be static and reference it as a static property.

static::$db->

Thanks for your reply!

Yes i noticed my mistake about the static function, and I have corrected that. But still, no luck.

I have made som changes to both HomeController and User model. And when calling $this->user->all() from HomeController, it should return This return should be from a query.... (from the repo).

But I’m receiving
Call to a member function all() on unknown

I don’t know how access User model´s functions

I don’t see the User model being passed in here https://github.com/ArneAnka/slim3-skeleton/blob/master/app/src/Controllers/HomeController.php#L17

When adding

	public function __construct(View $view, LoggerInterface $logger, FlashMessages $flash, User $user)
	{
		$this->view = $view;
		$this->flash = $flash;
		$this->logger = $logger;
		$this->User = $user;
	}

I get the following error:

Argument 4 passed to App\Controllers\HomeController::__construct() must be an instance of App\Models\User, none given, called

Because you also need to add it here https://github.com/ArneAnka/slim3-skeleton/blob/master/app/dependencies.php#L84-L86

return new App\Controllers\HomeController($c->view,
                                              $c->logger,
                                              $c->flash,
                                              $c->get('User'));

Thanks for your help!

I got things up and running. But I’m not really satisfied with the way things work. Seems like a lot of work to attached the model to the container, then inject the model into the controller. Almost as doing the same thing twice.

Is their another way of doing this?

There are other ways, but this is the recommended approach. This was you know exactly what your controller needs and exactly where it came from and how it was setup. You can look at PHP-DI which will do some magical stuff for you so you don’t need to do everything explicitly.

I’m not sure what you want to achieve here, since Eloquent wraps PDO. Do you want to execute raw SQL queries? If so, I may have a solution for you (it’s probably far from optimal but it allowed me to migrate an application with lots of raw SQL queries quickly and painlessly).

Yes i want to execute raw queries =) But I’m thinking of FluentPDO repo. That actually seems to be in the right direction for me.

I implemented the following PDO wrapper class which worked really well for me. I’m not saying that this is an optimal approach, but it allowed me to easily convert a codebase containing lots of raw SQL.

class DBUtil
{
    public static function getConnection ()
    {
        return \Illuminate\Database\Capsule\Manager::connection();
    }


    public static function select ($sql, $bindings=[], $fetchMode=\PDO::FETCH_ASSOC)
    {
        if (!$sql) {
            throw new \Exception ('Invalid sql statement received');
        }

        $dbConn = self::getConnection();
        $dbConn->setFetchMode ($fetchMode);
        $rows = $dbConn->select ($sql, $bindings);

        return $rows;
    }


    public static function selectOne ($sql, $bindings=[], $fetchMode=\PDO::FETCH_ASSOC)
    {
        if (!$sql) {
            throw new \Exception ('Invalid sql statement received');
        }

        $dbConn = self::getConnection();
        $dbConn->setFetchMode ($fetchMode);
        $row = $dbConn->selectOne ($sql, $bindings);

        return $row;
    }


    public static function delete ($sql, $bindings)
    {
        if (!$sql) {
            throw new \Exception ('Invalid sql statement received');
        }

        $dbConn = self::getConnection();
        return $dbConn->delete ($sql, $bindings);
    }


    public static function insert ($sql, $bindings)
    {
        if (!$sql) {
            throw new \Exception ('Invalid sql statement received');
        }

        $dbConn = self::getConnection();
        $dbConn->insert ($sql, $bindings);
        return $dbConn->getPdo()->lastInsertId();
    }


    public static function update ($sql, $bindings)
    {
        if (!$sql) {
            throw new \Exception ('Invalid sql statement received');
        }

        $dbConn = self::getConnection();
        return $dbConn->update ($sql, $bindings);
    }
}

Hopefully this helps.